Skip to content

Commit

Permalink
Fix dependency of otlp recordable, add Gauge support for `OtlpMetri…
Browse files Browse the repository at this point in the history
…cUtils::PopulateRequest`

Signed-off-by: owentou <owentou@tencent.com>
  • Loading branch information
owent committed Jul 16, 2022
1 parent 96a2c01 commit 9a79f7d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 30 deletions.
1 change: 1 addition & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
89 changes: 61 additions & 28 deletions exporters/otlp/src/otlp_metric_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<sdk::metrics::SumPointData>(point_data_with_attributes.point_data);

if ((nostd::holds_alternative<long>(sum_data.value_)))
{
proto_sum_point_data.set_as_int(nostd::get<long>(sum_data.value_));
proto_sum_point_data->set_as_int(nostd::get<long>(sum_data.value_));
}
else
{
proto_sum_point_data.set_as_double(nostd::get<double>(sum_data.value_));
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(),
OtlpPopulateAttributeUtils::PopulateAttribute(proto_sum_point_data->add_attributes(),
kv_attr.first, kv_attr.second);
}
*sum->add_data_points() = proto_sum_point_data;
}
}

Expand All @@ -90,51 +89,80 @@ 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<sdk::metrics::HistogramPointData>(point_data_with_attributes.point_data);
// sum
if ((nostd::holds_alternative<long>(histogram_data.sum_)))
{
proto_histogram_point_data.set_sum(nostd::get<long>(histogram_data.sum_));
proto_histogram_point_data->set_sum(nostd::get<long>(histogram_data.sum_));
}
else
{
proto_histogram_point_data.set_sum(nostd::get<double>(histogram_data.sum_));
proto_histogram_point_data->set_sum(nostd::get<double>(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<std::list<double>>(histogram_data.boundaries_)))
{
auto boundaries = nostd::get<std::list<double>>(histogram_data.boundaries_);
for (auto bound : boundaries)
{
proto_histogram_point_data.add_explicit_bounds(bound);
proto_histogram_point_data->add_explicit_bounds(bound);
}
}
else
{
auto boundaries = nostd::get<std::list<long>>(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<sdk::metrics::SumPointData>(point_data_with_attributes.point_data);

if ((nostd::holds_alternative<long>(sum_data.value_)))
{
proto_sum_point_data->set_as_int(nostd::get<long>(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);
}
*histogram->add_data_points() = proto_histogram_point_data;
}
}

Expand All @@ -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;
}
}

Expand Down
45 changes: 43 additions & 2 deletions exporters/otlp/test/otlp_metrics_serialization_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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<metrics_sdk::PointDataAttributes> 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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9a79f7d

Please sign in to comment.