From c134472168f604c4bfbc3cfa1181cd262add52dd Mon Sep 17 00:00:00 2001 From: xuan-cao-swi Date: Wed, 11 Dec 2024 18:10:43 -0500 Subject: [PATCH] add test case for metric_reader --- .../sdk/metrics/aggregation/drop.rb | 2 +- .../aggregation/explicit_bucket_histogram.rb | 2 +- .../sdk/metrics/aggregation/last_value.rb | 2 +- .../sdk/metrics/aggregation/sum.rb | 4 +- .../sdk/metrics/export/metric_exporter.rb | 4 + .../metrics/export/periodic_metric_reader.rb | 11 --- .../test/integration/metric_exporter_test.rb | 21 +++++ .../test/integration/metric_reader_test.rb | 92 +++++++++++++++++++ .../sdk/metrics/meter_provider_test.rb | 2 +- 9 files changed, 122 insertions(+), 18 deletions(-) create mode 100644 metrics_sdk/test/integration/metric_exporter_test.rb create mode 100644 metrics_sdk/test/integration/metric_reader_test.rb diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/drop.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/drop.rb index f638c649a..a3290b6cc 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/drop.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/drop.rb @@ -10,7 +10,7 @@ module Metrics module Aggregation # Contains the implementation of the Drop aggregation class Drop - attr_reader :aggregation_temporality + attr_accessor :aggregation_temporality def initialize(aggregation_temporality: :delta) @aggregation_temporality = aggregation_temporality diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb index 842db30af..ac23d6e24 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/explicit_bucket_histogram.rb @@ -14,7 +14,7 @@ class ExplicitBucketHistogram DEFAULT_BOUNDARIES = [0, 5, 10, 25, 50, 75, 100, 250, 500, 1000].freeze private_constant :DEFAULT_BOUNDARIES - attr_reader :aggregation_temporality + attr_accessor :aggregation_temporality # attr_accessor :aggregation_temporality # approach 2 diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/last_value.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/last_value.rb index b2cffb74e..dc48124f3 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/last_value.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/last_value.rb @@ -10,7 +10,7 @@ module Metrics module Aggregation # Contains the implementation of the LastValue aggregation class LastValue - attr_reader :aggregation_temporality + attr_accessor :aggregation_temporality def initialize(aggregation_temporality: :delta) @aggregation_temporality = aggregation_temporality diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/sum.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/sum.rb index 06e1924ec..fd41e044d 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/sum.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/sum.rb @@ -11,9 +11,7 @@ module Aggregation # Contains the implementation of the Sum aggregation # https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#sum-aggregation class Sum - attr_reader :aggregation_temporality - - # attr_accessor :aggregation_temporality # approach 2 + attr_accessor :aggregation_temporality def initialize(aggregation_temporality: ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE', :delta)) # TODO: the default should be :cumulative, see issue #1555 diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/export/metric_exporter.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/export/metric_exporter.rb index 94eaf00d8..e384ee49b 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/export/metric_exporter.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/export/metric_exporter.rb @@ -26,6 +26,10 @@ def pull(timeout: nil) export(collect, timeout: timeout) end + def export(metrics, timeout: nil) + Export::SUCCESS + end + def shutdown(timeout: nil) Export::SUCCESS end diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb index 2f7d6136f..97ccdd84b 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/export/periodic_metric_reader.rb @@ -29,7 +29,6 @@ def initialize(export_interval_millis: Float(ENV.fetch('OTEL_METRIC_EXPORT_INTER @export_interval = export_interval_millis / 1000.0 @export_timeout = export_timeout_millis / 1000.0 - # @exporter = exporter @thread = nil @continue = false @export_mutex = Mutex.new @@ -43,21 +42,14 @@ def shutdown(timeout: nil) @thread end thread&.join(@export_interval) - # @exporter.force_flush if @exporter.respond_to?(:force_flush) - # @exporter.shutdown super(timeout: timeout) - # Export::SUCCESS rescue StandardError => e OpenTelemetry.handle_error(exception: e, message: 'Fail to shutdown PeriodicMetricReader.') Export::FAILURE end def force_flush(timeout: nil) - # export(timeout: timeout) super(timeout: timeout) - # Export::SUCCESS - # rescue StandardError - # Export::FAILURE end private @@ -86,9 +78,6 @@ def start def export(timeout: nil) @export_mutex.synchronize do - # collected_metrics = collect - # collected_metrics = @exporters.each { |exporter| exporter.pull if exporter.respond_to?(:pull) } - # @exporter.export(collected_metrics, timeout: timeout || @export_timeout) unless collected_metrics.empty? @exporters.each { |exporter| exporter.pull(timeout: timeout || @export_timeout) if exporter.respond_to?(:pull) } end end diff --git a/metrics_sdk/test/integration/metric_exporter_test.rb b/metrics_sdk/test/integration/metric_exporter_test.rb new file mode 100644 index 000000000..f93036da3 --- /dev/null +++ b/metrics_sdk/test/integration/metric_exporter_test.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require_relative '../test_helper' + +describe OpenTelemetry::SDK do + describe '#metric_exporter' do + export = OpenTelemetry::SDK::Metrics::Export + let(:exporter) { export::MetricExporter.new } + + it 'verify basic exporter function' do + _(exporter.export(nil)).must_equal export::SUCCESS + _(exporter.shutdown).must_equal export::SUCCESS + _(exporter.force_flush).must_equal export::SUCCESS + _(exporter.collect).must_equal [] + end + end +end diff --git a/metrics_sdk/test/integration/metric_reader_test.rb b/metrics_sdk/test/integration/metric_reader_test.rb new file mode 100644 index 000000000..db506dbc0 --- /dev/null +++ b/metrics_sdk/test/integration/metric_reader_test.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +require_relative '../test_helper' + +describe OpenTelemetry::SDK do + describe '#metric_reader' do + export = OpenTelemetry::SDK::Metrics::Export + let(:exporter) { export::ConsoleMetricPullExporter.new } + + before do + reset_metrics_sdk + ENV['OTEL_METRICS_EXPORTER'] = 'none' + + @metric_reader = export::MetricReader.new(exporter: exporter) + + OpenTelemetry::SDK.configure + OpenTelemetry.meter_provider.add_metric_reader(@metric_reader) + + meter = OpenTelemetry.meter_provider.meter('test_1') + @counter = meter.create_counter('counter_1', unit: 'smidgen', description: 'a small amount of something') + end + + after do + ENV.delete('OTEL_METRICS_EXPORTER') + end + + it 'initialize metric_reader' do + metric_reader = export::MetricReader.new(exporter: exporter) + _(metric_reader.exporters.first.class).must_equal export::ConsoleMetricPullExporter + end + + it 'register additional metric_exporter' do + metric_reader = export::MetricReader.new(exporter: exporter) + in_memory_exporter = export::InMemoryMetricPullExporter.new + metric_reader.register_exporter(exporter: in_memory_exporter) + _(metric_reader.exporters[0].class).must_equal export::ConsoleMetricPullExporter + _(metric_reader.exporters[1].class).must_equal export::InMemoryMetricPullExporter + end + + it 'change default aggregator' do + default_aggregation = @counter.instance_variable_get(:@metric_streams).first.default_aggregation + + _(default_aggregation.class).must_equal OpenTelemetry::SDK::Metrics::Aggregation::Sum + _(default_aggregation.aggregation_temporality).must_equal :delta + + @metric_reader.aggregator(aggregator: OpenTelemetry::SDK::Metrics::Aggregation::Drop.new) + + default_aggregation = @counter.instance_variable_get(:@metric_streams).first.default_aggregation + _(default_aggregation.class).must_equal OpenTelemetry::SDK::Metrics::Aggregation::Drop + end + + it 'do not change default aggregator if different instrument kind' do + default_aggregation = @counter.instance_variable_get(:@metric_streams).first.default_aggregation + + _(default_aggregation.class).must_equal OpenTelemetry::SDK::Metrics::Aggregation::Sum + _(default_aggregation.aggregation_temporality).must_equal :delta + + @metric_reader.aggregator(aggregator: OpenTelemetry::SDK::Metrics::Aggregation::Drop.new, instrument_kind: :gauge) + + default_aggregation = @counter.instance_variable_get(:@metric_streams).first.default_aggregation + _(default_aggregation.class).must_equal OpenTelemetry::SDK::Metrics::Aggregation::Sum + end + + it 'change default aggregation_temporality' do + default_aggregation = @counter.instance_variable_get(:@metric_streams).first.default_aggregation + + _(default_aggregation.class).must_equal OpenTelemetry::SDK::Metrics::Aggregation::Sum + _(default_aggregation.aggregation_temporality).must_equal :delta + + @metric_reader.temporality(temporality: :cumulative) + + default_aggregation = @counter.instance_variable_get(:@metric_streams).first.default_aggregation + _(default_aggregation.aggregation_temporality).must_equal :cumulative + end + + it 'do not change default aggregation_temporality if different instrument kind' do + default_aggregation = @counter.instance_variable_get(:@metric_streams).first.default_aggregation + + _(default_aggregation.class).must_equal OpenTelemetry::SDK::Metrics::Aggregation::Sum + _(default_aggregation.aggregation_temporality).must_equal :delta + + @metric_reader.temporality(temporality: :cumulative, instrument_kind: :gauge) + + default_aggregation = @counter.instance_variable_get(:@metric_streams).first.default_aggregation + _(default_aggregation.aggregation_temporality).must_equal :delta + end + end +end diff --git a/metrics_sdk/test/opentelemetry/sdk/metrics/meter_provider_test.rb b/metrics_sdk/test/opentelemetry/sdk/metrics/meter_provider_test.rb index c7c727a5a..83e72daf6 100644 --- a/metrics_sdk/test/opentelemetry/sdk/metrics/meter_provider_test.rb +++ b/metrics_sdk/test/opentelemetry/sdk/metrics/meter_provider_test.rb @@ -119,7 +119,7 @@ metric_exporter_b = OpenTelemetry::SDK::Metrics::Export::MetricExporter.new metric_reader_b = OpenTelemetry::SDK::Metrics::Export::MetricReader.new(exporter: metric_exporter_b) OpenTelemetry.meter_provider.add_metric_reader(metric_reader_b) - + _(meter_a.instance_variable_get(:@metric_streams).size).must_equal(2) _(metric_reader_a.exporters.first.metric_store.instance_variable_get(:@metric_streams).size).must_equal(1) _(metric_reader_b.exporters.first.metric_store.instance_variable_get(:@metric_streams).size).must_equal(1)