Skip to content

Commit

Permalink
squash: Use Singleton Tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin committed Nov 1, 2023
1 parent a345fa2 commit 57e956e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ module Handlers
def subscribe
return unless Array(@subscriptions).empty?

tracer = OpenTelemetry.tracer_provider.tracer(ActiveJob.name, ActiveJob::VERSION)
mapper = Mappers::Attribute.new
config = ActiveJob::Instrumentation.instance.config
parent_span_provider = OpenTelemetry::Instrumentation::ActiveJob

# TODO, use delegation instead of inheritance
default_handler = Handlers::Default.new(tracer, parent_span_provider, mapper, config)
enqueue_handler = Handlers::Enqueue.new(tracer, parent_span_provider, mapper, config)
perform_handler = Handlers::Perform.new(tracer, parent_span_provider, mapper, config)
default_handler = Handlers::Default.new(parent_span_provider, mapper, config)
enqueue_handler = Handlers::Enqueue.new(parent_span_provider, mapper, config)
perform_handler = Handlers::Perform.new(parent_span_provider, mapper, config)

handlers_by_pattern = {
'enqueue' => enqueue_handler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ module Handlers
# Default handler to create internal spans for events
# This class provides default template methods that derived classes may override to generate spans and register contexts.
class Default
# @param tracer [OpenTelemetry::Trace::Tracer] to generate spans
# @param parent_span_provider [Object] provides access to the top most parent span (usually the ingress span)
# @param mapper [Callable] converts ActiveSupport::Notifications payloads to span attributes
# @param config [Hash] of instrumentation options
def initialize(tracer, parent_span_provider, mapper, config)
@tracer = tracer
def initialize(parent_span_provider, mapper, config)
@mapper = mapper
@config = config
@parent_span_provider = parent_span_provider
Expand All @@ -42,7 +40,7 @@ def start(name, id, payload)
# @param payload [Hash] containing job run information
# @return [Hash] with the span and generated context tokens
def start_span(name, _id, payload)
span = @tracer.start_span(name, attributes: @mapper.call(payload))
span = tracer.start_span(name, attributes: @mapper.call(payload))
tokens = [OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span))]

{ span: span, ctx_tokens: tokens }
Expand Down Expand Up @@ -104,6 +102,10 @@ def on_exception(exception, span)
span&.status = status
@parent_span_provider.current_span.status = status
end

def tracer
OpenTelemetry::Instrumentation::ActiveJob::Instrumentation.instance.tracer
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(...)
# @return [Hash] with the span and generated context tokens
def start_span(name, _id, payload)
job = payload.fetch(:job)
span = @tracer.start_span(@span_name_formatter.call(job), kind: :producer, attributes: @mapper.call(payload))
span = tracer.start_span(@span_name_formatter.call(job), kind: :producer, attributes: @mapper.call(payload))
tokens = [OpenTelemetry::Context.attach(OpenTelemetry::Trace.context_with_span(span))]
OpenTelemetry.propagation.inject(job.__otel_headers) # This must be transmitted over the wire
{ span: span, ctx_tokens: tokens }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def start_span(name, _id, payload)
propagation_style = @config[:propagation_style]
if propagation_style == :child
tokens << OpenTelemetry::Context.attach(parent_context)
span = @tracer.start_span(span_name, kind: :consumer, attributes: @mapper.call(payload))
span = tracer.start_span(span_name, kind: :consumer, attributes: @mapper.call(payload))
else
span_context = OpenTelemetry::Trace.current_span(parent_context).context
links = [OpenTelemetry::Trace::Link.new(span_context)] if span_context.valid? && propagation_style == :link
span = @tracer.start_root_span(span_name, kind: :consumer, attributes: @mapper.call(payload), links: links)
span = tracer.start_root_span(span_name, kind: :consumer, attributes: @mapper.call(payload), links: links)
end

tokens.concat(attach_consumer_context(span))
Expand Down

0 comments on commit 57e956e

Please sign in to comment.