Skip to content

Commit

Permalink
Add tracer_provider reference to tracer
Browse files Browse the repository at this point in the history
The tracer carries a reference to the tracer_provider that initialized it to
begin supporting multiple tracer providers.
  • Loading branch information
robertlaurin committed Jun 17, 2020
1 parent cf02a49 commit a43ce3a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
26 changes: 19 additions & 7 deletions sdk/lib/opentelemetry/sdk/trace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ module Trace
class Tracer < OpenTelemetry::Trace::Tracer
attr_reader :name
attr_reader :version
attr_reader :tracer_provider

# @api private
#
# Returns a new {Tracer} instance.
#
# @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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions sdk/test/opentelemetry/sdk/trace/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a43ce3a

Please sign in to comment.