1616
1717#include < cctype>
1818#include < cstdint>
19+ #include < limits>
1920#include < string>
2021#include < utility>
2122#include < vector>
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
3032namespace opencensus {
3133namespace 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}
8889void 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
109110template <typename T>
110111void 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
128131void 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 ()) {
0 commit comments