Skip to content

Commit

Permalink
Merge branch 'open-telemetry:main' into Metrics_DLL
Browse files Browse the repository at this point in the history
  • Loading branch information
perhapsmaple authored Oct 7, 2023
2 parents 606aebb + 0eaa794 commit 80aaa15
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 61 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Increment the:

* [DEPRECATION] Deprecate ZPAGES
[#2291](https://github.com/open-telemetry/opentelemetry-cpp/pull/2291)
* [EXPORTER] Prometheus exporter emit resource attributes
[#2301](https://github.com/open-telemetry/opentelemetry-cpp/pull/2301)
* [EXPORTER] Remove explicit timestamps from metric points exported by Prometheus
[#2324](https://github.com/open-telemetry/opentelemetry-cpp/pull/2324)
* [EXPORTER] Handle attribute key collisions caused by sanitation
Expand All @@ -27,6 +29,9 @@ Increment the:
[#2224](https://github.com/open-telemetry/opentelemetry-cpp/pull/2224)
* [REMOVAL] Drop C++11 support
[#2342](https://github.com/open-telemetry/opentelemetry-cpp/pull/2342)
* [EXPORTER] Add otel_scope_name and otel_scope_version labels to the prometheus
exporter.
[#2293](https://github.com/open-telemetry/opentelemetry-cpp/pull/2293)

Important changes:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ 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);
explicit PrometheusCollector(sdk::metrics::MetricReader *reader, bool populate_target_info);

/**
* Collects all metrics data from metricsToCollect collection.
Expand All @@ -42,6 +42,7 @@ class PrometheusCollector : public prometheus_client::Collectable

private:
sdk::metrics::MetricReader *reader_;
bool populate_target_info_;

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

// The endpoint the Prometheus backend can collect metrics from
std::string url;

// Populating target_info
bool populate_target_info = true;
};

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

private:
/**
* Append key-value pair to prometheus labels.
*
* @param name label name
* @param value label value
* @param labels target labels
*/
static void AddPrometheusLabel(std::string name,
std::string value,
std::vector<::prometheus::ClientMetric::Label> *labels);

static opentelemetry::sdk::metrics::AggregationType getAggregationType(
const opentelemetry::sdk::metrics::PointType &point_type);

Expand All @@ -41,15 +54,24 @@ class PrometheusExporterUtils
static ::prometheus::MetricType TranslateType(opentelemetry::sdk::metrics::AggregationType kind,
bool is_monotonic = true);

/**
* Add a target_info metric to collect resource attributes
*/
static void SetTarget(const sdk::metrics::ResourceMetrics &data,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
std::vector<::prometheus::MetricFamily> *output);

/**
* Set metric data for:
* Counter => Prometheus Counter
*/
template <typename T>
static void SetData(std::vector<T> values,
const opentelemetry::sdk::metrics::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
::prometheus::MetricType type,
::prometheus::MetricFamily *metric_family);
::prometheus::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource);

/**
* Set metric data for:
Expand All @@ -60,13 +82,18 @@ class PrometheusExporterUtils
const std::vector<double> &boundaries,
const std::vector<uint64_t> &counts,
const opentelemetry::sdk::metrics::PointAttributes &labels,
::prometheus::MetricFamily *metric_family);
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
::prometheus::MetricFamily *metric_family,
const opentelemetry::sdk::resource::Resource *resource);

/**
* Set time and labels to metric data
*/
static void SetMetricBasic(::prometheus::ClientMetric &metric,
const opentelemetry::sdk::metrics::PointAttributes &labels);
static void SetMetricBasic(
::prometheus::ClientMetric &metric,
const opentelemetry::sdk::metrics::PointAttributes &labels,
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
const opentelemetry::sdk::resource::Resource *resource);

/**
* Convert attribute value to string
Expand Down
11 changes: 8 additions & 3 deletions exporters/prometheus/src/collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ namespace metrics
* This constructor initializes the collection for metrics to export
* in this class with default capacity
*/
PrometheusCollector::PrometheusCollector(sdk::metrics::MetricReader *reader) : reader_(reader) {}
PrometheusCollector::PrometheusCollector(sdk::metrics::MetricReader *reader,
bool populate_target_info)
: reader_(reader), populate_target_info_(populate_target_info)
{}

/**
* Collects all metrics data from metricsToCollect collection.
Expand All @@ -36,8 +39,10 @@ std::vector<prometheus_client::MetricFamily> PrometheusCollector::Collect() cons
collection_lock_.lock();

std::vector<prometheus_client::MetricFamily> result;
reader_->Collect([&result](sdk::metrics::ResourceMetrics &metric_data) {
auto prometheus_metric_data = PrometheusExporterUtils::TranslateToPrometheus(metric_data);

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

exposer_->RegisterCollectable(collector_);
}
Expand Down
Loading

0 comments on commit 80aaa15

Please sign in to comment.