11// Copyright The OpenTelemetry Authors
22// SPDX-License-Identifier: Apache-2.0
33
4- #include < stdint.h >
4+ #include < cstdint >
55#include < map>
66#include < memory>
77#include < utility>
@@ -38,6 +38,18 @@ namespace otlp
3838{
3939namespace metric_sdk = opentelemetry::sdk::metrics;
4040
41+ namespace
42+ {
43+
44+ struct NumberDataPointValueSetter
45+ {
46+ proto::metrics::v1::NumberDataPoint *point;
47+ void operator ()(int64_t v) const noexcept { point->set_as_int (v); }
48+ void operator ()(double v) const noexcept { point->set_as_double (v); }
49+ };
50+
51+ } // namespace
52+
4153proto::metrics::v1::AggregationTemporality OtlpMetricUtils::GetProtoAggregationTemporality (
4254 const opentelemetry::sdk::metrics::AggregationTemporality &aggregation_temporality) noexcept
4355{
@@ -89,23 +101,22 @@ void OtlpMetricUtils::ConvertSumMetric(const metric_sdk::MetricData &metric_data
89101 (metric_data.instrument_descriptor .type_ == metric_sdk::InstrumentType::kObservableCounter ));
90102 auto start_ts = metric_data.start_ts .time_since_epoch ().count ();
91103 auto ts = metric_data.end_ts .time_since_epoch ().count ();
92- for (auto &point_data_with_attributes : metric_data.point_data_attr_ )
104+ for (const auto &point_data_with_attributes : metric_data.point_data_attr_ )
93105 {
94106 proto::metrics::v1::NumberDataPoint *proto_sum_point_data = sum->add_data_points ();
95107 proto_sum_point_data->set_start_time_unix_nano (start_ts);
96108 proto_sum_point_data->set_time_unix_nano (ts);
97- auto sum_data = nostd::get<sdk::metrics::SumPointData>(point_data_with_attributes.point_data );
98-
99- if ((nostd::holds_alternative<int64_t >(sum_data.value_ )))
100- {
101- proto_sum_point_data->set_as_int (nostd::get<int64_t >(sum_data.value_ ));
102- }
103- else
109+ const auto *maybe_sum_data =
110+ nostd::get_if<sdk::metrics::SumPointData>(&point_data_with_attributes.point_data );
111+ if (maybe_sum_data == nullptr )
104112 {
105- proto_sum_point_data-> set_as_double (nostd::get< double >(sum_data. value_ )) ;
113+ continue ;
106114 }
115+ const auto &sum_data = *maybe_sum_data;
116+
117+ nostd::visit (NumberDataPointValueSetter{proto_sum_point_data}, sum_data.value_ );
107118 // set attributes
108- for (auto &kv_attr : point_data_with_attributes.attributes )
119+ for (const auto &kv_attr : point_data_with_attributes.attributes )
109120 {
110121 OtlpPopulateAttributeUtils::PopulateAttribute (proto_sum_point_data->add_attributes (),
111122 kv_attr.first , kv_attr.second , false );
@@ -121,63 +132,46 @@ void OtlpMetricUtils::ConvertHistogramMetric(
121132 GetProtoAggregationTemporality (metric_data.aggregation_temporality ));
122133 auto start_ts = metric_data.start_ts .time_since_epoch ().count ();
123134 auto ts = metric_data.end_ts .time_since_epoch ().count ();
124- for (auto &point_data_with_attributes : metric_data.point_data_attr_ )
135+ for (const auto &point_data_with_attributes : metric_data.point_data_attr_ )
125136 {
126137 proto::metrics::v1::HistogramDataPoint *proto_histogram_point_data =
127138 histogram->add_data_points ();
128139 proto_histogram_point_data->set_start_time_unix_nano (start_ts);
129140 proto_histogram_point_data->set_time_unix_nano (ts);
130- auto histogram_data =
131- nostd::get<sdk::metrics::HistogramPointData>(point_data_with_attributes.point_data );
132- // sum
133- if ((nostd::holds_alternative<int64_t >(histogram_data.sum_ )))
134- {
135- // Use static_cast to avoid C4244 in MSVC
136- proto_histogram_point_data->set_sum (
137- static_cast <double >(nostd::get<int64_t >(histogram_data.sum_ )));
138- }
139- else
141+ const auto *maybe_histogram_data =
142+ nostd::get_if<sdk::metrics::HistogramPointData>(&point_data_with_attributes.point_data );
143+ if (maybe_histogram_data == nullptr )
140144 {
141- proto_histogram_point_data-> set_sum (nostd::get< double >(histogram_data. sum_ )) ;
145+ continue ;
142146 }
147+ const auto &histogram_data = *maybe_histogram_data;
148+ // sum
149+ // Use static_cast to avoid C4244 in MSVC
150+ proto_histogram_point_data->set_sum (
151+ nostd::visit ([](auto v) { return static_cast <double >(v); }, histogram_data.sum_ ));
143152 // count
144153 proto_histogram_point_data->set_count (histogram_data.count_ );
145154 if (histogram_data.record_min_max_ )
146155 {
147- if (nostd::holds_alternative<int64_t >(histogram_data.min_ ))
148- {
149- // Use static_cast to avoid C4244 in MSVC
150- proto_histogram_point_data->set_min (
151- static_cast <double >(nostd::get<int64_t >(histogram_data.min_ )));
152- }
153- else
154- {
155- proto_histogram_point_data->set_min (nostd::get<double >(histogram_data.min_ ));
156- }
157- if (nostd::holds_alternative<int64_t >(histogram_data.max_ ))
158- {
159- // Use static_cast to avoid C4244 in MSVC
160- proto_histogram_point_data->set_max (
161- static_cast <double >(nostd::get<int64_t >(histogram_data.max_ )));
162- }
163- else
164- {
165- proto_histogram_point_data->set_max (nostd::get<double >(histogram_data.max_ ));
166- }
156+ // Use static_cast to avoid C4244 in MSVC
157+ proto_histogram_point_data->set_min (
158+ nostd::visit ([](auto v) { return static_cast <double >(v); }, histogram_data.min_ ));
159+ proto_histogram_point_data->set_max (
160+ nostd::visit ([](auto v) { return static_cast <double >(v); }, histogram_data.max_ ));
167161 }
168162 // buckets
169163
170- for (auto bound : histogram_data.boundaries_ )
164+ for (const auto & bound : histogram_data.boundaries_ )
171165 {
172166 proto_histogram_point_data->add_explicit_bounds (bound);
173167 }
174168 // bucket counts
175- for (auto bucket_value : histogram_data.counts_ )
169+ for (const auto & bucket_value : histogram_data.counts_ )
176170 {
177171 proto_histogram_point_data->add_bucket_counts (bucket_value);
178172 }
179173 // attributes
180- for (auto &kv_attr : point_data_with_attributes.attributes )
174+ for (const auto &kv_attr : point_data_with_attributes.attributes )
181175 {
182176 OtlpPopulateAttributeUtils::PopulateAttribute (proto_histogram_point_data->add_attributes (),
183177 kv_attr.first , kv_attr.second , false );
@@ -193,14 +187,20 @@ void OtlpMetricUtils::ConvertExponentialHistogramMetric(
193187 GetProtoAggregationTemporality (metric_data.aggregation_temporality ));
194188 auto start_ts = metric_data.start_ts .time_since_epoch ().count ();
195189 auto ts = metric_data.end_ts .time_since_epoch ().count ();
196- for (auto &point_data_with_attributes : metric_data.point_data_attr_ )
190+ for (const auto &point_data_with_attributes : metric_data.point_data_attr_ )
197191 {
198192 proto::metrics::v1::ExponentialHistogramDataPoint *proto_histogram_point_data =
199193 histogram->add_data_points ();
200194 proto_histogram_point_data->set_start_time_unix_nano (start_ts);
201195 proto_histogram_point_data->set_time_unix_nano (ts);
202- auto histogram_data = nostd::get<sdk::metrics::Base2ExponentialHistogramPointData>(
203- point_data_with_attributes.point_data );
196+ const auto *maybe_histogram_data =
197+ nostd::get_if<sdk::metrics::Base2ExponentialHistogramPointData>(
198+ &point_data_with_attributes.point_data );
199+ if (maybe_histogram_data == nullptr )
200+ {
201+ continue ;
202+ }
203+ const auto &histogram_data = *maybe_histogram_data;
204204 if (histogram_data.positive_buckets_ == nullptr && histogram_data.negative_buckets_ == nullptr )
205205 {
206206 continue ;
@@ -241,7 +241,7 @@ void OtlpMetricUtils::ConvertExponentialHistogramMetric(
241241 proto_histogram_point_data->set_zero_count (histogram_data.zero_count_ );
242242
243243 // attributes
244- for (auto &kv_attr : point_data_with_attributes.attributes )
244+ for (const auto &kv_attr : point_data_with_attributes.attributes )
245245 {
246246 OtlpPopulateAttributeUtils::PopulateAttribute (proto_histogram_point_data->add_attributes (),
247247 kv_attr.first , kv_attr.second , false );
@@ -254,24 +254,22 @@ void OtlpMetricUtils::ConvertGaugeMetric(const opentelemetry::sdk::metrics::Metr
254254{
255255 auto start_ts = metric_data.start_ts .time_since_epoch ().count ();
256256 auto ts = metric_data.end_ts .time_since_epoch ().count ();
257- for (auto &point_data_with_attributes : metric_data.point_data_attr_ )
257+ for (const auto &point_data_with_attributes : metric_data.point_data_attr_ )
258258 {
259259 proto::metrics::v1::NumberDataPoint *proto_gauge_point_data = gauge->add_data_points ();
260260 proto_gauge_point_data->set_start_time_unix_nano (start_ts);
261261 proto_gauge_point_data->set_time_unix_nano (ts);
262- auto gauge_data =
263- nostd::get<sdk::metrics::LastValuePointData>(point_data_with_attributes.point_data );
264-
265- if ((nostd::holds_alternative<int64_t >(gauge_data.value_ )))
262+ const auto *maybe_gauge_data =
263+ nostd::get_if<sdk::metrics::LastValuePointData>(&point_data_with_attributes.point_data );
264+ if (maybe_gauge_data == nullptr )
266265 {
267- proto_gauge_point_data->set_as_int (nostd::get<int64_t >(gauge_data.value_ ));
268- }
269- else
270- {
271- proto_gauge_point_data->set_as_double (nostd::get<double >(gauge_data.value_ ));
266+ continue ;
272267 }
268+ const auto &gauge_data = *maybe_gauge_data;
269+
270+ nostd::visit (NumberDataPointValueSetter{proto_gauge_point_data}, gauge_data.value_ );
273271 // set attributes
274- for (auto &kv_attr : point_data_with_attributes.attributes )
272+ for (const auto &kv_attr : point_data_with_attributes.attributes )
275273 {
276274 OtlpPopulateAttributeUtils::PopulateAttribute (proto_gauge_point_data->add_attributes (),
277275 kv_attr.first , kv_attr.second , false );
0 commit comments