Skip to content

Commit

Permalink
add test case for metric_reader
Browse files Browse the repository at this point in the history
  • Loading branch information
xuan-cao-swi committed Dec 11, 2024
1 parent f3466ce commit c134472
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions metrics_sdk/lib/opentelemetry/sdk/metrics/aggregation/sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions metrics_sdk/test/integration/metric_exporter_test.rb
Original file line number Diff line number Diff line change
@@ -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
92 changes: 92 additions & 0 deletions metrics_sdk/test/integration/metric_reader_test.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c134472

Please sign in to comment.