Skip to content

Commit

Permalink
add _total suffix to prometheus counters
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Sep 1, 2023
1 parent f52ec6c commit bc16feb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Increment the:

## [Unreleased]

* [EXPORTER] Add _total suffixes to Prometheus counters
[#2288](https://github.com/open-telemetry/opentelemetry-cpp/pull/2288)

## [1.11.0] 2023-08-21

* [BUILD] Fix more cases for symbol name for 32-bit win32 DLL build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class PrometheusExporterUtils
static opentelemetry::sdk::metrics::AggregationType getAggregationType(
const opentelemetry::sdk::metrics::PointType &point_type);

inline bool endsWith(std::string const & value, std::string const & ending);

/**
* Translate the OTel metric type to Prometheus metric type
*/
Expand Down
41 changes: 29 additions & 12 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,35 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
{
for (const auto &metric_data : instrumentation_info.metric_data_)
{
auto origin_name = metric_data.instrument_descriptor.name_;
auto unit = metric_data.instrument_descriptor.unit_;
auto sanitized = SanitizeNames(origin_name);
if (metric_data.point_data_attr_.empty()) {
continue;
}
prometheus_client::MetricFamily metric_family;
auto front = metric_data.point_data_attr_.front();
auto kind = getAggregationType(front.point_data);
bool is_monotonic = true;
if (kind == sdk::metrics::AggregationType::kSum)
{
is_monotonic =
nostd::get<sdk::metrics::SumPointData>(front.point_data).is_monotonic_;
}
auto type = TranslateType(kind, is_monotonic);
metric_family.type = type;
auto origin_name = metric_data.instrument_descriptor.name_;
auto unit = metric_data.instrument_descriptor.unit_;
auto sanitized = SanitizeNames(origin_name);
if (type == prometheus_client::MetricType::Counter && endsWith(sanitized,"_total")) {
// trim _total from counters, since it will be appended after the unit.
sanitized = sanitized.substr(0, sanitized.length() - sizeof("_total"));
}
metric_family.name = sanitized + "_" + unit;
if (type == prometheus_client::MetricType::Counter) {
metric_family.name += "_total";
}
metric_family.help = metric_data.instrument_descriptor.description_;
auto time = metric_data.end_ts.time_since_epoch();
for (const auto &point_data_attr : metric_data.point_data_attr_)
{
auto kind = getAggregationType(point_data_attr.point_data);
bool is_monotonic = true;
if (kind == sdk::metrics::AggregationType::kSum)
{
is_monotonic =
nostd::get<sdk::metrics::SumPointData>(point_data_attr.point_data).is_monotonic_;
}
const prometheus_client::MetricType type = TranslateType(kind, is_monotonic);
metric_family.type = type;
if (type == prometheus_client::MetricType::Histogram) // Histogram
{
auto histogram_point_data =
Expand Down Expand Up @@ -121,6 +132,12 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
return output;
}

inline bool PrometheusExporterUtils::endsWith(std::string const & value, std::string const & ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}

/**
* Sanitize the given metric name or label according to Prometheus rule.
*
Expand Down
2 changes: 1 addition & 1 deletion exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter)

auto metric1 = translated[0];
std::vector<int> vals = {10};
assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Counter, 1,
assert_basic(metric1, "library_name_total", "description", prometheus_client::MetricType::Counter, 1,
vals);
}

Expand Down

0 comments on commit bc16feb

Please sign in to comment.