@@ -38,24 +38,37 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
38
38
{
39
39
for (const auto &metric_data : instrumentation_info.metric_data_ )
40
40
{
41
- auto origin_name = metric_data.instrument_descriptor .name_ ;
42
- auto unit = metric_data.instrument_descriptor .unit_ ;
43
- auto sanitized = SanitizeNames (origin_name);
41
+ if (metric_data.point_data_attr_ .empty ())
42
+ {
43
+ continue ;
44
+ }
44
45
prometheus_client::MetricFamily metric_family;
46
+ auto front = metric_data.point_data_attr_ .front ();
47
+ auto kind = getAggregationType (front.point_data );
48
+ bool is_monotonic = true ;
49
+ if (kind == sdk::metrics::AggregationType::kSum )
50
+ {
51
+ is_monotonic = nostd::get<sdk::metrics::SumPointData>(front.point_data ).is_monotonic_ ;
52
+ }
53
+ auto type = TranslateType (kind, is_monotonic);
54
+ metric_family.type = type;
55
+ auto origin_name = metric_data.instrument_descriptor .name_ ;
56
+ auto unit = metric_data.instrument_descriptor .unit_ ;
57
+ auto sanitized = SanitizeNames (origin_name);
58
+ if (type == prometheus_client::MetricType::Counter && endsWith (sanitized, " _total" ))
59
+ {
60
+ // trim _total from counters, since it will be appended after the unit.
61
+ sanitized = sanitized.substr (0 , sanitized.length () - sizeof (" _total" ));
62
+ }
45
63
metric_family.name = sanitized + " _" + unit;
64
+ if (type == prometheus_client::MetricType::Counter)
65
+ {
66
+ metric_family.name += " _total" ;
67
+ }
46
68
metric_family.help = metric_data.instrument_descriptor .description_ ;
47
69
auto time = metric_data.end_ts .time_since_epoch ();
48
70
for (const auto &point_data_attr : metric_data.point_data_attr_ )
49
71
{
50
- auto kind = getAggregationType (point_data_attr.point_data );
51
- bool is_monotonic = true ;
52
- if (kind == sdk::metrics::AggregationType::kSum )
53
- {
54
- is_monotonic =
55
- nostd::get<sdk::metrics::SumPointData>(point_data_attr.point_data ).is_monotonic_ ;
56
- }
57
- const prometheus_client::MetricType type = TranslateType (kind, is_monotonic);
58
- metric_family.type = type;
59
72
if (type == prometheus_client::MetricType::Histogram) // Histogram
60
73
{
61
74
auto histogram_point_data =
@@ -121,6 +134,13 @@ std::vector<prometheus_client::MetricFamily> PrometheusExporterUtils::TranslateT
121
134
return output;
122
135
}
123
136
137
+ inline bool PrometheusExporterUtils::endsWith (std::string const &value, std::string const &ending)
138
+ {
139
+ if (ending.size () > value.size ())
140
+ return false ;
141
+ return std::equal (ending.rbegin (), ending.rend (), value.rbegin ());
142
+ }
143
+
124
144
/* *
125
145
* Sanitize the given metric name or label according to Prometheus rule.
126
146
*
0 commit comments