7
7
#include < string>
8
8
#include < vector>
9
9
#include " opentelemetry/metrics/provider.h"
10
+ #include " opentelemetry/nostd/string_view.h"
10
11
#include " opentelemetry/sdk/metrics/meter.h"
11
12
#include " opentelemetry/version.h"
12
13
@@ -35,7 +36,17 @@ class PrometheusExporterUtils
35
36
36
37
private:
37
38
/* *
38
- * Append key-value pair to prometheus labels.
39
+ * Sanitize the given metric name or label according to Prometheus rule.
40
+ *
41
+ * This function is needed because names in OpenTelemetry can contain
42
+ * alphanumeric characters, '_', '.', and '-', whereas in Prometheus the
43
+ * name should only contain alphanumeric characters and '_'.
44
+ * @param name name
45
+ */
46
+ static std::string SanitizeNames (std::string name);
47
+
48
+ /* *
49
+ * Sanitize the given metric name or label according to Prometheus rule.
39
50
*
40
51
* @param name label name
41
52
* @param value label value
@@ -45,6 +56,79 @@ class PrometheusExporterUtils
45
56
std::string value,
46
57
std::vector<::prometheus::ClientMetric::Label> *labels);
47
58
59
+ static std::string MapToPrometheusName (const std::string &name,
60
+ const std::string &unit,
61
+ ::prometheus::MetricType prometheus_type);
62
+
63
+ /* *
64
+ * A utility function that returns the equivalent Prometheus name for the provided OTLP metric
65
+ * unit.
66
+ *
67
+ * @param raw_metric_unitName The raw metric unit for which Prometheus metric unit needs to be
68
+ * computed.
69
+ * @return the computed Prometheus metric unit equivalent of the OTLP metric unit
70
+ */
71
+ static std::string GetEquivalentPrometheusUnit (const std::string &raw_metric_unitName);
72
+
73
+ /* *
74
+ * This method retrieves the expanded Prometheus unit name for known abbreviations. OTLP metrics
75
+ * use the c/s notation as specified at <a href="https://ucum.org/ucum.html">UCUM</a>. The list of
76
+ * mappings is adopted from <a
77
+ * href="https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/9a9d4778bbbf242dba233db28e2fbcfda3416959/pkg/translator/prometheus/normalize_name.go#L30">OpenTelemetry
78
+ * Collector Contrib</a>.
79
+ *
80
+ * @param unit_abbreviation The unit that name that needs to be expanded/converted to Prometheus
81
+ * units.
82
+ * @return The expanded/converted unit name if known, otherwise returns the input unit name as-is.
83
+ */
84
+ static std::string GetPrometheusUnit (const std::string &unit_abbreviation);
85
+
86
+ /* *
87
+ * This method retrieves the expanded Prometheus unit name to be used with "per" units for known
88
+ * units. For example: s => per second (singular)
89
+ *
90
+ * @param per_unit_abbreviation The unit abbreviation used in a 'per' unit.
91
+ * @return The expanded unit equivalent to be used in 'per' unit if the input is a known unit,
92
+ * otherwise returns the input as-is.
93
+ */
94
+ static std::string GetPrometheusPerUnit (const std::string &per_unit_abbreviation);
95
+
96
+ /* *
97
+ * Replaces all characters that are not a letter or a digit with '_' to make the resulting string
98
+ * Prometheus compliant. This method also removes leading and trailing underscores - this is done
99
+ * to keep the resulting unit similar to what is produced from the collector's implementation.
100
+ *
101
+ * @param str The string input that needs to be made Prometheus compliant.
102
+ * @return the cleaned-up Prometheus compliant string.
103
+ */
104
+ static std::string CleanUpString (const std::string &str);
105
+
106
+ /* *
107
+ * This method is used to convert the units expressed as a rate via '/' symbol in their name to
108
+ * their expanded text equivalent. For instance, km/h => km_per_hour. The method operates on the
109
+ * input by splitting it in 2 parts - before and after '/' symbol and will attempt to expand any
110
+ * known unit abbreviation in both parts. Unknown abbreviations & unsupported characters will
111
+ * remain unchanged in the final output of this function.
112
+ *
113
+ * @param rate_expressed_unit The rate unit input that needs to be converted to its text
114
+ * equivalent.
115
+ * @return The text equivalent of unit expressed as rate. If the input does not contain '/', the
116
+ * function returns it as-is.
117
+ */
118
+ static std::string ConvertRateExpressedToPrometheusUnit (const std::string &rate_expressed_unit);
119
+
120
+ /* *
121
+ * This method drops all characters enclosed within '{}' (including the curly braces) by replacing
122
+ * them with an empty string. Note that this method will not produce the intended effect if there
123
+ * are nested curly braces within the outer enclosure of '{}'.
124
+ *
125
+ * <p>For instance, {packet{s}s} => s}.
126
+ *
127
+ * @param unit The input unit from which text within curly braces needs to be removed.
128
+ * @return The resulting unit after removing the text within '{}'.
129
+ */
130
+ static std::string RemoveUnitPortionInBraces (const std::string &unit);
131
+
48
132
static opentelemetry::sdk::metrics::AggregationType getAggregationType (
49
133
const opentelemetry::sdk::metrics::PointType &point_type);
50
134
@@ -58,6 +142,7 @@ class PrometheusExporterUtils
58
142
* Add a target_info metric to collect resource attributes
59
143
*/
60
144
static void SetTarget (const sdk::metrics::ResourceMetrics &data,
145
+ std::chrono::nanoseconds time,
61
146
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
62
147
std::vector<::prometheus::MetricFamily> *output);
63
148
@@ -70,6 +155,7 @@ class PrometheusExporterUtils
70
155
const opentelemetry::sdk::metrics::PointAttributes &labels,
71
156
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
72
157
::prometheus::MetricType type,
158
+ std::chrono::nanoseconds time,
73
159
::prometheus::MetricFamily *metric_family,
74
160
const opentelemetry::sdk::resource::Resource *resource);
75
161
@@ -83,6 +169,7 @@ class PrometheusExporterUtils
83
169
const std::vector<uint64_t > &counts,
84
170
const opentelemetry::sdk::metrics::PointAttributes &labels,
85
171
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
172
+ std::chrono::nanoseconds time,
86
173
::prometheus::MetricFamily *metric_family,
87
174
const opentelemetry::sdk::resource::Resource *resource);
88
175
@@ -92,6 +179,7 @@ class PrometheusExporterUtils
92
179
static void SetMetricBasic (
93
180
::prometheus::ClientMetric &metric,
94
181
const opentelemetry::sdk::metrics::PointAttributes &labels,
182
+ std::chrono::nanoseconds time,
95
183
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope,
96
184
const opentelemetry::sdk::resource::Resource *resource);
97
185
@@ -122,6 +210,9 @@ class PrometheusExporterUtils
122
210
const std::vector<double > &boundaries,
123
211
const std::vector<uint64_t > &counts,
124
212
::prometheus::ClientMetric *metric);
213
+
214
+ // For testing
215
+ friend class SanitizeNameTester ;
125
216
};
126
217
} // namespace metrics
127
218
} // namespace exporter
0 commit comments