Skip to content

Commit

Permalink
[EXPORTER] Add option to disable Prometheus otel_scope_name and otel_…
Browse files Browse the repository at this point in the history
…scope_version attributes (open-telemetry#2451)
  • Loading branch information
timwoj authored Dec 16, 2023
1 parent e79a10f commit e8afbb8
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class PrometheusCollector : public prometheus_client::Collectable
* This constructor initializes the collection for metrics to export
* in this class with default capacity
*/
explicit PrometheusCollector(sdk::metrics::MetricReader *reader, bool populate_target_info);
explicit PrometheusCollector(sdk::metrics::MetricReader *reader,
bool populate_target_info,
bool populate_otel_scope);

/**
* Collects all metrics data from metricsToCollect collection.
Expand All @@ -43,6 +45,7 @@ class PrometheusCollector : public prometheus_client::Collectable
private:
sdk::metrics::MetricReader *reader_;
bool populate_target_info_;
bool populate_otel_scope_;

/*
* Lock when operating the metricsToCollect collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ struct PrometheusExporterOptions

// Populating target_info
bool populate_target_info = true;

// Populating otel_scope_name/otel_scope_labels attributes
bool populate_otel_scope = true;
};

} // namespace metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ class PrometheusExporterUtils
*
* @param records a collection of metrics in OpenTelemetry
* @param populate_target_info whether to populate target_info
* @param populate_otel_scope whether to populate otel_scope_name and otel_scope_version
* attributes
* @return a collection of translated metrics that is acceptable by Prometheus
*/
static std::vector<::prometheus::MetricFamily> TranslateToPrometheus(
const sdk::metrics::ResourceMetrics &data,
bool populate_target_info = true);
bool populate_target_info = true,
bool populate_otel_scope = true);

private:
/**
Expand Down
11 changes: 7 additions & 4 deletions exporters/prometheus/src/collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ namespace metrics
* in this class with default capacity
*/
PrometheusCollector::PrometheusCollector(sdk::metrics::MetricReader *reader,
bool populate_target_info)
: reader_(reader), populate_target_info_(populate_target_info)
bool populate_target_info,
bool populate_otel_scope)
: reader_(reader),
populate_target_info_(populate_target_info),
populate_otel_scope_(populate_otel_scope)
{}

/**
Expand All @@ -41,8 +44,8 @@ std::vector<prometheus_client::MetricFamily> PrometheusCollector::Collect() cons
std::vector<prometheus_client::MetricFamily> result;

reader_->Collect([&result, this](sdk::metrics::ResourceMetrics &metric_data) {
auto prometheus_metric_data =
PrometheusExporterUtils::TranslateToPrometheus(metric_data, this->populate_target_info_);
auto prometheus_metric_data = PrometheusExporterUtils::TranslateToPrometheus(
metric_data, this->populate_target_info_, this->populate_otel_scope_);
for (auto &data : prometheus_metric_data)
result.emplace_back(data);
return true;
Expand Down
2 changes: 1 addition & 1 deletion exporters/prometheus/src/exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ PrometheusExporter::PrometheusExporter(const PrometheusExporterOptions &options)
return;
}
collector_ = std::shared_ptr<PrometheusCollector>(
new PrometheusCollector(this, options_.populate_target_info));
new PrometheusCollector(this, options_.populate_target_info, options_.populate_otel_scope));

exposer_->RegisterCollectable(collector_);
}
Expand Down
30 changes: 29 additions & 1 deletion exporters/prometheus/src/exporter_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,35 @@ inline const std::string GetPrometheusDefaultHttpEndpoint()
return exists ? endpoint : kPrometheusEndpointDefault;
}

PrometheusExporterOptions::PrometheusExporterOptions() : url(GetPrometheusDefaultHttpEndpoint()) {}
inline bool GetPrometheusPopulateOtelScope()
{
constexpr char kPrometheusPopulateOtelScope[] =
"OTEL_CPP_PROMETHEUS_EXPORTER_POPULATE_OTEL_SCOPE";

bool setting;
auto exists =
opentelemetry::sdk::common::GetBoolEnvironmentVariable(kPrometheusPopulateOtelScope, setting);

return exists ? setting : true;
}

inline bool GetPrometheusPopulateTargetInfo()
{
constexpr char kPrometheusPopulateTargetInfo[] =
"OTEL_CPP_PROMETHEUS_EXPORTER_POPULATE_TARGET_INFO";

bool setting;
auto exists = opentelemetry::sdk::common::GetBoolEnvironmentVariable(
kPrometheusPopulateTargetInfo, setting);

return exists ? setting : true;
}

PrometheusExporterOptions::PrometheusExporterOptions()
: url(GetPrometheusDefaultHttpEndpoint()),
populate_target_info(GetPrometheusPopulateTargetInfo()),
populate_otel_scope(GetPrometheusPopulateOtelScope())
{}

} // namespace metrics
} // namespace exporter
Expand Down
23 changes: 13 additions & 10 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ std::string SanitizeLabel(std::string label_key)
*/
std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateToPrometheus(
const sdk::metrics::ResourceMetrics &data,
bool populate_target_info)
bool populate_target_info,
bool populate_otel_scope)
{

// initialize output vector
Expand All @@ -126,7 +127,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
{
SetTarget(data,
data.scope_metric_data_.begin()->metric_data_.begin()->end_ts.time_since_epoch(),
(*data.scope_metric_data_.begin()).scope_, &output);
populate_otel_scope ? (*data.scope_metric_data_.begin()).scope_ : nullptr, &output);
}

for (const auto &instrumentation_info : data.scope_metric_data_)
Expand All @@ -149,6 +150,9 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
metric_family.name = MapToPrometheusName(metric_data.instrument_descriptor.name_,
metric_data.instrument_descriptor.unit_, type);
metric_family.type = type;
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope =
populate_otel_scope ? instrumentation_info.scope_ : nullptr;

for (const auto &point_data_attr : metric_data.point_data_attr_)
{
if (type == prometheus_client::MetricType::Histogram) // Histogram
Expand All @@ -167,8 +171,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
sum = static_cast<double>(nostd::get<int64_t>(histogram_point_data.sum_));
}
SetData(std::vector<double>{sum, (double)histogram_point_data.count_}, boundaries, counts,
point_data_attr.attributes, instrumentation_info.scope_, time, &metric_family,
data.resource_);
point_data_attr.attributes, scope, time, &metric_family, data.resource_);
}
else if (type == prometheus_client::MetricType::Gauge)
{
Expand All @@ -178,16 +181,16 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
auto last_value_point_data =
nostd::get<sdk::metrics::LastValuePointData>(point_data_attr.point_data);
std::vector<metric_sdk::ValueType> values{last_value_point_data.value_};
SetData(values, point_data_attr.attributes, instrumentation_info.scope_, type, time,
&metric_family, data.resource_);
SetData(values, point_data_attr.attributes, scope, type, time, &metric_family,
data.resource_);
}
else if (nostd::holds_alternative<sdk::metrics::SumPointData>(point_data_attr.point_data))
{
auto sum_point_data =
nostd::get<sdk::metrics::SumPointData>(point_data_attr.point_data);
std::vector<metric_sdk::ValueType> values{sum_point_data.value_};
SetData(values, point_data_attr.attributes, instrumentation_info.scope_, type, time,
&metric_family, data.resource_);
SetData(values, point_data_attr.attributes, scope, type, time, &metric_family,
data.resource_);
}
else
{
Expand All @@ -203,8 +206,8 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
auto sum_point_data =
nostd::get<sdk::metrics::SumPointData>(point_data_attr.point_data);
std::vector<metric_sdk::ValueType> values{sum_point_data.value_};
SetData(values, point_data_attr.attributes, instrumentation_info.scope_, type, time,
&metric_family, data.resource_);
SetData(values, point_data_attr.attributes, scope, type, time, &metric_family,
data.resource_);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion exporters/prometheus/test/collector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ TEST(PrometheusCollector, BasicTests)
MockMetricReader *reader = new MockMetricReader();
MockMetricProducer *producer = new MockMetricProducer();
reader->SetMetricProducer(producer);
PrometheusCollector collector(reader, true);
PrometheusCollector collector(reader, true, true);
auto data = collector.Collect();

// Collection size should be the same as the size
Expand Down

0 comments on commit e8afbb8

Please sign in to comment.