SERVER-114946: Add the MetricUnit enum. (#45140)

Co-authored-by: Cedric Sirianni <cedric.sirianni@mongodb.com>
GitOrigin-RevId: b45c6e1684f743daa882567841d3de01a29ca52d
This commit is contained in:
Mike Nugent 2025-12-11 16:39:05 -05:00 committed by MongoDB Bot
parent 66b44393e7
commit 033efe970c
4 changed files with 176 additions and 0 deletions

View File

@ -110,3 +110,16 @@ mongo_cc_unit_test(
"//src/third_party/opentelemetry-cpp/api",
],
)
mongo_cc_library(
name = "metric_units",
srcs = ["metric_units.cpp"],
deps = ["//src/mongo:base"],
)
mongo_cc_unit_test(
name = "metric_units_test",
srcs = ["metric_units_test.cpp"],
tags = ["mongo_unittest_second_group"],
deps = [":metric_units"],
)

View File

@ -0,0 +1,64 @@
/**
* Copyright (C) 2025-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/otel/metrics/metric_units.h"
#include "mongo/logv2/log.h"
namespace mongo::otel::metrics {
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kDefault
StringData toString(MetricUnit unit) {
switch (unit) {
// Time
case MetricUnit::kMicroseconds:
return "microseconds";
case MetricUnit::kMilliseconds:
return "milliseconds";
case MetricUnit::kSeconds:
return "seconds";
// Space
case MetricUnit::kBytes:
return "bytes";
// Database
case MetricUnit::kOperations:
return "operations";
case MetricUnit::kQueries:
return "queries";
// Networking
case MetricUnit::kConnections:
return "connections";
}
LOGV2_FATAL(11494600, "Unknown MetricUnit value", "value"_attr = unit);
}
} // namespace mongo::otel::metrics

View File

@ -0,0 +1,57 @@
/**
* Copyright (C) 2025-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/base/string_data.h"
#include "mongo/util/modules.h"
MONGO_MOD_PUBLIC;
namespace mongo::otel::metrics {
// Used to denote the unit of measurement in a metric.
enum class MetricUnit {
// Time
kMicroseconds,
kMilliseconds,
kSeconds,
// Space
kBytes,
// Database
kOperations,
kQueries,
// Networking
kConnections,
};
// Converts any of the above units to a string.
StringData toString(MetricUnit unit);
} // namespace mongo::otel::metrics

View File

@ -0,0 +1,42 @@
/**
* Copyright (C) 2025-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/otel/metrics/metric_units.h"
#include "mongo/unittest/death_test.h"
#include "mongo/unittest/unittest.h"
namespace mongo::otel::metrics {
TEST(ToStringTest, Works) {
ASSERT_EQ(toString(MetricUnit::kSeconds), "seconds");
}
DEATH_TEST(ToStringDeathTest, DiesOnBadUnit, "11494600") {
toString(static_cast<MetricUnit>(-1));
}
} // namespace mongo::otel::metrics