Skip to content

Commit

Permalink
refactor: try to make sense of a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
arielvalentin committed Oct 8, 2023
1 parent f1253bc commit a39ce1b
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ class PerformHandler < DefaultHandler
def on_start(name, _id, payload)
tokens = []
parent_context = OpenTelemetry.propagation.extract(payload.fetch(:job).__otel_headers)
span_context = OpenTelemetry::Trace.current_span(parent_context).context

otel_config = ActiveJob::Instrumentation.instance.config
span_name = "#{otel_config[:span_naming] == :job_class ? payload.fetch(:job).class.name : payload.fetch(:job).queue_name} process"
span_name = span_name_from(payload)

# TODO: Refactor into a propagation strategy
propagation_style = otel_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))
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)
end
Expand All @@ -138,6 +138,15 @@ def attach_consumer_context(span)

[consumer_context, aj_context].map { |context| OpenTelemetry::Context.attach(context) }
end

# TODO: refactor into a strategy
def span_name_from(payload)
"#{otel_config[:span_naming] == :job_class ? payload.fetch(:job).class.name : payload.fetch(:job).queue_name} process"
end

def otel_config
ActiveJob::Instrumentation.instance.config
end
end

# Custom subscriber that handles ActiveJob notifications
Expand Down

0 comments on commit a39ce1b

Please sign in to comment.