Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporters/prometheus] Omit underscore suffix for unitless metrics #2323

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Increment the:

* [DEPRECATION] Deprecate ZPAGES
[#2291](https://github.com/open-telemetry/opentelemetry-cpp/pull/2291)
* [EXPORTER] Omit underscore suffix for unitless metrics
[#2323](https://github.com/open-telemetry/opentelemetry-cpp/pull/2323)

## [1.11.0] 2023-08-21

Expand Down
6 changes: 5 additions & 1 deletion exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
auto unit = metric_data.instrument_descriptor.unit_;
auto sanitized = SanitizeNames(origin_name);
prometheus_client::MetricFamily metric_family;
metric_family.name = sanitized + "_" + unit;
metric_family.name = sanitized;
if (!unit.empty())
{
metric_family.name += "_" + unit;
};
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_)
Expand Down
9 changes: 9 additions & 0 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,13 @@ TEST(PrometheusExporterUtils, SanitizeName)
ASSERT_EQ(exporter::metrics::SanitizeNameTester::sanitize("name?__name:"), "name_name:");
}

TEST(PrometheusExporterUtils, HandleMissingUnit)
{
TestDataPoints dp;
metric_sdk::ResourceMetrics data = dp.CreateSumPointData("");
std::vector<prometheus::MetricFamily> family =
PrometheusExporterUtils::TranslateToPrometheus(data);
ASSERT_EQ(family.begin()->name, "library_name");
}

OPENTELEMETRY_END_NAMESPACE
4 changes: 2 additions & 2 deletions exporters/prometheus/test/prometheus_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct TestDataPoints
/**
* Helper function to create ResourceMetrics
*/
inline metric_sdk::ResourceMetrics CreateSumPointData()
inline metric_sdk::ResourceMetrics CreateSumPointData(std::string unit = "unit")
{
metric_sdk::SumPointData sum_point_data{};
sum_point_data.value_ = 10.0;
Expand All @@ -34,7 +34,7 @@ struct TestDataPoints
metric_sdk::ResourceMetrics data;
data.resource_ = &resource;
metric_sdk::MetricData metric_data{
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentDescriptor{"library_name", "description", unit,
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
Expand Down