diff --git a/sdk/lib/opentelemetry/sdk/trace/tracer.rb b/sdk/lib/opentelemetry/sdk/trace/tracer.rb index 3fa998fea..d42d86cb8 100644 --- a/sdk/lib/opentelemetry/sdk/trace/tracer.rb +++ b/sdk/lib/opentelemetry/sdk/trace/tracer.rb @@ -11,6 +11,7 @@ module Trace class Tracer < OpenTelemetry::Trace::Tracer attr_reader :name attr_reader :version + attr_reader :tracer_provider # @api private # @@ -18,13 +19,14 @@ class Tracer < OpenTelemetry::Trace::Tracer # # @param [String] name Instrumentation package name # @param [String] version Instrumentation package version + # @param [TracerProvider] tracer_provider TracerProvider that initialized the tracer # # @return [Tracer] - def initialize(name, version) + def initialize(name, version, tracer_provider) @name = name @version = version @instrumentation_library = InstrumentationLibrary.new(name, version) - @resource = OpenTelemetry.tracer_provider.resource + @tracer_provider = tracer_provider end def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil) @@ -41,7 +43,7 @@ def start_span(name, with_parent: nil, with_parent_context: nil, attributes: nil trace_id = parent_span_context&.trace_id trace_id ||= OpenTelemetry::Trace.generate_trace_id span_id = OpenTelemetry::Trace.generate_span_id - sampler = OpenTelemetry.tracer_provider.active_trace_config.sampler + sampler = tracer_provider.active_trace_config.sampler result = sampler.call(trace_id: trace_id, span_id: span_id, parent_context: parent_span_context, links: links, name: name, kind: kind, attributes: attributes) internal_create_span(result, name, kind, trace_id, span_id, parent_span_id, attributes, links, start_timestamp, tracestate) @@ -50,13 +52,23 @@ def start_span(name, with_parent: nil, with_parent_context: nil, attributes: nil private def internal_create_span(result, name, kind, trace_id, span_id, parent_span_id, attributes, links, start_timestamp, tracestate) # rubocop:disable Metrics/AbcSize - if result.recording? && !OpenTelemetry.tracer_provider.stopped? + if result.recording? && !tracer_provider.stopped? trace_flags = result.sampled? ? OpenTelemetry::Trace::TraceFlags::SAMPLED : OpenTelemetry::Trace::TraceFlags::DEFAULT context = OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, trace_flags: trace_flags, tracestate: tracestate) attributes = attributes&.merge(result.attributes) || result.attributes - active_trace_config = OpenTelemetry.tracer_provider.active_trace_config - active_span_processor = OpenTelemetry.tracer_provider.active_span_processor - Span.new(context, name, kind, parent_span_id, active_trace_config, active_span_processor, attributes, links, start_timestamp || Time.now, @resource, @instrumentation_library) + Span.new( + context, + name, + kind, + parent_span_id, + tracer_provider.active_trace_config, + tracer_provider.active_span_processor, + attributes, + links, + start_timestamp || Time.now, + tracer_provider.resource, + @instrumentation_library + ) else OpenTelemetry::Trace::Span.new(span_context: OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id)) end diff --git a/sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb b/sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb index ce0aeb43a..8492b15c3 100644 --- a/sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb +++ b/sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb @@ -38,7 +38,7 @@ def initialize(resource = OpenTelemetry::SDK::Resources::Resource.create) def tracer(name = nil, version = nil) name ||= '' version ||= '' - @mutex.synchronize { @registry[Key.new(name, version)] ||= Tracer.new(name, version) } + @mutex.synchronize { @registry[Key.new(name, version)] ||= Tracer.new(name, version, self) } end # Attempts to stop all the activity for this {Tracer}. Calls diff --git a/sdk/test/opentelemetry/sdk/trace/tracer_test.rb b/sdk/test/opentelemetry/sdk/trace/tracer_test.rb index 43102418a..9994acc2b 100644 --- a/sdk/test/opentelemetry/sdk/trace/tracer_test.rb +++ b/sdk/test/opentelemetry/sdk/trace/tracer_test.rb @@ -20,13 +20,19 @@ describe '#name' do it 'reflects the name passed in' do - _(Tracer.new('component', 'semver:1.0').name).must_equal('component') + _(Tracer.new('component', 'semver:1.0', tracer_provider).name).must_equal('component') end end describe '#version' do it 'reflects the version passed in' do - _(Tracer.new('component', 'semver:1.0').version).must_equal('semver:1.0') + _(Tracer.new('component', 'semver:1.0', tracer_provider).version).must_equal('semver:1.0') + end + end + + describe '#tracer_provider' do + it 'reflects the tracer_provider passed in' do + _(Tracer.new('component', 'semver:1.0', tracer_provider).tracer_provider).must_equal(tracer_provider) end end