Skip to content

Commit 35d03b3

Browse files
committed
add _total suffix to prometheus counters
1 parent f52ec6c commit 35d03b3

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [EXPORTER] Add _total suffixes to Prometheus counters
19+
[#2288](https://github.com/open-telemetry/opentelemetry-cpp/pull/2288)
20+
1821
## [1.11.0] 2023-08-21
1922

2023
* [BUILD] Fix more cases for symbol name for 32-bit win32 DLL build

exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class PrometheusExporterUtils
4444
static opentelemetry::sdk::metrics::AggregationType getAggregationType(
4545
const opentelemetry::sdk::metrics::PointType &point_type);
4646

47+
inline bool endsWith(std::string const &value, std::string const &ending);
48+
4749
/**
4850
* Translate the OTel metric type to Prometheus metric type
4951
*/

exporters/prometheus/src/exporter_utils.cc

+32-12
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,37 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
3838
{
3939
for (const auto &metric_data : instrumentation_info.metric_data_)
4040
{
41-
auto origin_name = metric_data.instrument_descriptor.name_;
42-
auto unit = metric_data.instrument_descriptor.unit_;
43-
auto sanitized = SanitizeNames(origin_name);
41+
if (metric_data.point_data_attr_.empty())
42+
{
43+
continue;
44+
}
4445
prometheus_client::MetricFamily metric_family;
46+
auto front = metric_data.point_data_attr_.front();
47+
auto kind = getAggregationType(front.point_data);
48+
bool is_monotonic = true;
49+
if (kind == sdk::metrics::AggregationType::kSum)
50+
{
51+
is_monotonic = nostd::get<sdk::metrics::SumPointData>(front.point_data).is_monotonic_;
52+
}
53+
auto type = TranslateType(kind, is_monotonic);
54+
metric_family.type = type;
55+
auto origin_name = metric_data.instrument_descriptor.name_;
56+
auto unit = metric_data.instrument_descriptor.unit_;
57+
auto sanitized = SanitizeNames(origin_name);
58+
if (type == prometheus_client::MetricType::Counter && endsWith(sanitized, "_total"))
59+
{
60+
// trim _total from counters, since it will be appended after the unit.
61+
sanitized = sanitized.substr(0, sanitized.length() - sizeof("_total"));
62+
}
4563
metric_family.name = sanitized + "_" + unit;
64+
if (type == prometheus_client::MetricType::Counter)
65+
{
66+
metric_family.name += "_total";
67+
}
4668
metric_family.help = metric_data.instrument_descriptor.description_;
4769
auto time = metric_data.end_ts.time_since_epoch();
4870
for (const auto &point_data_attr : metric_data.point_data_attr_)
4971
{
50-
auto kind = getAggregationType(point_data_attr.point_data);
51-
bool is_monotonic = true;
52-
if (kind == sdk::metrics::AggregationType::kSum)
53-
{
54-
is_monotonic =
55-
nostd::get<sdk::metrics::SumPointData>(point_data_attr.point_data).is_monotonic_;
56-
}
57-
const prometheus_client::MetricType type = TranslateType(kind, is_monotonic);
58-
metric_family.type = type;
5972
if (type == prometheus_client::MetricType::Histogram) // Histogram
6073
{
6174
auto histogram_point_data =
@@ -121,6 +134,13 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
121134
return output;
122135
}
123136

137+
inline bool PrometheusExporterUtils::endsWith(std::string const &value, std::string const &ending)
138+
{
139+
if (ending.size() > value.size())
140+
return false;
141+
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
142+
}
143+
124144
/**
125145
* Sanitize the given metric name or label according to Prometheus rule.
126146
*

exporters/prometheus/test/exporter_utils_test.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter)
114114

115115
auto metric1 = translated[0];
116116
std::vector<int> vals = {10};
117-
assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Counter, 1,
118-
vals);
117+
assert_basic(metric1, "library_name_total", "description", prometheus_client::MetricType::Counter,
118+
1, vals);
119119
}
120120

121121
TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue)

0 commit comments

Comments
 (0)