diff --git a/instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb b/instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb index c15f70047e..156bd6166f 100644 --- a/instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb +++ b/instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb @@ -24,14 +24,25 @@ def call(_worker_class, job, _queue, _redis_pool) } attributes[SemanticConventions::Trace::PEER_SERVICE] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service] + scheduled_at = job['at'] + op = scheduled_at.nil? ? 'publish' : 'scheduled' + span_name = case instrumentation_config[:span_naming] - when :job_class then "#{job['wrapped']&.to_s || job['class']} publish" - else "#{job['queue']} publish" + when :job_class then "#{job['wrapped']&.to_s || job['class']} #{op}" + else "#{job['queue']} #{op}" end - tracer.in_span(span_name, attributes: attributes, kind: :producer) do |span| + # In case this is Scheduled job, there is already context injected, so link to that context + # NOTE: :propagation_style = :child is not supported as it is quite tricky when :trace_poller_enqueue = true + extracted_context = OpenTelemetry.propagation.extract(job, context: OpenTelemetry::Context::ROOT) + links = [] + span_context = OpenTelemetry::Trace.current_span(extracted_context).context + links << OpenTelemetry::Trace::Link.new(span_context) if instrumentation_config[:propagation_style] == :link && span_context.valid? + + tracer.in_span(span_name, attributes: attributes, links: links, kind: :producer) do |span| OpenTelemetry.propagation.inject(job) span.add_event('created_at', timestamp: time_from_timestamp(job['created_at'])) + span.add_event('scheduled_at', timestamp: time_from_timestamp(scheduled_at)) unless scheduled_at.nil? yield end end diff --git a/instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware.rb b/instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware.rb index 47c3d7dce7..c28f7b8803 100644 --- a/instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware.rb +++ b/instrumentation/sidekiq/lib/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware.rb @@ -32,12 +32,14 @@ def call(_worker, msg, _queue) extracted_context = OpenTelemetry.propagation.extract(msg) created_at = time_from_timestamp(msg['created_at']) - enqueued_at = time_from_timestamp(msg['created_at']) + enqueued_at = time_from_timestamp(msg['enqueued_at']) + scheduled_at = time_from_timestamp(msg['at']) unless msg['at'].nil? OpenTelemetry::Context.with_current(extracted_context) do if instrumentation_config[:propagation_style] == :child tracer.in_span(span_name, attributes: attributes, kind: :consumer) do |span| span.add_event('created_at', timestamp: created_at) span.add_event('enqueued_at', timestamp: enqueued_at) + span.add_event('scheduled_at', timestamp: scheduled_at) unless scheduled_at.nil? yield end else @@ -48,6 +50,7 @@ def call(_worker, msg, _queue) OpenTelemetry::Trace.with_span(span) do span.add_event('created_at', timestamp: created_at) span.add_event('enqueued_at', timestamp: enqueued_at) + span.add_event('scheduled_at', timestamp: scheduled_at) unless scheduled_at.nil? yield rescue Exception => e # rubocop:disable Lint/RescueException span.record_exception(e) diff --git a/instrumentation/sidekiq/test/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware_test.rb b/instrumentation/sidekiq/test/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware_test.rb index 5c770bb78c..0ea036ee38 100644 --- a/instrumentation/sidekiq/test/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware_test.rb +++ b/instrumentation/sidekiq/test/opentelemetry/instrumentation/sidekiq/middlewares/server/tracer_middleware_test.rb @@ -133,7 +133,7 @@ _(root_span.kind).must_equal :producer # process span is linked to the root enqueuing job - child_span1 = spans.find { |s| s.links && s.links.first.span_context.span_id == root_span.span_id } + child_span1 = spans.find { |s| s.links&.first && s.links.first.span_context.span_id == root_span.span_id } _(child_span1.name).must_equal 'default process' _(child_span1.kind).must_equal :consumer @@ -143,7 +143,7 @@ _(child_span2.kind).must_equal :producer # last process job is linked back to the process job that enqueued it - child_span3 = spans.find { |s| s.links && s.links.first.span_context.span_id == child_span2.span_id } + child_span3 = spans.find { |s| s.links&.first && s.links.first.span_context.span_id == child_span2.span_id } _(child_span3.name).must_equal 'default process' _(child_span3.kind).must_equal :consumer end