Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: metrics sdk support for schema_url and attributes #1719

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metrics_api/lib/opentelemetry/metrics/meter_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions metrics_sdk/lib/opentelemetry/sdk/metrics/meter_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading