Skip to content

Commit

Permalink
[exporters/prometheus] More namespace cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
punya committed Sep 24, 2023
1 parent 31dabd8 commit 80308a7
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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:
/**
Expand All @@ -38,7 +36,7 @@ class PrometheusCollector : public prometheus_client::Collectable
*
* @return all metrics in the metricsToCollect snapshot
*/
std::vector<prometheus_client::MetricFamily> Collect() const override;
std::vector<::prometheus::MetricFamily> Collect() const override;

private:
sdk::metrics::MetricReader *reader_;
Expand Down
8 changes: 5 additions & 3 deletions exporters/prometheus/src/collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +26,7 @@ PrometheusCollector::PrometheusCollector(sdk::metrics::MetricReader *reader) : r
*
* @return all metrics in the metricsToCollect snapshot
*/
std::vector<prometheus_client::MetricFamily> PrometheusCollector::Collect() const
std::vector<::prometheus::MetricFamily> PrometheusCollector::Collect() const
{
if (reader_->IsShutdown())
{
Expand All @@ -35,7 +37,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusCollector::Collect() cons
}
collection_lock_.lock();

std::vector<prometheus_client::MetricFamily> 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)
Expand Down
46 changes: 23 additions & 23 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -184,7 +184,7 @@ struct ClientMetricWrapper
* Handle Counter.
*/
template <typename T>
void SetValue(const std::vector<T> &values, prometheus_client::MetricType type)
void SetValue(const std::vector<T> &values, ::prometheus::MetricType type)
{
double value = 0.0;
const auto &value_var = values[0];
Expand All @@ -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;
}
Expand All @@ -226,18 +226,18 @@ struct ClientMetricWrapper
metric.histogram.sample_sum = values[0];
metric.histogram.sample_count = values[1];
int cumulative = 0;
std::vector<prometheus_client::ClientMetric::Bucket> 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<double>::infinity();
Expand Down Expand Up @@ -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<prometheus_client::MetricFamily> TranslateToPrometheus(
std::vector<::prometheus::MetricFamily> TranslateToPrometheus(
const sdk::metrics::ResourceMetrics &data)
{

// initialize output vector
std::vector<prometheus_client::MetricFamily> output;
std::vector<::prometheus::MetricFamily> output;

for (const auto &instrumentation_info : data.scope_metric_data_)
{
Expand All @@ -282,7 +282,7 @@ std::vector<prometheus_client::MetricFamily> 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_)
Expand All @@ -294,10 +294,10 @@ std::vector<prometheus_client::MetricFamily> TranslateToPrometheus(
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;
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<sdk::metrics::HistogramPointData>(point_data_attr.point_data);
Expand All @@ -316,7 +316,7 @@ std::vector<prometheus_client::MetricFamily> TranslateToPrometheus(
.SetValue(std::vector<double>{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<sdk::metrics::LastValuePointData>(
point_data_attr.point_data))
Expand Down
14 changes: 9 additions & 5 deletions exporters/prometheus/test/collector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#include <map>
#include <thread>

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
{
Expand Down Expand Up @@ -82,3 +84,5 @@ TEST(PrometheusCollector, BasicTests)
delete reader;
delete producer;
}

OPENTELEMETRY_END_NAMESPACE
15 changes: 10 additions & 5 deletions exporters/prometheus/test/exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
#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
* an exposer as an argument, and instead takes no arguments; this
* 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.
Expand Down Expand Up @@ -77,3 +80,5 @@ TEST(PrometheusExporter, CheckAggregationTemporality)
ASSERT_EQ(exporter.GetAggregationTemporality(InstrumentType::kUpDownCounter),
AggregationTemporality::kCumulative);
}

OPENTELEMETRY_END_NAMESPACE
21 changes: 10 additions & 11 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
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<T> vals)
{
Expand All @@ -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;
Expand All @@ -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<double> boundaries,
std::vector<int> correct)
{
Expand Down Expand Up @@ -105,7 +104,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", "description", ::prometheus::MetricType::Counter, 1,
vals);
}

Expand All @@ -119,7 +118,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue)

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

Expand All @@ -133,7 +132,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal)

auto metric = translated[0];
std::vector<double> 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<double>{10.1, 20.2, 30.2}, {200, 300, 400, 500});
}
Expand Down
15 changes: 8 additions & 7 deletions exporters/prometheus/test/prometheus_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -123,7 +125,6 @@ struct TestDataPoints
};
} // namespace

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
Expand Down

0 comments on commit 80308a7

Please sign in to comment.