From e0cc1b59f9a2b77f0a9ea7c652996060a8fb902c Mon Sep 17 00:00:00 2001 From: Leonardo Parente Date: Tue, 10 Jan 2023 16:17:04 -0400 Subject: [PATCH] Implement OTLP Histogram --- src/Metrics.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Metrics.h b/src/Metrics.h index 1212ec1ad..ea66beee2 100644 --- a/src/Metrics.h +++ b/src/Metrics.h @@ -296,13 +296,33 @@ class Histogram final : public Metric void to_opentelemetry(metrics::v1::ScopeMetrics &scope, timespec &start, timespec &end, LabelMap add_labels = {}) const { + if (_sketch.is_empty()) { + return; + } + auto bins_pmf = _get_boundaries(); + auto histogram_pmf = _sketch.get_PMF(bins_pmf.first.data(), bins_pmf.second); + std::vector bins; + for (size_t i = 0; i < bins_pmf.second; ++i) { + if (histogram_pmf[i]) { + bins.push_back(bins_pmf.first[i]); + } + } + auto histogram = _sketch.get_CDF(bins.data(), bins.size()); + auto pace = _get_pace(); + auto metric = scope.add_metrics(); metric->set_name(base_name_snake()); metric->set_description(_desc); auto hist_data_point = metric->mutable_histogram()->add_data_points(); hist_data_point->set_start_time_unix_nano(timespec_to_uint64(start)); hist_data_point->set_time_unix_nano(timespec_to_uint64(end)); - // Implement histogram + + for (std::size_t i = 0; i < bins.size(); ++i) { + hist_data_point->add_explicit_bounds(bins[i] - pace); + hist_data_point->add_bucket_counts(histogram[i] * _sketch.get_n()); + } + hist_data_point->set_count(_sketch.get_n()); + for (const auto &label : add_labels) { auto attribute = hist_data_point->add_attributes(); attribute->set_key(label.first);