From 586c23235fc93746629f3f44e046bd42dd389214 Mon Sep 17 00:00:00 2001 From: Josh Westbrook Date: Mon, 16 Dec 2024 11:38:42 -0600 Subject: [PATCH 1/3] feat: Gauge metrics exporter encoding --- .../exporter/otlp/metrics/metrics_exporter.rb | 14 ++++++++++- .../otlp/metrics/metrics_exporter_test.rb | 25 ++++++++++++++++++- exporter/otlp-metrics/test/test_helper.rb | 1 + 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb b/exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb index 36705bbf7..55f7ded7b 100644 --- a/exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb +++ b/exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb @@ -214,11 +214,23 @@ def encode(metrics_data) end # metrics_pb has following type of data: :gauge, :sum, :histogram, :exponential_histogram, :summary - # current metric sdk only implements instrument: :counter -> :sum, :histogram -> :histogram + # current metric sdk only implements instrument: :counter -> :sum, :histogram -> :histogram, :gauge -> :gauge # # metrics [MetricData] def as_otlp_metrics(metrics) case metrics.instrument_kind + when :gauge + Opentelemetry::Proto::Metrics::V1::Metric.new( + name: metrics.name, + description: metrics.description, + unit: metrics.unit, + gauge: Opentelemetry::Proto::Metrics::V1::Gauge.new( + data_points: metrics.data_points.map do |ndp| + number_data_point(ndp) + end + ) + ) + when :observable_gauge Opentelemetry::Proto::Metrics::V1::Metric.new( name: metrics.name, diff --git a/exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb b/exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb index 6076a80a2..fc9b4d74f 100644 --- a/exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb +++ b/exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb @@ -576,11 +576,16 @@ stub_request(:post, 'http://localhost:4318/v1/metrics').to_return(status: 200) meter_provider.add_metric_reader(exporter) meter = meter_provider.meter('test') + counter = meter.create_counter('test_counter', unit: 'smidgen', description: 'a small amount of something') counter.add(5, attributes: { 'foo' => 'bar' }) histogram = meter.create_histogram('test_histogram', unit: 'smidgen', description: 'a small amount of something') histogram.record(10, attributes: { 'oof' => 'rab' }) + + gauge = meter.create_gauge('test_gauge', unit: 'smidgen', description: 'a small amount of something') + gauge.record(15, attributes: { 'baz' => 'qux' }) + exporter.pull meter_provider.shutdown @@ -644,7 +649,25 @@ ], aggregation_temporality: Opentelemetry::Proto::Metrics::V1::AggregationTemporality::AGGREGATION_TEMPORALITY_DELTA ) - ) + ), + Opentelemetry::Proto::Metrics::V1::Metric.new( + name: 'test_gauge', + description: 'a small amount of something', + unit: 'smidgen', + gauge: Opentelemetry::Proto::Metrics::V1::Gauge.new( + data_points: [ + Opentelemetry::Proto::Metrics::V1::NumberDataPoint.new( + attributes: [ + Opentelemetry::Proto::Common::V1::KeyValue.new(key: 'baz', value: Opentelemetry::Proto::Common::V1::AnyValue.new(string_value: 'qux')) + ], + as_int: 15, + start_time_unix_nano: 1_699_593_427_329_946_585, + time_unix_nano: 1_699_593_427_329_946_586, + exemplars: nil + ) + ], + ) + ), ] ) ] diff --git a/exporter/otlp-metrics/test/test_helper.rb b/exporter/otlp-metrics/test/test_helper.rb index 9fc446645..4d3e4df6d 100644 --- a/exporter/otlp-metrics/test/test_helper.rb +++ b/exporter/otlp-metrics/test/test_helper.rb @@ -26,6 +26,7 @@ def collect(start_time, end_time, data_points) OpenTelemetry::SDK::Metrics::Aggregation::Sum.prepend(MockSum) OpenTelemetry::SDK::Metrics::Aggregation::ExplicitBucketHistogram.prepend(MockSum) +OpenTelemetry::SDK::Metrics::Aggregation::LastValue.prepend(MockSum) def create_metrics_data(name: '', description: '', unit: '', instrument_kind: :counter, resource: nil, instrumentation_scope: OpenTelemetry::SDK::InstrumentationScope.new('', 'v0.0.1'), From c32f2c0d490189f3bc39322c617cd137d6be8128 Mon Sep 17 00:00:00 2001 From: Josh Westbrook Date: Mon, 16 Dec 2024 13:14:06 -0600 Subject: [PATCH 2/3] fix: combine observable_gauge and gauge --- .../exporter/otlp/metrics/metrics_exporter.rb | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb b/exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb index 55f7ded7b..d2beac6d5 100644 --- a/exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb +++ b/exporter/otlp-metrics/lib/opentelemetry/exporter/otlp/metrics/metrics_exporter.rb @@ -219,7 +219,7 @@ def encode(metrics_data) # metrics [MetricData] def as_otlp_metrics(metrics) case metrics.instrument_kind - when :gauge + when :observable_gauge, :gauge Opentelemetry::Proto::Metrics::V1::Metric.new( name: metrics.name, description: metrics.description, @@ -231,19 +231,6 @@ def as_otlp_metrics(metrics) ) ) - when :observable_gauge - Opentelemetry::Proto::Metrics::V1::Metric.new( - name: metrics.name, - description: metrics.description, - unit: metrics.unit, - gauge: Opentelemetry::Proto::Metrics::V1::Gauge.new( - aggregation_temporality: as_otlp_aggregation_temporality(metrics.aggregation_temporality), - data_points: metrics.data_points.map do |ndp| - number_data_point(ndp) - end - ) - ) - when :counter, :up_down_counter Opentelemetry::Proto::Metrics::V1::Metric.new( name: metrics.name, From 4bf61939d4c6c45bc4697e3dca3003271c89fef0 Mon Sep 17 00:00:00 2001 From: Josh Westbrook Date: Mon, 16 Dec 2024 14:34:30 -0600 Subject: [PATCH 3/3] fix: rubocop --- .../exporter/otlp/metrics/metrics_exporter_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb b/exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb index fc9b4d74f..020d0e9a8 100644 --- a/exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb +++ b/exporter/otlp-metrics/test/opentelemetry/exporter/otlp/metrics/metrics_exporter_test.rb @@ -665,9 +665,9 @@ time_unix_nano: 1_699_593_427_329_946_586, exemplars: nil ) - ], + ] ) - ), + ) ] ) ]