diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h index 46d270905b..4759b96669 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h @@ -12,8 +12,6 @@ #include "opentelemetry/exporters/prometheus/exporter_utils.h" #include "opentelemetry/sdk/metrics/metric_reader.h" -namespace prometheus_client = ::prometheus; - OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter { @@ -22,7 +20,7 @@ namespace metrics /** * The Prometheus Collector maintains the intermediate collection in Prometheus Exporter */ -class PrometheusCollector : public prometheus_client::Collectable +class PrometheusCollector : public ::prometheus::Collectable { public: /** @@ -38,7 +36,7 @@ class PrometheusCollector : public prometheus_client::Collectable * * @return all metrics in the metricsToCollect snapshot */ - std::vector Collect() const override; + std::vector<::prometheus::MetricFamily> Collect() const override; private: sdk::metrics::MetricReader *reader_; diff --git a/exporters/prometheus/src/collector.cc b/exporters/prometheus/src/collector.cc index 20bb6c3e1d..e444364b6a 100644 --- a/exporters/prometheus/src/collector.cc +++ b/exporters/prometheus/src/collector.cc @@ -4,9 +4,11 @@ #include "opentelemetry/exporters/prometheus/collector.h" #include "opentelemetry/sdk/common/global_log_handler.h" -namespace metric_sdk = opentelemetry::sdk::metrics; OPENTELEMETRY_BEGIN_NAMESPACE + +namespace metric_sdk = sdk::metrics; + namespace exporter { namespace metrics @@ -24,7 +26,7 @@ PrometheusCollector::PrometheusCollector(sdk::metrics::MetricReader *reader) : r * * @return all metrics in the metricsToCollect snapshot */ -std::vector PrometheusCollector::Collect() const +std::vector<::prometheus::MetricFamily> PrometheusCollector::Collect() const { if (reader_->IsShutdown()) { @@ -35,7 +37,7 @@ std::vector PrometheusCollector::Collect() cons } collection_lock_.lock(); - std::vector result; + std::vector<::prometheus::MetricFamily> result; reader_->Collect([&result](sdk::metrics::ResourceMetrics &metric_data) { auto prometheus_metric_data = TranslateToPrometheus(metric_data); for (auto &data : prometheus_metric_data) diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index 6c1704947b..c15f44d233 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -12,10 +12,10 @@ #include "opentelemetry/sdk/common/global_log_handler.h" -namespace prometheus_client = ::prometheus; -namespace metric_sdk = opentelemetry::sdk::metrics; - OPENTELEMETRY_BEGIN_NAMESPACE + +namespace metric_sdk = sdk::metrics; + namespace exporter { namespace metrics @@ -47,28 +47,28 @@ metric_sdk::AggregationType getAggregationType(const metric_sdk::PointType &poin /** * Translate the OTel metric type to Prometheus metric type */ -prometheus_client::MetricType TranslateType(metric_sdk::AggregationType kind, bool is_monotonic) +::prometheus::MetricType TranslateType(metric_sdk::AggregationType kind, bool is_monotonic) { switch (kind) { case metric_sdk::AggregationType::kSum: if (!is_monotonic) { - return prometheus_client::MetricType::Gauge; + return ::prometheus::MetricType::Gauge; } else { - return prometheus_client::MetricType::Counter; + return ::prometheus::MetricType::Counter; } break; case metric_sdk::AggregationType::kHistogram: - return prometheus_client::MetricType::Histogram; + return ::prometheus::MetricType::Histogram; break; case metric_sdk::AggregationType::kLastValue: - return prometheus_client::MetricType::Gauge; + return ::prometheus::MetricType::Gauge; break; default: - return prometheus_client::MetricType::Untyped; + return ::prometheus::MetricType::Untyped; } } @@ -184,7 +184,7 @@ struct ClientMetricWrapper * Handle Counter. */ template - void SetValue(const std::vector &values, prometheus_client::MetricType type) + void SetValue(const std::vector &values, ::prometheus::MetricType type) { double value = 0.0; const auto &value_var = values[0]; @@ -199,15 +199,15 @@ struct ClientMetricWrapper switch (type) { - case prometheus_client::MetricType::Counter: { + case ::prometheus::MetricType::Counter: { metric.counter.value = value; break; } - case prometheus_client::MetricType::Gauge: { + case ::prometheus::MetricType::Gauge: { metric.gauge.value = value; break; } - case prometheus_client::MetricType::Untyped: { + case ::prometheus::MetricType::Untyped: { metric.untyped.value = value; break; } @@ -226,18 +226,18 @@ struct ClientMetricWrapper metric.histogram.sample_sum = values[0]; metric.histogram.sample_count = values[1]; int cumulative = 0; - std::vector buckets; + std::vector<::prometheus::ClientMetric::Bucket> buckets; uint32_t idx = 0; for (const auto &boundary : boundaries) { - prometheus_client::ClientMetric::Bucket bucket; + ::prometheus::ClientMetric::Bucket bucket; cumulative += counts[idx]; bucket.cumulative_count = cumulative; bucket.upper_bound = boundary; buckets.emplace_back(bucket); ++idx; } - prometheus_client::ClientMetric::Bucket bucket; + ::prometheus::ClientMetric::Bucket bucket; cumulative += counts[idx]; bucket.cumulative_count = cumulative; bucket.upper_bound = std::numeric_limits::infinity(); @@ -268,12 +268,12 @@ struct MetricFamilyWrapper * @param records a collection of metrics in OpenTelemetry * @return a collection of translated metrics that is acceptable by Prometheus */ -std::vector TranslateToPrometheus( +std::vector<::prometheus::MetricFamily> TranslateToPrometheus( const sdk::metrics::ResourceMetrics &data) { // initialize output vector - std::vector output; + std::vector<::prometheus::MetricFamily> output; for (const auto &instrumentation_info : data.scope_metric_data_) { @@ -282,7 +282,7 @@ std::vector TranslateToPrometheus( auto origin_name = metric_data.instrument_descriptor.name_; auto unit = metric_data.instrument_descriptor.unit_; auto sanitized = SanitizeNames(origin_name); - prometheus_client::MetricFamily metric_family; + ::prometheus::MetricFamily metric_family; metric_family.name = sanitized + "_" + unit; metric_family.help = metric_data.instrument_descriptor.description_; for (const auto &point_data_attr : metric_data.point_data_attr_) @@ -294,10 +294,10 @@ std::vector TranslateToPrometheus( is_monotonic = nostd::get(point_data_attr.point_data).is_monotonic_; } - const prometheus_client::MetricType type = TranslateType(kind, is_monotonic); - metric_family.type = type; + const ::prometheus::MetricType type = TranslateType(kind, is_monotonic); + metric_family.type = type; MetricFamilyWrapper wrapper{metric_family}; - if (type == prometheus_client::MetricType::Histogram) // Histogram + if (type == ::prometheus::MetricType::Histogram) // Histogram { auto histogram_point_data = nostd::get(point_data_attr.point_data); @@ -316,7 +316,7 @@ std::vector TranslateToPrometheus( .SetValue(std::vector{sum, (double)histogram_point_data.count_}, boundaries, counts); } - else if (type == prometheus_client::MetricType::Gauge) + else if (type == ::prometheus::MetricType::Gauge) { if (nostd::holds_alternative( point_data_attr.point_data)) diff --git a/exporters/prometheus/test/collector_test.cc b/exporters/prometheus/test/collector_test.cc index 8947dcfd27..5b191e7d6c 100644 --- a/exporters/prometheus/test/collector_test.cc +++ b/exporters/prometheus/test/collector_test.cc @@ -11,11 +11,13 @@ #include #include -using opentelemetry::exporter::metrics::PrometheusCollector; -using opentelemetry::sdk::metrics::ResourceMetrics; -namespace metric_api = opentelemetry::metrics; -namespace metric_sdk = opentelemetry::sdk::metrics; -namespace metric_exporter = opentelemetry::exporter::metrics; +OPENTELEMETRY_BEGIN_NAMESPACE + +using exporter::metrics::PrometheusCollector; +using sdk::metrics::ResourceMetrics; +namespace metric_api = metrics; +namespace metric_sdk = sdk::metrics; +namespace metric_exporter = exporter::metrics; class MockMetricProducer : public opentelemetry::sdk::metrics::MetricProducer { @@ -82,3 +84,5 @@ TEST(PrometheusCollector, BasicTests) delete reader; delete producer; } + +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/prometheus/test/exporter_test.cc b/exporters/prometheus/test/exporter_test.cc index 3bda4c3d7b..28ed1fd71b 100644 --- a/exporters/prometheus/test/exporter_test.cc +++ b/exporters/prometheus/test/exporter_test.cc @@ -9,6 +9,8 @@ #include "opentelemetry/version.h" #include "prometheus_test_helper.h" +OPENTELEMETRY_BEGIN_NAMESPACE + /** * PrometheusExporterTest is a friend class of PrometheusExporter. * It has access to a private constructor that does not take in @@ -16,11 +18,12 @@ * private constructor is only to be used here for testing */ -using opentelemetry::exporter::metrics::PrometheusCollector; -using opentelemetry::exporter::metrics::PrometheusExporter; -using opentelemetry::exporter::metrics::PrometheusExporterOptions; -using opentelemetry::sdk::metrics::AggregationTemporality; -using opentelemetry::sdk::metrics::InstrumentType; +using exporter::metrics::PrometheusCollector; +using exporter::metrics::PrometheusExporter; +using exporter::metrics::PrometheusExporterOptions; +using sdk::metrics::AggregationTemporality; +using sdk::metrics::InstrumentType; + /** * When a PrometheusExporter is initialized, * isShutdown should be false. @@ -77,3 +80,5 @@ TEST(PrometheusExporter, CheckAggregationTemporality) ASSERT_EQ(exporter.GetAggregationTemporality(InstrumentType::kUpDownCounter), AggregationTemporality::kCumulative); } + +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/prometheus/test/exporter_utils_test.cc b/exporters/prometheus/test/exporter_utils_test.cc index 5839caa106..91f190e605 100644 --- a/exporters/prometheus/test/exporter_utils_test.cc +++ b/exporters/prometheus/test/exporter_utils_test.cc @@ -13,13 +13,12 @@ OPENTELEMETRY_BEGIN_NAMESPACE using exporter::metrics::TranslateToPrometheus; namespace metric_sdk = sdk::metrics; namespace metric_api = metrics; -namespace prometheus_client = ::prometheus; template -void assert_basic(prometheus_client::MetricFamily &metric, +void assert_basic(::prometheus::MetricFamily &metric, const std::string &sanitized_name, const std::string &description, - prometheus_client::MetricType type, + ::prometheus::MetricType type, int label_num, std::vector vals) { @@ -38,23 +37,23 @@ void assert_basic(prometheus_client::MetricFamily &metric, switch (type) { - case prometheus_client::MetricType::Counter: { + case ::prometheus::MetricType::Counter: { ASSERT_DOUBLE_EQ(metric_data.counter.value, vals[0]); break; } - case prometheus_client::MetricType::Histogram: { + case ::prometheus::MetricType::Histogram: { ASSERT_DOUBLE_EQ(metric_data.histogram.sample_count, vals[0]); ASSERT_DOUBLE_EQ(metric_data.histogram.sample_sum, vals[1]); auto buckets = metric_data.histogram.bucket; ASSERT_EQ(buckets.size(), vals[2]); break; } - case prometheus_client::MetricType::Gauge: { + case ::prometheus::MetricType::Gauge: { ASSERT_DOUBLE_EQ(metric_data.gauge.value, vals[0]); break; } break; - case prometheus_client::MetricType::Summary: + case ::prometheus::MetricType::Summary: // Summary and Info type not supported ASSERT_TRUE(false); break; @@ -65,7 +64,7 @@ void assert_basic(prometheus_client::MetricFamily &metric, } } -void assert_histogram(prometheus_client::MetricFamily &metric, +void assert_histogram(::prometheus::MetricFamily &metric, std::list boundaries, std::vector correct) { @@ -105,7 +104,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter) auto metric1 = translated[0]; std::vector vals = {10}; - assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Counter, 1, + assert_basic(metric1, "library_name", "description", ::prometheus::MetricType::Counter, 1, vals); } @@ -119,7 +118,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue) auto metric1 = translated[0]; std::vector vals = {10}; - assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Gauge, 1, + assert_basic(metric1, "library_name", "description", ::prometheus::MetricType::Gauge, 1, vals); } @@ -133,7 +132,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal) auto metric = translated[0]; std::vector vals = {3, 900.5, 4}; - assert_basic(metric, "library_name", "description", prometheus_client::MetricType::Histogram, 1, + assert_basic(metric, "library_name", "description", ::prometheus::MetricType::Histogram, 1, vals); assert_histogram(metric, std::list{10.1, 20.2, 30.2}, {200, 300, 400, 500}); } diff --git a/exporters/prometheus/test/prometheus_test_helper.h b/exporters/prometheus/test/prometheus_test_helper.h index d42f66c6eb..35b28ecaa3 100644 --- a/exporters/prometheus/test/prometheus_test_helper.h +++ b/exporters/prometheus/test/prometheus_test_helper.h @@ -5,16 +5,18 @@ #include "opentelemetry/version.h" -namespace metric_sdk = opentelemetry::sdk::metrics; -namespace nostd = opentelemetry::nostd; -namespace exportermetrics = opentelemetry::exporter::metrics; +OPENTELEMETRY_BEGIN_NAMESPACE + +namespace metric_sdk = sdk::metrics; +namespace nostd = nostd; +namespace exportermetrics = exporter::metrics; namespace { -using opentelemetry::sdk::instrumentationscope::InstrumentationScope; -using opentelemetry::sdk::resource::Resource; -using opentelemetry::sdk::resource::ResourceAttributes; +using sdk::instrumentationscope::InstrumentationScope; +using sdk::resource::Resource; +using sdk::resource::ResourceAttributes; struct TestDataPoints { @@ -123,7 +125,6 @@ struct TestDataPoints }; } // namespace -OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { namespace metrics