Skip to content

Commit

Permalink
Add Prometheus option to disable otel_scope attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
timwoj committed Dec 13, 2023
1 parent 96e5078 commit faa1e7b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 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
7 changes: 5 additions & 2 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 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
15 changes: 14 additions & 1 deletion exporters/prometheus/src/exporter_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,20 @@ inline const std::string GetPrometheusDefaultHttpEndpoint()
return exists ? endpoint : kPrometheusEndpointDefault;
}

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

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

return exists ? setting : true;
}

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

} // namespace metrics
} // namespace exporter
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 faa1e7b

Please sign in to comment.