Skip to content

Commit

Permalink
Use Prometheus populate_otel_scope option to disable associated attri…
Browse files Browse the repository at this point in the history
…butes
  • Loading branch information
timwoj committed Dec 16, 2023
1 parent 57fbe5a commit 5a69527
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
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
4 changes: 2 additions & 2 deletions exporters/prometheus/src/collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,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
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

0 comments on commit 5a69527

Please sign in to comment.