diff --git a/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/subscriber.rb b/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/subscriber.rb index 31cb85a37..36ea81e04 100644 --- a/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/subscriber.rb +++ b/instrumentation/active_job/lib/opentelemetry/instrumentation/active_job/subscriber.rb @@ -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 @@ -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