Skip to content

Commit

Permalink
Only set for spans present
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikspang committed Sep 16, 2024
1 parent f63653d commit cd35fcf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
16 changes: 7 additions & 9 deletions sentry-sidekiq/lib/sentry/sidekiq/sentry_context_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ def call(worker, job, queue)
begin
Sentry.with_child_span(op: "queue.process", description: "Process #{worker.class.name}") do |span|
# Set span data
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)
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
rescue
span.set_status("internal_error")
raise
end
rescue => ex
p ex
finish_transaction(transaction, 500)
raise
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,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)
Expand Down
3 changes: 1 addition & 2 deletions sentry-sidekiq/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,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
Expand Down

0 comments on commit cd35fcf

Please sign in to comment.