Skip to content

Commit

Permalink
fix: ruby style
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlaurin committed Feb 25, 2022
1 parent f64082e commit f075de5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
35 changes: 21 additions & 14 deletions metrics_sdk/lib/opentelemetry/sdk/metrics/meter_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def meter(name, version = nil)
# Attempts to stop all the activity for this {MeterProvider}.
#
# Calls MetricReader#shutdown for all registered MetricReaders.
# Calls MetricExporter#shutdown for all registered MetricExporters.
#
# After this is called all the newly created {Meter}s will be no-op.
#
Expand All @@ -56,11 +55,13 @@ def shutdown(timeout: nil)
Export::FAILURE
else
start_time = OpenTelemetry::Common::Utilities.timeout_timestamp

results = @metric_readers.map do |metric_reader|
remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
break [Export::TIMEOUT] if remaining_timeout&.zero?
metric_reader.shutdown(timeout: remaining_timeout)
if remaining_timeout&.zero?
Export::TIMEOUT
else
metric_reader.shutdown(timeout: remaining_timeout)
end
end

@stopped = true
Expand All @@ -80,16 +81,21 @@ def shutdown(timeout: nil)
# a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.
def force_flush(timeout: nil)
@mutex.synchronize do
return Export::SUCCESS if @stopped

start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
results = @metric_readers.map do |metric_reader|
remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
return Export::TIMEOUT if remaining_timeout&.zero?
if @stopped
Export::SUCCESS
else
start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
results = @metric_readers.map do |metric_reader|
remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
if remaining_timeout&.zero?
Export::TIMEOUT
else
metric_reader.force_flush(timeout: remaining_timeout)
end
end

metric_reader.force_flush(timeout: remaining_timeout)
results.max || Export::SUCCESS
end
results.max || Export::SUCCESS
end
end

Expand All @@ -100,10 +106,11 @@ def add_metric_reader(metric_reader)
@mutex.synchronize do
if @stopped
OpenTelemetry.logger.warn('calling MetricProvider#add_metric_reader after shutdown.')
return
else
@metric_readers.push(metric_reader)
end

@metric_readers = @metric_readers.push(metric_reader)
nil
end
end

Expand Down
37 changes: 20 additions & 17 deletions metrics_sdk/test/opentelemetry/sdk/metrics/meter_provider_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@
end

it 'invokes shutdown on all registered Metric Readers' do
mock_metric_reader_1 = Minitest::Mock.new
mock_metric_reader_2 = Minitest::Mock.new
mock_metric_reader_1.expect(:shutdown, nil, [{ timeout: nil }])
mock_metric_reader_2.expect(:shutdown, nil, [{ timeout: nil }])
mock_metric_reader1 = Minitest::Mock.new
mock_metric_reader2 = Minitest::Mock.new
mock_metric_reader1.expect(:shutdown, nil, [{ timeout: nil }])
mock_metric_reader2.expect(:shutdown, nil, [{ timeout: nil }])

OpenTelemetry.meter_provider.add_metric_reader(mock_metric_reader_1)
OpenTelemetry.meter_provider.add_metric_reader(mock_metric_reader_2)
OpenTelemetry.meter_provider.add_metric_reader(mock_metric_reader1)
OpenTelemetry.meter_provider.add_metric_reader(mock_metric_reader2)
OpenTelemetry.meter_provider.shutdown

mock_metric_reader_1.verify
mock_metric_reader_2.verify
mock_metric_reader1.verify
mock_metric_reader2.verify
end
end

Expand All @@ -82,17 +82,17 @@
end

it 'invokes force_flush on all registered Metric Readers' do
mock_metric_reader_1 = Minitest::Mock.new
mock_metric_reader_2 = Minitest::Mock.new
mock_metric_reader_1.expect(:force_flush, nil, [{ timeout: nil }])
mock_metric_reader_2.expect(:force_flush, nil, [{ timeout: nil }])
OpenTelemetry.meter_provider.add_metric_reader(mock_metric_reader_1)
OpenTelemetry.meter_provider.add_metric_reader(mock_metric_reader_2)
mock_metric_reader1 = Minitest::Mock.new
mock_metric_reader2 = Minitest::Mock.new
mock_metric_reader1.expect(:force_flush, nil, [{ timeout: nil }])
mock_metric_reader2.expect(:force_flush, nil, [{ timeout: nil }])
OpenTelemetry.meter_provider.add_metric_reader(mock_metric_reader1)
OpenTelemetry.meter_provider.add_metric_reader(mock_metric_reader2)

OpenTelemetry.meter_provider.force_flush

mock_metric_reader_1.verify
mock_metric_reader_2.verify
mock_metric_reader1.verify
mock_metric_reader2.verify
end
end

Expand All @@ -109,6 +109,9 @@
end

describe '#add_view' do
# TODO
it 'adds a view' do
# TODO
# OpenTelemetry.meter_provider.add_view
end
end
end

0 comments on commit f075de5

Please sign in to comment.