From 94b71b0f783bead06b8fef845201c8cf7cdd72ec Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Tue, 29 Oct 2024 12:24:06 -0700 Subject: [PATCH] test: Update tests for JRuby --- metrics_sdk/Gemfile | 2 +- .../sdk/metrics/configuration_patch.rb | 2 ++ .../sdk/metrics/configuration_patch_test.rb | 26 ++++++++++++++----- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/metrics_sdk/Gemfile b/metrics_sdk/Gemfile index 02b533208..c4b8797f4 100644 --- a/metrics_sdk/Gemfile +++ b/metrics_sdk/Gemfile @@ -16,7 +16,7 @@ gem 'opentelemetry-sdk', path: '../sdk' gem 'opentelemetry-test-helpers', path: '../test_helpers' group :test, :development do - gem 'opentelemetry-exporter-otlp-metrics', path: '../exporter/otlp-metrics' + gem 'opentelemetry-exporter-otlp-metrics', path: '../exporter/otlp-metrics' unless RUBY_ENGINE == 'jruby' gem 'pry' gem 'pry-byebug' unless RUBY_ENGINE == 'jruby' end diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/configuration_patch.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/configuration_patch.rb index 71deca294..cc163187e 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/configuration_patch.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/configuration_patch.rb @@ -43,6 +43,8 @@ def wrapped_metric_exporters_from_env exporter: Metrics::Export::ConsoleMetricPullExporter.new ) ) + when 'in-memory' + OpenTelemetry.meter_provider.add_metric_reader(Metrics::Export::InMemoryMetricPullExporter.new) when 'otlp' begin OpenTelemetry.meter_provider.add_metric_reader( diff --git a/metrics_sdk/test/opentelemetry/sdk/metrics/configuration_patch_test.rb b/metrics_sdk/test/opentelemetry/sdk/metrics/configuration_patch_test.rb index cf3b6ec21..88cd4bdaa 100644 --- a/metrics_sdk/test/opentelemetry/sdk/metrics/configuration_patch_test.rb +++ b/metrics_sdk/test/opentelemetry/sdk/metrics/configuration_patch_test.rb @@ -5,7 +5,7 @@ # SPDX-License-Identifier: Apache-2.0 require 'test_helper' -require 'opentelemetry-exporter-otlp-metrics' +require 'opentelemetry-exporter-otlp-metrics' unless RUBY_ENGINE == 'jruby' describe OpenTelemetry::SDK::Metrics::ConfiguratorPatch do let(:configurator) { OpenTelemetry::SDK::Configurator.new } @@ -36,6 +36,8 @@ 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 @@ -71,7 +73,7 @@ end it 'supports multiple exporters passed by environment variable' do - OpenTelemetry::TestHelpers.with_env('OTEL_METRICS_EXPORTER' => 'otlp,console') do + OpenTelemetry::TestHelpers.with_env('OTEL_METRICS_EXPORTER' => 'console,in-memory') do configurator.configure end @@ -81,10 +83,9 @@ reader2 = OpenTelemetry.meter_provider.metric_readers[1] assert_instance_of OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader, reader1 - assert_instance_of OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter, reader1.instance_variable_get(:@exporter) + assert_instance_of OpenTelemetry::SDK::Metrics::Export::ConsoleMetricPullExporter, reader1.instance_variable_get(:@exporter) - assert_instance_of OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader, reader2 - assert_instance_of OpenTelemetry::SDK::Metrics::Export::ConsoleMetricPullExporter, reader2.instance_variable_get(:@exporter) + assert_instance_of OpenTelemetry::SDK::Metrics::Export::InMemoryMetricPullExporter, reader2 end it 'defaults to noop with invalid env var' do @@ -99,8 +100,8 @@ end it 'rescues NameErrors when otlp set to env var and the library is not installed' do - OpenTelemetry::TestHelpers.with_test_logger do |log_stream| - OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.stub(:new, -> { raise NameError }) 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 @@ -108,6 +109,17 @@ 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