From d2318b0da04dfea617ff52911ddc6deb760bfe00 Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Mon, 16 Sep 2024 16:24:57 -0700 Subject: [PATCH] feat: Use an empty frozen hash as default advice --- metrics_api/lib/opentelemetry/internal/proxy_instrument.rb | 2 +- metrics_api/lib/opentelemetry/metrics/meter.rb | 6 ++++-- .../lib/opentelemetry/sdk/metrics/instrument/histogram.rb | 2 +- .../sdk/metrics/instrument/synchronous_instrument.rb | 2 +- metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb | 2 +- metrics_sdk/test/opentelemetry/sdk/metrics/meter_test.rb | 2 +- 6 files changed, 9 insertions(+), 7 deletions(-) diff --git a/metrics_api/lib/opentelemetry/internal/proxy_instrument.rb b/metrics_api/lib/opentelemetry/internal/proxy_instrument.rb index 7b1718c9e7..d6b38c22c0 100644 --- a/metrics_api/lib/opentelemetry/internal/proxy_instrument.rb +++ b/metrics_api/lib/opentelemetry/internal/proxy_instrument.rb @@ -8,7 +8,7 @@ module OpenTelemetry module Internal # @api private class ProxyInstrument - def initialize(kind, name, unit, desc, callable, advice = nil) + def initialize(kind, name, unit, desc, callable, advice = Metrics::Meter::EMPTY_ADVICE) @kind = kind @name = name @unit = unit diff --git a/metrics_api/lib/opentelemetry/metrics/meter.rb b/metrics_api/lib/opentelemetry/metrics/meter.rb index f32216a955..b07e04c72e 100644 --- a/metrics_api/lib/opentelemetry/metrics/meter.rb +++ b/metrics_api/lib/opentelemetry/metrics/meter.rb @@ -15,6 +15,8 @@ class Meter UP_DOWN_COUNTER = Instrument::UpDownCounter.new OBSERVABLE_UP_DOWN_COUNTER = Instrument::ObservableUpDownCounter.new + EMPTY_ADVICE = {}.freeze + NAME_REGEX = /\A[a-zA-Z][-.\w]{0,62}\z/ private_constant(:COUNTER, :OBSERVABLE_COUNTER, :HISTOGRAM, :OBSERVABLE_GAUGE, :UP_DOWN_COUNTER, :OBSERVABLE_UP_DOWN_COUNTER) @@ -33,7 +35,7 @@ def create_counter(name, unit: nil, description: nil) create_instrument(:counter, name, unit, description, nil) { COUNTER } end - def create_histogram(name, unit: nil, description: nil, advice: nil) + def create_histogram(name, unit: nil, description: nil, advice: EMPTY_ADVICE) create_instrument(:histogram, name, unit, description, nil, advice) { HISTOGRAM } end @@ -55,7 +57,7 @@ def create_observable_up_down_counter(name, callback:, unit: nil, description: n private - def create_instrument(kind, name, unit, description, callback, advice = nil) + def create_instrument(kind, name, unit, description, callback, advice = EMPTY_ADVICE) raise InstrumentNameError if name.nil? raise InstrumentNameError if name.empty? raise InstrumentNameError unless NAME_REGEX.match?(name) diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/histogram.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/histogram.rb index 828f02a8ee..92e839a38e 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/histogram.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/histogram.rb @@ -10,7 +10,7 @@ module Metrics module Instrument # {Histogram} is the SDK implementation of {OpenTelemetry::Metrics::Histogram}. class Histogram < OpenTelemetry::SDK::Metrics::Instrument::SynchronousInstrument - def initialize(name, unit, description, instrumentation_scope, meter_provider, advice = nil) + def initialize(name, unit, description, instrumentation_scope, meter_provider, advice = OpenTelemetry::Metrics::Meter::EMPTY_ADVICE) @advice = advice super diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/synchronous_instrument.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/synchronous_instrument.rb index 31daf19f37..db5905afb1 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/synchronous_instrument.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/instrument/synchronous_instrument.rb @@ -11,7 +11,7 @@ module Instrument # {SynchronousInstrument} contains the common functionality shared across # the synchronous instruments SDK instruments. class SynchronousInstrument - def initialize(name, unit, description, instrumentation_scope, meter_provider, advice = nil) + def initialize(name, unit, description, instrumentation_scope, meter_provider, advice = OpenTelemetry::Metrics::Meter::EMPTY_ADVICE) @name = name @unit = unit @description = description diff --git a/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb b/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb index ed1a002fcc..d0b9a904cb 100644 --- a/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb +++ b/metrics_sdk/lib/opentelemetry/sdk/metrics/meter.rb @@ -33,7 +33,7 @@ def add_metric_reader(metric_reader) end end - def create_instrument(kind, name, unit, description, callback, advice = nil) + def create_instrument(kind, name, unit, description, callback, advice = OpenTelemetry::Metrics::Meter::EMPTY_ADVICE) super do case kind when :counter then OpenTelemetry::SDK::Metrics::Instrument::Counter.new(name, unit, description, @instrumentation_scope, @meter_provider) diff --git a/metrics_sdk/test/opentelemetry/sdk/metrics/meter_test.rb b/metrics_sdk/test/opentelemetry/sdk/metrics/meter_test.rb index 31850eb2e9..7888e7e073 100644 --- a/metrics_sdk/test/opentelemetry/sdk/metrics/meter_test.rb +++ b/metrics_sdk/test/opentelemetry/sdk/metrics/meter_test.rb @@ -34,7 +34,7 @@ it 'does not require advice' do instrument = meter.create_histogram('histogram', description: 'stuff', unit: 'things') - assert_nil instrument.instance_variable_get(:@advice) + assert_equal({}, instrument.instance_variable_get(:@advice)) end end