Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit a0e7708

Browse files
author
Ian Sturdy
authored
Update the Prometheus exporter to the new API. (#156)
1 parent fdf0f30 commit a0e7708

File tree

7 files changed

+464
-185
lines changed

7 files changed

+464
-185
lines changed

WORKSPACE

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,15 @@ cc_library(
8484
"""
8585
)
8686

87-
# TODO: Remove protobuf dependency and reset to head.
8887
http_archive(
8988
name = "com_github_jupp0r_prometheus_cpp",
90-
strip_prefix = "prometheus-cpp-a97127c9aeec8decb76cf3bec1fb9c9a0dd4a9fc",
91-
urls = ["https://github.com/jupp0r/prometheus-cpp/archive/a97127c9aeec8decb76cf3bec1fb9c9a0dd4a9fc.zip"],
89+
strip_prefix = "prometheus-cpp-master",
90+
urls = ["https://github.com/jupp0r/prometheus-cpp/archive/master.zip"],
9291
)
9392

94-
load("@com_github_jupp0r_prometheus_cpp//:repositories.bzl",
95-
"load_prometheus_client_model",
96-
"load_civetweb")
93+
load("@com_github_jupp0r_prometheus_cpp//:repositories.bzl", "load_civetweb")
9794

9895
# Load dependencies individually since we load some of them above.
99-
load_prometheus_client_model()
10096
load_civetweb()
10197

10298
# Curl library - used by zipkin exporter.

opencensus/exporters/stats/prometheus/BUILD

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ cc_library(
2828
":prometheus_utils",
2929
"//opencensus/stats",
3030
"@com_github_jupp0r_prometheus_cpp//:prometheus_cpp",
31-
"@prometheus_client_model",
3231
],
3332
)
3433

@@ -42,9 +41,9 @@ cc_library(
4241
copts = DEFAULT_COPTS,
4342
deps = [
4443
"//opencensus/stats",
44+
"@com_github_jupp0r_prometheus_cpp//:prometheus_cpp",
4545
"@com_google_absl//absl/strings",
4646
"@com_google_absl//absl/time",
47-
"@prometheus_client_model",
4847
],
4948
)
5049

@@ -61,8 +60,6 @@ cc_test(
6160
"//opencensus/stats:test_utils",
6261
"@com_google_absl//absl/strings",
6362
"@com_google_googletest//:gtest_main",
64-
"@com_google_protobuf//:protobuf",
65-
"@prometheus_client_model",
6663
],
6764
)
6865

opencensus/exporters/stats/prometheus/internal/prometheus_exporter.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717
#include <utility>
1818
#include <vector>
1919

20-
#include "metrics.pb.h"
2120
#include "opencensus/exporters/stats/prometheus/internal/prometheus_utils.h"
2221
#include "opencensus/stats/stats.h"
22+
#include "prometheus/metric_family.h"
2323

2424
namespace opencensus {
2525
namespace exporters {
2626
namespace stats {
2727

28-
std::vector<io::prometheus::client::MetricFamily>
29-
PrometheusExporter::Collect() {
28+
std::vector<prometheus::MetricFamily> PrometheusExporter::Collect() {
3029
const auto data = opencensus::stats::StatsExporter::GetViewData();
31-
std::vector<io::prometheus::client::MetricFamily> output(data.size());
30+
std::vector<prometheus::MetricFamily> output(data.size());
3231
for (int i = 0; i < data.size(); ++i) {
3332
SetMetricFamily(data[i].first, data[i].second, &output[i]);
3433
}

opencensus/exporters/stats/prometheus/internal/prometheus_utils.cc

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <cctype>
1818
#include <cstdint>
19+
#include <limits>
1920
#include <string>
2021
#include <utility>
2122
#include <vector>
@@ -24,8 +25,9 @@
2425
#include "absl/strings/str_cat.h"
2526
#include "absl/strings/string_view.h"
2627
#include "absl/time/time.h"
27-
#include "metrics.pb.h"
2828
#include "opencensus/stats/stats.h"
29+
#include "prometheus/metric.h"
30+
#include "prometheus/metric_type.h"
2931

3032
namespace opencensus {
3133
namespace exporters {
@@ -42,99 +44,100 @@ std::string SanitizeName(absl::string_view name) {
4244
return sanitized;
4345
}
4446

45-
io::prometheus::client::MetricType MetricType(
46-
opencensus::stats::Aggregation::Type type) {
47+
prometheus::MetricType MetricType(opencensus::stats::Aggregation::Type type) {
4748
switch (type) {
4849
case opencensus::stats::Aggregation::Type::kCount:
49-
return io::prometheus::client::MetricType::COUNTER;
50+
return prometheus::MetricType::Counter;
5051
case opencensus::stats::Aggregation::Type::kSum:
51-
return io::prometheus::client::MetricType::UNTYPED;
52+
return prometheus::MetricType::Untyped;
5253
case opencensus::stats::Aggregation::Type::kLastValue:
53-
return io::prometheus::client::MetricType::GAUGE;
54+
return prometheus::MetricType::Gauge;
5455
case opencensus::stats::Aggregation::Type::kDistribution:
55-
return io::prometheus::client::MetricType::HISTOGRAM;
56+
return prometheus::MetricType::Histogram;
5657
}
5758
}
5859

59-
void SetValue(double value, io::prometheus::client::MetricType type,
60-
io::prometheus::client::Metric* metric) {
61-
if (type == io::prometheus::client::MetricType::UNTYPED) {
62-
metric->mutable_untyped()->set_value(value);
60+
void SetValue(double value, prometheus::MetricType type,
61+
prometheus::ClientMetric* metric) {
62+
if (type == prometheus::MetricType::Untyped) {
63+
metric->untyped.value = value;
6364
} else {
64-
ABSL_ASSERT(type == io::prometheus::client::MetricType::GAUGE);
65-
metric->mutable_gauge()->set_value(value);
65+
ABSL_ASSERT(type == prometheus::MetricType::Gauge);
66+
metric->gauge.value = value;
6667
}
6768
}
6869

69-
void SetValue(int64_t value, io::prometheus::client::MetricType type,
70-
io::prometheus::client::Metric* metric) {
70+
void SetValue(int64_t value, prometheus::MetricType type,
71+
prometheus::ClientMetric* metric) {
7172
switch (type) {
72-
case io::prometheus::client::MetricType::COUNTER: {
73-
metric->mutable_counter()->set_value(value);
73+
case prometheus::MetricType::Counter: {
74+
metric->counter.value = value;
7475
break;
7576
}
76-
case io::prometheus::client::MetricType::GAUGE: {
77-
metric->mutable_gauge()->set_value(value);
77+
case prometheus::MetricType::Gauge: {
78+
metric->gauge.value = value;
7879
break;
7980
}
80-
case io::prometheus::client::MetricType::UNTYPED: {
81-
metric->mutable_untyped()->set_value(value);
81+
case prometheus::MetricType::Untyped: {
82+
metric->untyped.value = value;
8283
break;
8384
}
8485
default:
8586
ABSL_ASSERT(false && "Invalid MetricType for int64 value.");
8687
}
8788
}
8889
void SetValue(const opencensus::stats::Distribution& value,
89-
io::prometheus::client::MetricType type,
90-
io::prometheus::client::Metric* metric) {
91-
auto* histogram = metric->mutable_histogram();
92-
histogram->set_sample_count(value.count());
93-
histogram->set_sample_sum(value.count() * value.mean());
90+
prometheus::MetricType type, prometheus::ClientMetric* metric) {
91+
auto& histogram = metric->histogram;
92+
histogram.sample_count = value.count();
93+
histogram.sample_sum = value.count() * value.mean();
9494

9595
int64_t cumulative_count = 0;
96+
histogram.bucket.reserve(value.bucket_boundaries().num_buckets());
9697
for (int i = 0; i < value.bucket_boundaries().num_buckets(); ++i) {
9798
cumulative_count += value.bucket_counts()[i];
98-
auto* bucket = histogram->add_bucket();
99-
bucket->set_cumulative_count(cumulative_count);
99+
histogram.bucket.emplace_back();
100+
histogram.bucket[i].cumulative_count = cumulative_count;
100101
// We use lower boundaries plus an underflow bucket; Prometheus uses upper
101102
// boundaries, including a +Inf boundary.
102-
bucket->set_upper_bound(
103+
histogram.bucket[i].upper_bound =
103104
i < value.bucket_boundaries().lower_boundaries().size()
104105
? value.bucket_boundaries().lower_boundaries()[i]
105-
: std::numeric_limits<double>::infinity());
106+
: std::numeric_limits<double>::infinity();
106107
}
107108
}
108109

109110
template <typename T>
110111
void SetData(const opencensus::stats::ViewDescriptor& descriptor,
111112
const opencensus::stats::ViewData::DataMap<T>& data, int64_t time,
112-
io::prometheus::client::MetricType type,
113-
io::prometheus::client::MetricFamily* metric_family) {
113+
prometheus::MetricType type,
114+
prometheus::MetricFamily* metric_family) {
115+
metric_family->metric.reserve(data.size());
114116
for (const auto& row : data) {
115-
auto* metric = metric_family->add_metric();
116-
metric->set_timestamp_ms(time);
117+
metric_family->metric.emplace_back();
118+
prometheus::ClientMetric& metric = metric_family->metric.back();
119+
metric.timestamp_ms = time;
120+
metric.label.resize(descriptor.num_columns());
117121
for (int i = 0; i < descriptor.num_columns(); ++i) {
118-
auto* label = metric->add_label();
119-
label->set_name(SanitizeName(descriptor.columns()[i].name()));
120-
label->set_value(row.first[i]);
122+
metric.label[i].name = SanitizeName(descriptor.columns()[i].name());
123+
metric.label[i].value = row.first[i];
121124
}
122-
SetValue(row.second, type, metric);
125+
SetValue(row.second, type, &metric);
123126
}
124127
}
125128

126129
} // namespace
127130

128131
void SetMetricFamily(const opencensus::stats::ViewDescriptor& descriptor,
129132
const opencensus::stats::ViewData& data,
130-
io::prometheus::client::MetricFamily* metric_family) {
131-
const io::prometheus::client::MetricType type =
133+
prometheus::MetricFamily* metric_family) {
134+
const prometheus::MetricType type =
132135
MetricType(descriptor.aggregation().type());
133136
// TODO(sturdy): convert common units into base units (e.g. ms->s).
134-
metric_family->set_name(SanitizeName(absl::StrCat(
135-
descriptor.name(), "_", descriptor.measure_descriptor().units())));
136-
metric_family->set_help(descriptor.description());
137-
metric_family->set_type(type);
137+
metric_family->name = SanitizeName(absl::StrCat(
138+
descriptor.name(), "_", descriptor.measure_descriptor().units()));
139+
metric_family->help = descriptor.description();
140+
metric_family->type = type;
138141

139142
const int64_t time = absl::ToUnixMillis(data.end_time());
140143
switch (data.type()) {

opencensus/exporters/stats/prometheus/internal/prometheus_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include <utility>
1919
#include <vector>
2020

21-
#include "metrics.pb.h"
2221
#include "opencensus/stats/stats.h"
22+
#include "prometheus/metric_family.h"
2323

2424
namespace opencensus {
2525
namespace exporters {
@@ -28,7 +28,7 @@ namespace stats {
2828
// Populates metric_family based on view_descriptor and view_data.
2929
void SetMetricFamily(const opencensus::stats::ViewDescriptor& descriptor,
3030
const opencensus::stats::ViewData& data,
31-
io::prometheus::client::MetricFamily* metric_family);
31+
prometheus::MetricFamily* metric_family);
3232

3333
} // namespace stats
3434
} // namespace exporters

0 commit comments

Comments
 (0)