forked from open-telemetry/opentelemetry-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update metrics configuration patch (open-telemetry#1548)
* feat: Add configuration patch to set metrics exporter by environment variable The environment variable has options for: * console (default) * otlp * in-memory * none Like the Traces exporter, more than one exporter can be configured * fix: Use updated class name for OTLP exporter * feat: Update config patch for spec compliance * Default option is now OTLP * In-Memory option removed, it's not in the spec * PeriodicMetricReader added for both Console and OTLP exporters * Add the metrics OTLP exporter gem to the test gem group * test: Remove setup/teardown tracer env var setting * test: Update tests for JRuby * style: Rubocop lines in block --------- Co-authored-by: Matthew Wear <[email protected]>
- Loading branch information
1 parent
aa6ecce
commit d60e3a7
Showing
5 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
metrics_sdk/test/opentelemetry/sdk/metrics/configuration_patch_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'test_helper' | ||
require 'opentelemetry-exporter-otlp-metrics' unless RUBY_ENGINE == 'jruby' | ||
|
||
describe OpenTelemetry::SDK::Metrics::ConfiguratorPatch do | ||
let(:configurator) { OpenTelemetry::SDK::Configurator.new } | ||
let(:default_resource_attributes) do | ||
{ | ||
'telemetry.sdk.name' => 'opentelemetry', | ||
'telemetry.sdk.language' => 'ruby', | ||
'telemetry.sdk.version' => OpenTelemetry::SDK::VERSION, | ||
'process.pid' => Process.pid, | ||
'process.command' => $PROGRAM_NAME, | ||
'process.runtime.name' => RUBY_ENGINE, | ||
'process.runtime.version' => RUBY_VERSION, | ||
'process.runtime.description' => RUBY_DESCRIPTION, | ||
'service.name' => 'unknown_service' | ||
} | ||
end | ||
|
||
describe '#configure' do | ||
describe 'meter_provider' do | ||
it 'is an instance of SDK::Metrics::MeterProvider' do | ||
configurator.configure | ||
|
||
_(OpenTelemetry.meter_provider).must_be_instance_of( | ||
OpenTelemetry::SDK::Metrics::MeterProvider | ||
) | ||
end | ||
end | ||
|
||
describe 'metric readers' do | ||
it 'defaults to a periodic reader with an otlp exporter' do | ||
skip 'OTLP exporter not compatible with JRuby' if RUBY_ENGINE == 'jruby' | ||
|
||
configurator.configure | ||
|
||
assert_equal 1, OpenTelemetry.meter_provider.metric_readers.size | ||
reader = OpenTelemetry.meter_provider.metric_readers[0] | ||
|
||
assert_instance_of OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader, reader | ||
assert_instance_of OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter, reader.instance_variable_get(:@exporter) | ||
end | ||
|
||
it 'can be set by environment variable' do | ||
OpenTelemetry::TestHelpers.with_env('OTEL_METRICS_EXPORTER' => 'console') do | ||
configurator.configure | ||
end | ||
|
||
assert_equal 1, OpenTelemetry.meter_provider.metric_readers.size | ||
|
||
reader = OpenTelemetry.meter_provider.metric_readers[0] | ||
|
||
assert_instance_of OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader, reader | ||
assert_instance_of OpenTelemetry::SDK::Metrics::Export::ConsoleMetricPullExporter, reader.instance_variable_get(:@exporter) | ||
end | ||
|
||
it 'supports "none" as an environment variable' do | ||
OpenTelemetry::TestHelpers.with_test_logger do |log_stream| | ||
OpenTelemetry::TestHelpers.with_env('OTEL_METRICS_EXPORTER' => 'none') do | ||
configurator.configure | ||
end | ||
|
||
assert_empty OpenTelemetry.meter_provider.metric_readers | ||
|
||
refute_match(/The none exporter is unknown and cannot be configured/, log_stream.string) | ||
end | ||
end | ||
|
||
it 'supports multiple exporters passed by environment variable' do | ||
OpenTelemetry::TestHelpers.with_env('OTEL_METRICS_EXPORTER' => 'console,in-memory') do | ||
configurator.configure | ||
end | ||
|
||
assert_equal 2, OpenTelemetry.meter_provider.metric_readers.size | ||
|
||
reader1 = OpenTelemetry.meter_provider.metric_readers[0] | ||
reader2 = OpenTelemetry.meter_provider.metric_readers[1] | ||
|
||
assert_instance_of OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader, reader1 | ||
assert_instance_of OpenTelemetry::SDK::Metrics::Export::ConsoleMetricPullExporter, reader1.instance_variable_get(:@exporter) | ||
|
||
assert_instance_of OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter, reader2 | ||
end | ||
|
||
it 'defaults to noop with invalid env var' do | ||
OpenTelemetry::TestHelpers.with_test_logger do |log_stream| | ||
OpenTelemetry::TestHelpers.with_env('OTEL_METRICS_EXPORTER' => 'unladen_swallow') do | ||
configurator.configure | ||
end | ||
|
||
assert_empty OpenTelemetry.meter_provider.metric_readers | ||
assert_match(/The unladen_swallow exporter is unknown and cannot be configured/, log_stream.string) | ||
end | ||
end | ||
|
||
it 'rescues NameErrors when otlp set to env var and the library is not installed' do | ||
if RUBY_ENGINE == 'jruby' | ||
OpenTelemetry::TestHelpers.with_test_logger do |log_stream| | ||
OpenTelemetry::TestHelpers.with_env('OTEL_METRICS_EXPORTER' => 'otlp') do | ||
configurator.configure | ||
end | ||
|
||
assert_empty OpenTelemetry.meter_provider.metric_readers | ||
assert_match(/The otlp metrics exporter cannot be configured - please add opentelemetry-exporter-otlp-metrics to your Gemfile, metrics will not be exported/, log_stream.string) | ||
end | ||
else | ||
OpenTelemetry::TestHelpers.with_test_logger do |log_stream| | ||
OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.stub(:new, -> { raise NameError }) do | ||
OpenTelemetry::TestHelpers.with_env('OTEL_METRICS_EXPORTER' => 'otlp') do | ||
configurator.configure | ||
end | ||
|
||
assert_empty OpenTelemetry.meter_provider.metric_readers | ||
assert_match(/The otlp metrics exporter cannot be configured - please add opentelemetry-exporter-otlp-metrics to your Gemfile, metrics will not be exported/, log_stream.string) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters