From 71dc8e39a8c22dd90550adcabf8fd48518e67957 Mon Sep 17 00:00:00 2001 From: xuan-cao-swi Date: Wed, 4 Sep 2024 12:24:01 -0400 Subject: [PATCH] feat: metrics sdk support for schema_url and attributes --- .../lib/opentelemetry/metrics/meter_provider.rb | 2 +- metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb | 4 +++- .../opentelemetry/sdk/metrics/meter_provider.rb | 8 +++++--- .../sdk/metrics/meter_provider_test.rb | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/metrics_api/lib/opentelemetry/metrics/meter_provider.rb b/metrics_api/lib/opentelemetry/metrics/meter_provider.rb index 006f9ba03c..f442adba67 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 8db3ea992c..184f38bb84 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb @@ -19,11 +19,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 205ff5db0d..ceb0587205 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 @@ -28,15 +28,17 @@ 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 - @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 f781d613b3..8b9f3e4264 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