-
Notifications
You must be signed in to change notification settings - Fork 438
/
otlp_metric_utils.cc
308 lines (287 loc) · 11.5 KB
/
otlp_metric_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "opentelemetry/exporters/otlp/otlp_metric_utils.h"
#include "opentelemetry/exporters/otlp/otlp_populate_attribute_utils.h"
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{
namespace metric_sdk = opentelemetry::sdk::metrics;
proto::metrics::v1::AggregationTemporality OtlpMetricUtils::GetProtoAggregationTemporality(
const opentelemetry::sdk::metrics::AggregationTemporality &aggregation_temporality) noexcept
{
if (aggregation_temporality == opentelemetry::sdk::metrics::AggregationTemporality::kCumulative)
return proto::metrics::v1::AggregationTemporality::AGGREGATION_TEMPORALITY_CUMULATIVE;
else if (aggregation_temporality == opentelemetry::sdk::metrics::AggregationTemporality::kDelta)
return proto::metrics::v1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA;
else
return proto::metrics::v1::AggregationTemporality::AGGREGATION_TEMPORALITY_UNSPECIFIED;
}
metric_sdk::AggregationType OtlpMetricUtils::GetAggregationType(
const opentelemetry::sdk::metrics::MetricData &metric_data) noexcept
{
if (metric_data.point_data_attr_.size() == 0)
{
return metric_sdk::AggregationType::kDrop;
}
auto point_data_with_attributes = metric_data.point_data_attr_[0];
if (nostd::holds_alternative<sdk::metrics::SumPointData>(point_data_with_attributes.point_data))
{
return metric_sdk::AggregationType::kSum;
}
else if (nostd::holds_alternative<sdk::metrics::HistogramPointData>(
point_data_with_attributes.point_data))
{
return metric_sdk::AggregationType::kHistogram;
}
else if (nostd::holds_alternative<sdk::metrics::LastValuePointData>(
point_data_with_attributes.point_data))
{
return metric_sdk::AggregationType::kLastValue;
}
return metric_sdk::AggregationType::kDrop;
}
void OtlpMetricUtils::ConvertSumMetric(const metric_sdk::MetricData &metric_data,
proto::metrics::v1::Sum *const sum) noexcept
{
sum->set_aggregation_temporality(
GetProtoAggregationTemporality(metric_data.aggregation_temporality));
sum->set_is_monotonic(
(metric_data.instrument_descriptor.type_ == metric_sdk::InstrumentType::kCounter) ||
(metric_data.instrument_descriptor.type_ == metric_sdk::InstrumentType::kObservableCounter));
auto start_ts = metric_data.start_ts.time_since_epoch().count();
auto ts = metric_data.end_ts.time_since_epoch().count();
for (auto &point_data_with_attributes : metric_data.point_data_attr_)
{
proto::metrics::v1::NumberDataPoint *proto_sum_point_data = sum->add_data_points();
proto_sum_point_data->set_start_time_unix_nano(start_ts);
proto_sum_point_data->set_time_unix_nano(ts);
auto sum_data = nostd::get<sdk::metrics::SumPointData>(point_data_with_attributes.point_data);
if ((nostd::holds_alternative<int64_t>(sum_data.value_)))
{
proto_sum_point_data->set_as_int(nostd::get<int64_t>(sum_data.value_));
}
else
{
proto_sum_point_data->set_as_double(nostd::get<double>(sum_data.value_));
}
// set attributes
for (auto &kv_attr : point_data_with_attributes.attributes)
{
OtlpPopulateAttributeUtils::PopulateAttribute(proto_sum_point_data->add_attributes(),
kv_attr.first, kv_attr.second);
}
}
}
void OtlpMetricUtils::ConvertHistogramMetric(
const metric_sdk::MetricData &metric_data,
proto::metrics::v1::Histogram *const histogram) noexcept
{
histogram->set_aggregation_temporality(
GetProtoAggregationTemporality(metric_data.aggregation_temporality));
auto start_ts = metric_data.start_ts.time_since_epoch().count();
auto ts = metric_data.end_ts.time_since_epoch().count();
for (auto &point_data_with_attributes : metric_data.point_data_attr_)
{
proto::metrics::v1::HistogramDataPoint *proto_histogram_point_data =
histogram->add_data_points();
proto_histogram_point_data->set_start_time_unix_nano(start_ts);
proto_histogram_point_data->set_time_unix_nano(ts);
auto histogram_data =
nostd::get<sdk::metrics::HistogramPointData>(point_data_with_attributes.point_data);
// sum
if ((nostd::holds_alternative<int64_t>(histogram_data.sum_)))
{
// Use static_cast to avoid C4244 in MSVC
proto_histogram_point_data->set_sum(
static_cast<double>(nostd::get<int64_t>(histogram_data.sum_)));
}
else
{
proto_histogram_point_data->set_sum(nostd::get<double>(histogram_data.sum_));
}
// count
proto_histogram_point_data->set_count(histogram_data.count_);
if (histogram_data.record_min_max_)
{
if (nostd::holds_alternative<int64_t>(histogram_data.min_))
{
// Use static_cast to avoid C4244 in MSVC
proto_histogram_point_data->set_min(
static_cast<double>(nostd::get<int64_t>(histogram_data.min_)));
}
else
{
proto_histogram_point_data->set_min(nostd::get<double>(histogram_data.min_));
}
if (nostd::holds_alternative<int64_t>(histogram_data.max_))
{
// Use static_cast to avoid C4244 in MSVC
proto_histogram_point_data->set_max(
static_cast<double>(nostd::get<int64_t>(histogram_data.max_)));
}
else
{
proto_histogram_point_data->set_max(nostd::get<double>(histogram_data.max_));
}
}
// buckets
for (auto bound : histogram_data.boundaries_)
{
proto_histogram_point_data->add_explicit_bounds(bound);
}
// bucket counts
for (auto bucket_value : histogram_data.counts_)
{
proto_histogram_point_data->add_bucket_counts(bucket_value);
}
// attributes
for (auto &kv_attr : point_data_with_attributes.attributes)
{
OtlpPopulateAttributeUtils::PopulateAttribute(proto_histogram_point_data->add_attributes(),
kv_attr.first, kv_attr.second);
}
}
}
void OtlpMetricUtils::ConvertGaugeMetric(const opentelemetry::sdk::metrics::MetricData &metric_data,
proto::metrics::v1::Gauge *const gauge) noexcept
{
auto start_ts = metric_data.start_ts.time_since_epoch().count();
auto ts = metric_data.end_ts.time_since_epoch().count();
for (auto &point_data_with_attributes : metric_data.point_data_attr_)
{
proto::metrics::v1::NumberDataPoint *proto_gauge_point_data = gauge->add_data_points();
proto_gauge_point_data->set_start_time_unix_nano(start_ts);
proto_gauge_point_data->set_time_unix_nano(ts);
auto gauge_data =
nostd::get<sdk::metrics::LastValuePointData>(point_data_with_attributes.point_data);
if ((nostd::holds_alternative<int64_t>(gauge_data.value_)))
{
proto_gauge_point_data->set_as_int(nostd::get<int64_t>(gauge_data.value_));
}
else
{
proto_gauge_point_data->set_as_double(nostd::get<double>(gauge_data.value_));
}
// set attributes
for (auto &kv_attr : point_data_with_attributes.attributes)
{
OtlpPopulateAttributeUtils::PopulateAttribute(proto_gauge_point_data->add_attributes(),
kv_attr.first, kv_attr.second);
}
}
}
void OtlpMetricUtils::PopulateInstrumentInfoMetrics(
const opentelemetry::sdk::metrics::MetricData &metric_data,
proto::metrics::v1::Metric *metric) noexcept
{
metric->set_name(metric_data.instrument_descriptor.name_);
metric->set_description(metric_data.instrument_descriptor.description_);
metric->set_unit(metric_data.instrument_descriptor.unit_);
auto kind = GetAggregationType(metric_data);
switch (kind)
{
case metric_sdk::AggregationType::kSum: {
ConvertSumMetric(metric_data, metric->mutable_sum());
break;
}
case metric_sdk::AggregationType::kHistogram: {
ConvertHistogramMetric(metric_data, metric->mutable_histogram());
break;
}
case metric_sdk::AggregationType::kLastValue: {
ConvertGaugeMetric(metric_data, metric->mutable_gauge());
break;
}
default:
break;
}
}
void OtlpMetricUtils::PopulateResourceMetrics(
const opentelemetry::sdk::metrics::ResourceMetrics &data,
proto::metrics::v1::ResourceMetrics *resource_metrics) noexcept
{
OtlpPopulateAttributeUtils::PopulateAttribute(resource_metrics->mutable_resource(),
*(data.resource_));
for (auto &scope_metrics : data.scope_metric_data_)
{
if (scope_metrics.scope_ == nullptr)
{
continue;
}
auto scope_lib_metrics = resource_metrics->add_scope_metrics();
proto::common::v1::InstrumentationScope *scope = scope_lib_metrics->mutable_scope();
scope->set_name(scope_metrics.scope_->GetName());
scope->set_version(scope_metrics.scope_->GetVersion());
for (auto &metric_data : scope_metrics.metric_data_)
{
PopulateInstrumentInfoMetrics(metric_data, scope_lib_metrics->add_metrics());
}
}
}
void OtlpMetricUtils::PopulateRequest(
const opentelemetry::sdk::metrics::ResourceMetrics &data,
proto::collector::metrics::v1::ExportMetricsServiceRequest *request) noexcept
{
if (request == nullptr || data.resource_ == nullptr)
{
return;
}
auto resource_metrics = request->add_resource_metrics();
PopulateResourceMetrics(data, resource_metrics);
}
sdk::metrics::AggregationTemporalitySelector OtlpMetricUtils::ChooseTemporalitySelector(
PreferredAggregationTemporality preferred_aggregation_temporality) noexcept
{
if (preferred_aggregation_temporality == PreferredAggregationTemporality::kDelta)
{
return DeltaTemporalitySelector;
}
else if (preferred_aggregation_temporality == PreferredAggregationTemporality::kCumulative)
{
return CumulativeTemporalitySelector;
}
return LowMemoryTemporalitySelector;
}
sdk::metrics::AggregationTemporality OtlpMetricUtils::DeltaTemporalitySelector(
sdk::metrics::InstrumentType instrument_type) noexcept
{
switch (instrument_type)
{
case sdk::metrics::InstrumentType::kCounter:
case sdk::metrics::InstrumentType::kObservableCounter:
case sdk::metrics::InstrumentType::kHistogram:
case sdk::metrics::InstrumentType::kObservableGauge:
return sdk::metrics::AggregationTemporality::kDelta;
case sdk::metrics::InstrumentType::kUpDownCounter:
case sdk::metrics::InstrumentType::kObservableUpDownCounter:
return sdk::metrics::AggregationTemporality::kCumulative;
}
return sdk::metrics::AggregationTemporality::kUnspecified;
}
sdk::metrics::AggregationTemporality OtlpMetricUtils::CumulativeTemporalitySelector(
sdk::metrics::InstrumentType /* instrument_type */) noexcept
{
return sdk::metrics::AggregationTemporality::kCumulative;
}
sdk::metrics::AggregationTemporality OtlpMetricUtils::LowMemoryTemporalitySelector(
sdk::metrics::InstrumentType instrument_type) noexcept
{
switch (instrument_type)
{
case sdk::metrics::InstrumentType::kCounter:
case sdk::metrics::InstrumentType::kHistogram:
return sdk::metrics::AggregationTemporality::kDelta;
case sdk::metrics::InstrumentType::kObservableCounter:
case sdk::metrics::InstrumentType::kObservableGauge:
case sdk::metrics::InstrumentType::kUpDownCounter:
case sdk::metrics::InstrumentType::kObservableUpDownCounter:
return sdk::metrics::AggregationTemporality::kCumulative;
}
return sdk::metrics::AggregationTemporality::kUnspecified;
}
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE