]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | ||
6 | #ifndef ENABLE_METRICS_PREVIEW | |
7 | ||
8 | # include <prometheus/metric_family.h> | |
9 | # include <string> | |
10 | # include <vector> | |
11 | # include "opentelemetry/metrics/provider.h" | |
12 | # include "opentelemetry/sdk/metrics/meter.h" | |
13 | # include "opentelemetry/version.h" | |
14 | ||
15 | OPENTELEMETRY_BEGIN_NAMESPACE | |
16 | namespace exporter | |
17 | { | |
18 | namespace metrics | |
19 | { | |
20 | /** | |
21 | * The Prometheus Utils contains utility functions for Prometheus Exporter | |
22 | */ | |
23 | class PrometheusExporterUtils | |
24 | { | |
25 | public: | |
26 | /** | |
27 | * Helper function to convert OpenTelemetry metrics data collection | |
28 | * to Prometheus metrics data collection | |
29 | * | |
30 | * @param records a collection of metrics in OpenTelemetry | |
31 | * @return a collection of translated metrics that is acceptable by Prometheus | |
32 | */ | |
33 | static std::vector<::prometheus::MetricFamily> TranslateToPrometheus( | |
34 | const std::vector<std::unique_ptr<sdk::metrics::ResourceMetrics>> &data); | |
35 | ||
36 | private: | |
37 | /** | |
38 | * Sanitize the given metric name or label according to Prometheus rule. | |
39 | * | |
40 | * This function is needed because names in OpenTelemetry can contain | |
41 | * alphanumeric characters, '_', '.', and '-', whereas in Prometheus the | |
42 | * name should only contain alphanumeric characters and '_'. | |
43 | */ | |
44 | static std::string SanitizeNames(std::string name); | |
45 | ||
46 | static opentelemetry::sdk::metrics::AggregationType getAggregationType( | |
47 | const opentelemetry::sdk::metrics::PointType &point_type); | |
48 | ||
49 | /** | |
50 | * Translate the OTel metric type to Prometheus metric type | |
51 | */ | |
52 | static ::prometheus::MetricType TranslateType(opentelemetry::sdk::metrics::AggregationType kind); | |
53 | ||
54 | /** | |
55 | * Set metric data for: | |
56 | * Counter => Prometheus Counter | |
57 | */ | |
58 | template <typename T> | |
59 | static void SetData(std::vector<T> values, | |
60 | const opentelemetry::sdk::metrics::PointAttributes &labels, | |
61 | ::prometheus::MetricType type, | |
62 | std::chrono::nanoseconds time, | |
63 | ::prometheus::MetricFamily *metric_family); | |
64 | ||
65 | /** | |
66 | * Set metric data for: | |
67 | * Histogram => Prometheus Histogram | |
68 | */ | |
69 | template <typename T> | |
70 | static void SetData(std::vector<T> values, | |
71 | const opentelemetry::sdk::metrics::ListType &boundaries, | |
72 | const std::vector<uint64_t> &counts, | |
73 | const opentelemetry::sdk::metrics::PointAttributes &labels, | |
74 | std::chrono::nanoseconds time, | |
75 | ::prometheus::MetricFamily *metric_family); | |
76 | ||
77 | /** | |
78 | * Set time and labels to metric data | |
79 | */ | |
80 | static void SetMetricBasic(::prometheus::ClientMetric &metric, | |
81 | std::chrono::nanoseconds time, | |
82 | const opentelemetry::sdk::metrics::PointAttributes &labels); | |
83 | ||
84 | /** | |
85 | * Convert attribute value to string | |
86 | */ | |
87 | static std::string AttributeValueToString( | |
88 | const opentelemetry::sdk::common::OwnedAttributeValue &value); | |
89 | ||
90 | /** | |
91 | * Handle Counter and Gauge. | |
92 | */ | |
93 | template <typename T> | |
94 | static void SetValue(std::vector<T> values, | |
95 | ::prometheus::MetricType type, | |
96 | ::prometheus::ClientMetric *metric); | |
97 | ||
98 | /** | |
99 | * Handle Gauge from MinMaxSumCount | |
100 | */ | |
101 | static void SetValue(double value, ::prometheus::ClientMetric *metric); | |
102 | ||
103 | /** | |
104 | * Handle Histogram | |
105 | */ | |
106 | template <typename T, typename U> | |
107 | static void SetValue(std::vector<T> values, | |
108 | const std::list<U> &boundaries, | |
109 | const std::vector<uint64_t> &counts, | |
110 | ::prometheus::ClientMetric *metric); | |
111 | }; | |
112 | } // namespace metrics | |
113 | } // namespace exporter | |
114 | OPENTELEMETRY_END_NAMESPACE | |
115 | #endif |