diff --git a/metrics_api/lib/opentelemetry/metrics/meter_provider.rb b/metrics_api/lib/opentelemetry/metrics/meter_provider.rb index 006f9ba03..f442adba6 100644 --- a/metrics_api/lib/opentelemetry/metrics/meter_provider.rb +++ b/metrics_api/lib/opentelemetry/metrics/meter_provider.rb @@ -14,7 +14,7 @@ class MeterProvider # @param [optional String] version Instrumentation package version # # @return [Meter] - def meter(name, version: nil) + def meter(name, version: nil, schema_url: nil, attributes: {}) @meter ||= Meter.new end end diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb index 1307817ec..b07398030 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb @@ -21,11 +21,13 @@ class Meter < OpenTelemetry::Metrics::Meter # @param [String] version Instrumentation package version # # @return [Meter] - def initialize(name, version, meter_provider) + def initialize(name, version, schema_url, attributes, meter_provider) @mutex = Mutex.new @instrument_registry = {} @instrumentation_scope = InstrumentationScope.new(name, version) @meter_provider = meter_provider + @schema_url = schema_url + @attributes = attributes end # @api private diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/meter_provider.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/meter_provider.rb index 4538a88db..c27baa669 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/meter_provider.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/meter_provider.rb @@ -11,7 +11,7 @@ module SDK module Metrics # {MeterProvider} is the SDK implementation of {OpenTelemetry::Metrics::MeterProvider}. class MeterProvider < OpenTelemetry::Metrics::MeterProvider - Key = Struct.new(:name, :version) + Key = Struct.new(:name, :version, :schema_url, :attributes) private_constant(:Key) attr_reader :resource, :metric_readers, :registered_views @@ -29,16 +29,18 @@ def initialize(resource: OpenTelemetry::SDK::Resources::Resource.create) # # @param [String] name Instrumentation package name # @param [optional String] version Instrumentation package version + # @param [optional String] schema_url Schema URL that should be recorded in the emitted telemetry + # @param [optional Hash] attributes Instrumentation scope attributes to associate with emitted telemetry # # @return [Meter] - def meter(name, version: nil) + def meter(name, version: nil, schema_url: nil, attributes: {}) version ||= '' if @stopped OpenTelemetry.logger.warn 'calling MeterProvider#meter after shutdown, a noop meter will be returned.' OpenTelemetry::Metrics::Meter.new else OpenTelemetry.logger.warn "Invalid meter name provided: #{name.nil? ? 'nil' : 'empty'} value" if name.to_s.empty? - @mutex.synchronize { @meter_registry[Key.new(name, version)] ||= Meter.new(name, version, self) } + @mutex.synchronize { @meter_registry[Key.new(name, version, schema_url, attributes)] ||= Meter.new(name, version, schema_url, attributes, self) } 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 a82513a3c..d513e4190 100644 --- a/metrics_sdk/test/opentelemetry/sdk/metrics/meter_provider_test.rb +++ b/metrics_sdk/test/opentelemetry/sdk/metrics/meter_provider_test.rb @@ -29,6 +29,20 @@ _(meter_a).must_equal(meter_b) end + + it 'repeated calls does not recreate a meter of the same name, schema_url and attributes' do + meter_a = OpenTelemetry.meter_provider.meter('test', schema_url: 'http//:otel/v0.1', attributes: { foo: 'bar' }) + meter_b = OpenTelemetry.meter_provider.meter('test', schema_url: 'http//:otel/v0.1', attributes: { foo: 'bar' }) + + _(meter_a).must_equal(meter_b) + end + + it 'repeated calls does create a new meter of the same name, different schema_url' do + meter_a = OpenTelemetry.meter_provider.meter('test', schema_url: 'http//:otel/v0.1') + meter_b = OpenTelemetry.meter_provider.meter('test', schema_url: 'http//:otel/v0.2') + + _(meter_a).wont_equal(meter_b) + end end describe '#shutdown' do