diff --git a/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb b/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb index e43acb653..a2eda0237 100644 --- a/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb +++ b/sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb @@ -8,7 +8,7 @@ class SentryContextServerMiddleware OP_NAME = "queue.sidekiq" SPAN_ORIGIN = "auto.queue.sidekiq" - def call(_worker, job, queue) + def call(worker, job, queue) return yield unless Sentry.initialized? context_filter = Sentry::Sidekiq::ContextFilter.new(job) @@ -26,8 +26,19 @@ def call(_worker, job, queue) scope.set_span(transaction) if transaction begin - yield - rescue + Sentry.with_child_span(op: "queue.process", description: "Process #{worker.class.name}") do |span| + # Set span data + if span + span.set_data("messaging.message.id", job["jid"]) + span.set_data("messaging.destination.name", queue) + span.set_data("messaging.message.body.size", job.to_json.size) + span.set_data("messaging.message.receive.latency", ((Time.now.to_f - job["created_at"]) * 1000).to_i) + span.set_data("messaging.message.retry.count", job["retry_count"] || 0) + end + + yield + end + rescue => ex finish_transaction(transaction, 500) raise end @@ -63,13 +74,23 @@ def finish_transaction(transaction, status) end class SentryContextClientMiddleware - def call(_worker_class, job, _queue, _redis_pool) + def call(worker_class, job, queue, _redis_pool) return yield unless Sentry.initialized? user = Sentry.get_current_scope.user job["sentry_user"] = user unless user.empty? job["trace_propagation_headers"] ||= Sentry.get_trace_propagation_headers - yield + + Sentry.with_child_span(op: "queue.publish", description: "Enqueue #{worker_class}") do |span| + # Set span data + if span + span.set_data("messaging.message.id", job["jid"]) + span.set_data("messaging.destination.name", queue) + span.set_data("messaging.message.body.size", job.to_json.size) + end + + yield + end end end end diff --git a/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb b/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb index de5e9416f..b90799e49 100644 --- a/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb +++ b/sentry-sidekiq/spec/sentry/sidekiq/sentry_context_middleware_spec.rb @@ -73,6 +73,7 @@ execute_worker(processor, HappyWorker, trace_propagation_headers: trace_propagation_headers) expect(transport.events.count).to eq(1) + transaction = transport.events[0] expect(transaction).not_to be_nil expect(transaction.contexts.dig(:trace, :trace_id)).to eq(parent_transaction.trace_id) diff --git a/sentry-sidekiq/spec/spec_helper.rb b/sentry-sidekiq/spec/spec_helper.rb index 9d6b122b6..6aac1ab5a 100644 --- a/sentry-sidekiq/spec/spec_helper.rb +++ b/sentry-sidekiq/spec/spec_helper.rb @@ -231,13 +231,12 @@ def sidekiq_config(opts) def execute_worker(processor, klass, **options) klass_options = klass.sidekiq_options_hash || {} - # for Ruby < 2.6 klass_options.each do |k, v| options[k.to_sym] = v end - msg = Sidekiq.dump_json(jid: "123123", class: klass, args: [], **options) + msg = Sidekiq.dump_json(created_at: Time.now.to_f, jid: "123123", class: klass, args: [], **options) work = Sidekiq::BasicFetch::UnitOfWork.new('queue:default', msg) process_work(processor, work) end