From 9a79f7d3bb0d72781e8ecad3800db9118227b7c2 Mon Sep 17 00:00:00 2001 From: owentou Date: Wed, 13 Jul 2022 00:19:36 +0800 Subject: [PATCH] Fix dependency of otlp recordable, add `Gauge` support for `OtlpMetricUtils::PopulateRequest` Signed-off-by: owentou --- exporters/otlp/BUILD | 1 + .../exporters/otlp/otlp_metric_utils.h | 3 + exporters/otlp/src/otlp_metric_utils.cc | 89 +++++++++++++------ .../test/otlp_metrics_serialization_test.cc | 45 +++++++++- 4 files changed, 108 insertions(+), 30 deletions(-) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index fc8b33f8fa..072fd2ca4f 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -41,6 +41,7 @@ cc_library( "//sdk/src/resource", "//sdk/src/trace", "@com_github_opentelemetry_proto//:logs_service_proto_cc", + "@com_github_opentelemetry_proto//:metrics_service_proto_cc", "@com_github_opentelemetry_proto//:trace_service_proto_cc", ], ) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_metric_utils.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_metric_utils.h index c344613cb3..380c96d279 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_metric_utils.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_metric_utils.h @@ -38,6 +38,9 @@ class OtlpMetricUtils static void ConvertHistogramMetric(const opentelemetry::sdk::metrics::MetricData &metric_data, proto::metrics::v1::Histogram *const histogram) noexcept; + static void ConvertGaugeMetric(const opentelemetry::sdk::metrics::MetricData &metric_data, + proto::metrics::v1::Gauge *const gauge) noexcept; + static void PopulateInstrumentationInfoMetric( const opentelemetry::sdk::metrics::MetricData &metric_data, proto::metrics::v1::Metric *metric) noexcept; diff --git a/exporters/otlp/src/otlp_metric_utils.cc b/exporters/otlp/src/otlp_metric_utils.cc index ad9e2d117f..eb3e72ac3a 100644 --- a/exporters/otlp/src/otlp_metric_utils.cc +++ b/exporters/otlp/src/otlp_metric_utils.cc @@ -57,26 +57,25 @@ void OtlpMetricUtils::ConvertSumMetric(const metric_sdk::MetricData &metric_data 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; - proto_sum_point_data.set_start_time_unix_nano(start_ts); - proto_sum_point_data.set_time_unix_nano(ts); + 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(point_data_with_attributes.point_data); if ((nostd::holds_alternative(sum_data.value_))) { - proto_sum_point_data.set_as_int(nostd::get(sum_data.value_)); + proto_sum_point_data->set_as_int(nostd::get(sum_data.value_)); } else { - proto_sum_point_data.set_as_double(nostd::get(sum_data.value_)); + proto_sum_point_data->set_as_double(nostd::get(sum_data.value_)); } // set attributes for (auto &kv_attr : point_data_with_attributes.attributes) { - OtlpPopulateAttributeUtils::PopulateAttribute(proto_sum_point_data.add_attributes(), + OtlpPopulateAttributeUtils::PopulateAttribute(proto_sum_point_data->add_attributes(), kv_attr.first, kv_attr.second); } - *sum->add_data_points() = proto_sum_point_data; } } @@ -90,29 +89,30 @@ void OtlpMetricUtils::ConvertHistogramMetric( 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; - proto_histogram_point_data.set_start_time_unix_nano(start_ts); - proto_histogram_point_data.set_time_unix_nano(ts); + 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(point_data_with_attributes.point_data); // sum if ((nostd::holds_alternative(histogram_data.sum_))) { - proto_histogram_point_data.set_sum(nostd::get(histogram_data.sum_)); + proto_histogram_point_data->set_sum(nostd::get(histogram_data.sum_)); } else { - proto_histogram_point_data.set_sum(nostd::get(histogram_data.sum_)); + proto_histogram_point_data->set_sum(nostd::get(histogram_data.sum_)); } // count - proto_histogram_point_data.set_count(histogram_data.count_); + proto_histogram_point_data->set_count(histogram_data.count_); // buckets if ((nostd::holds_alternative>(histogram_data.boundaries_))) { auto boundaries = nostd::get>(histogram_data.boundaries_); for (auto bound : boundaries) { - proto_histogram_point_data.add_explicit_bounds(bound); + proto_histogram_point_data->add_explicit_bounds(bound); } } else @@ -120,21 +120,49 @@ void OtlpMetricUtils::ConvertHistogramMetric( auto boundaries = nostd::get>(histogram_data.boundaries_); for (auto bound : boundaries) { - proto_histogram_point_data.add_explicit_bounds(bound); + 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); + 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(), + 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_sum_point_data = gauge->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(point_data_with_attributes.point_data); + + if ((nostd::holds_alternative(sum_data.value_))) + { + proto_sum_point_data->set_as_int(nostd::get(sum_data.value_)); + } + else + { + proto_sum_point_data->set_as_double(nostd::get(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); } - *histogram->add_data_points() = proto_histogram_point_data; } } @@ -146,17 +174,22 @@ void OtlpMetricUtils::PopulateInstrumentationInfoMetric( metric->set_description(metric_data.instrument_descriptor.description_); metric->set_unit(metric_data.instrument_descriptor.unit_); auto kind = GetAggregationType(metric_data.instrument_descriptor.type_); - if (kind == metric_sdk::AggregationType::kSum) - { - proto::metrics::v1::Sum sum; - ConvertSumMetric(metric_data, &sum); - *metric->mutable_sum() = sum; - } - else if (kind == metric_sdk::AggregationType::kHistogram) + switch (kind) { - proto::metrics::v1::Histogram histogram; - ConvertHistogramMetric(metric_data, &histogram); - *metric->mutable_histogram() = histogram; + 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; } } diff --git a/exporters/otlp/test/otlp_metrics_serialization_test.cc b/exporters/otlp/test/otlp_metrics_serialization_test.cc index 30b176ee53..855d8941e5 100644 --- a/exporters/otlp/test/otlp_metrics_serialization_test.cc +++ b/exporters/otlp/test/otlp_metrics_serialization_test.cc @@ -18,7 +18,7 @@ namespace proto = opentelemetry::proto; namespace metrics_sdk = opentelemetry::sdk::metrics; namespace otlp_exporter = opentelemetry::exporter::otlp; -metrics_sdk::MetricData CreateSumAggregationData() +static metrics_sdk::MetricData CreateSumAggregationData() { metrics_sdk::MetricData data; data.start_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()); @@ -45,7 +45,7 @@ metrics_sdk::MetricData CreateSumAggregationData() return data; } -metrics_sdk::MetricData CreateHistogramAggregationData() +static metrics_sdk::MetricData CreateHistogramAggregationData() { metrics_sdk::MetricData data; data.start_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()); @@ -78,6 +78,33 @@ metrics_sdk::MetricData CreateHistogramAggregationData() return data; } +static metrics_sdk::MetricData CreateObservableGaugeAggregationData() +{ + metrics_sdk::MetricData data; + data.start_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()); + metrics_sdk::InstrumentDescriptor inst_desc = {"LastValue", "desc", "unit", + metrics_sdk::InstrumentType::kObservableGauge, + metrics_sdk::InstrumentValueType::kDouble}; + metrics_sdk::SumPointData s_data_1, s_data_2; + s_data_1.value_ = 30.2; + s_data_2.value_ = 50.2; + + data.aggregation_temporality = metrics_sdk::AggregationTemporality::kCumulative; + data.end_ts = opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()); + data.instrument_descriptor = inst_desc; + metrics_sdk::PointDataAttributes point_data_attr_1, point_data_attr_2; + point_data_attr_1.attributes = {{"k1", "v1"}}; + point_data_attr_1.point_data = s_data_1; + + point_data_attr_2.attributes = {{"k2", "v2"}}; + point_data_attr_2.point_data = s_data_1; + std::vector point_data_attr; + point_data_attr.push_back(point_data_attr_1); + point_data_attr.push_back(point_data_attr_2); + data.point_data_attr_ = std::move(point_data_attr); + return data; +} + TEST(OtlpMetricSerializationTest, Counter) { metrics_sdk::MetricData data = CreateSumAggregationData(); @@ -111,6 +138,20 @@ TEST(OtlpMetricSerializationTest, Histogram) EXPECT_EQ(1, 1); } +TEST(OtlpMetricSerializationTest, ObservableGauge) +{ + metrics_sdk::MetricData data = CreateObservableGaugeAggregationData(); + opentelemetry::proto::metrics::v1::Gauge gauge; + otlp_exporter::OtlpMetricUtils::ConvertGaugeMetric(data, &gauge); + for (size_t i = 0; i < 1; i++) + { + auto proto_number_point = gauge.data_points(i); + EXPECT_EQ(proto_number_point.as_double(), i == 0 ? 30.2 : 50.2); + } + + EXPECT_EQ(1, 1); +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE