diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h index 1da9baf6ab..9122626ede 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h @@ -25,24 +25,11 @@ class PrometheusExporterUtils * Helper function to convert OpenTelemetry metrics data collection * to Prometheus metrics data collection * - * @param records a collection of metrics in OpenTelemetry + * @param data a collection of metrics in OpenTelemetry * @return a collection of translated metrics that is acceptable by Prometheus */ static std::vector<::prometheus::MetricFamily> TranslateToPrometheus( const sdk::metrics::ResourceMetrics &data); - -private: - /** - * Sanitize the given metric name or label according to Prometheus rule. - * - * This function is needed because names in OpenTelemetry can contain - * alphanumeric characters, '_', '.', and '-', whereas in Prometheus the - * name should only contain alphanumeric characters and '_'. - */ - static std::string SanitizeNames(std::string name); - - // For testing - friend class SanitizeNameTester; }; } // namespace metrics } // namespace exporter diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index e4ad5664d9..053841f623 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -112,7 +112,14 @@ std::string AttributeValueToString(const opentelemetry::sdk::common::OwnedAttrib return result; } -std::string SanitizeNamesLocal(std::string name) +/** + * Sanitize the given metric name or label according to Prometheus rule. + * + * This function is needed because names in OpenTelemetry can contain + * alphanumeric characters, '_', '.', and '-', whereas in Prometheus the + * name should only contain alphanumeric characters and '_'. + */ +std::string SanitizeNames(std::string name) { constexpr const auto replacement = '_'; constexpr const auto replacement_dup = '='; @@ -166,7 +173,7 @@ struct ClientMetricWrapper size_t i = 0; for (auto const &label : labels) { - auto sanitized = SanitizeNamesLocal(label.first); + auto sanitized = SanitizeNames(label.first); metric.label[i].name = sanitized; metric.label[i++].value = AttributeValueToString(label.second); } @@ -356,18 +363,6 @@ std::vector PrometheusExporterUtils::TranslateT return output; } -/** - * Sanitize the given metric name or label according to Prometheus rule. - * - * This function is needed because names in OpenTelemetry can contain - * alphanumeric characters, '_', '.', and '-', whereas in Prometheus the - * name should only contain alphanumeric characters and '_'. - */ -std::string PrometheusExporterUtils::SanitizeNames(std::string name) -{ - return SanitizeNamesLocal(name); -} - } // namespace metrics } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/prometheus/test/exporter_utils_test.cc b/exporters/prometheus/test/exporter_utils_test.cc index ac9ed4545f..f5a6983e43 100644 --- a/exporters/prometheus/test/exporter_utils_test.cc +++ b/exporters/prometheus/test/exporter_utils_test.cc @@ -15,21 +15,6 @@ namespace prometheus_client = ::prometheus; OPENTELEMETRY_BEGIN_NAMESPACE -namespace exporter -{ -namespace metrics -{ -class SanitizeNameTester -{ -public: - static std::string sanitize(std::string name) - { - return PrometheusExporterUtils::SanitizeNames(name); - } -}; -} // namespace metrics -} // namespace exporter - template void assert_basic(prometheus_client::MetricFamily &metric, const std::string &sanitized_name, @@ -153,14 +138,32 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal) assert_histogram(metric, std::list{10.1, 20.2, 30.2}, {200, 300, 400, 500}); } -TEST(PrometheusExporterUtils, SanitizeName) +class SanitizeNameTest : public ::testing::Test +{ + Resource resource_ = Resource::Create(ResourceAttributes{}); + nostd::unique_ptr instrumentation_scope_ = + InstrumentationScope::Create("library_name", "1.2.0"); + +protected: + void CheckSanitation(const std::string &original, const std::string &sanitized) + { + metric_sdk::InstrumentDescriptor instrument_descriptor{ + original, "description", "unit", metric_sdk::InstrumentType::kCounter, + metric_sdk::InstrumentValueType::kDouble}; + std::vector result = PrometheusExporterUtils::TranslateToPrometheus( + {&resource_, + {{instrumentation_scope_.get(), {{instrument_descriptor, {}, {}, {}, {{}}}}}}}); + EXPECT_EQ(result.begin()->name, sanitized + "_unit"); + } +}; + +TEST_F(SanitizeNameTest, SanitizeName) { - ASSERT_EQ(exporter::metrics::SanitizeNameTester::sanitize("name"), "name"); - ASSERT_EQ(exporter::metrics::SanitizeNameTester::sanitize("name?"), "name_"); - ASSERT_EQ(exporter::metrics::SanitizeNameTester::sanitize("name???"), "name_"); - ASSERT_EQ(exporter::metrics::SanitizeNameTester::sanitize("name?__"), "name_"); - ASSERT_EQ(exporter::metrics::SanitizeNameTester::sanitize("name?__name"), "name_name"); - ASSERT_EQ(exporter::metrics::SanitizeNameTester::sanitize("name?__name:"), "name_name:"); + CheckSanitation("name", "name"); + CheckSanitation("name?", "name_"); + CheckSanitation("name???", "name_"); + CheckSanitation("name?__name", "name_name"); + CheckSanitation("name?__name:", "name_name:"); } OPENTELEMETRY_END_NAMESPACE