From 99bed279e813a8e17962a65ef1bd9ae859d77b7f Mon Sep 17 00:00:00 2001 From: Punya Biswal Date: Mon, 25 Sep 2023 04:25:42 -0400 Subject: [PATCH] [exporters/prometheus] Get rid of friend test Instead, test using the TranslateToPrometheus public API. --- .../exporters/prometheus/exporter_utils.h | 3 -- .../prometheus/test/exporter_utils_test.cc | 48 ++++++++++--------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h index cb7da0b2b5..312b641c3e 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter_utils.h @@ -104,9 +104,6 @@ class PrometheusExporterUtils const std::vector &boundaries, const std::vector &counts, ::prometheus::ClientMetric *metric); - - // For testing - friend class SanitizeNameTester; }; } // namespace metrics } // namespace exporter diff --git a/exporters/prometheus/test/exporter_utils_test.cc b/exporters/prometheus/test/exporter_utils_test.cc index 0c08e2ffa7..228b63dbcf 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,33 @@ 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({}); + 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, All) { - 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_"); + CheckSanitation("name?__name", "name_name"); + CheckSanitation("name?__name:", "name_name:"); } class AttributeCollisionTest : public ::testing::Test