Skip to content

Commit

Permalink
Merge branch 'feat/DEX-2551/write-oteltrace_id-to-outgoing-message-lo…
Browse files Browse the repository at this point in the history
…gs' into 'master'

[DEX-2551] feat: write otel trace_id to outgoing message logs

Closes DEX-2551

See merge request nstmrt/rubygems/outbox!102
  • Loading branch information
Arlantir committed Sep 25, 2024
2 parents 00f2285 + 9d3e8f1 commit 93467c2
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

## [6.10.1] - 2024-09-23

### Fixed

- log OTEL `trace_id`

## [6.10.0] - 2024-09-19

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def call(item)
extracted_context = ::OpenTelemetry.propagation.extract(item_options_headers)
::OpenTelemetry::Context.with_current(extracted_context) do
tracer.in_span(span_name(item_class), attributes: span_attributes.compact, kind: :consumer) do
yield
Sbmt::Outbox.logger.with_tags(trace_id: trace_id) do
yield
end
end
end
end
Expand All @@ -42,6 +44,12 @@ def tracer
def span_name(item_class)
"#{item_class.box_type}/#{item_class.box_name} process item"
end

def trace_id
context = ::OpenTelemetry::Trace.current_span.context

context.valid? ? context.hex_trace_id : nil
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def call(job)

# transaction will be nil if sentry tracing is not enabled
transaction = start_transaction(scope, job)
job.log_tags[:trace_id] = scope&.tags&.[](:trace_id)

begin
yield
Expand Down
2 changes: 1 addition & 1 deletion lib/sbmt/outbox/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Sbmt
module Outbox
VERSION = "6.10.0"
VERSION = "6.10.1"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
before do
allow(::Sbmt::Outbox::Instrumentation::OpenTelemetryLoader).to receive(:instance).and_return(instrumentation_instance)
allow(instrumentation_instance).to receive(:tracer).and_return(tracer)
allow(OpenTelemetry::Trace).to receive(:current_span).and_return(double(context: double(valid?: true, hex_trace_id: "trace-id"))) # rubocop:disable RSpec/VerifiedDoubles
end

describe ".call" do
it "injects context into message headers" do
it "injects context into message headers and logs the trace_id" do
expect(tracer).to receive(:in_span).with("inbox/inbox_item process item", kind: :consumer, attributes: {
"messaging.destination" => "InboxItem",
"messaging.destination_kind" => "database",
Expand All @@ -23,6 +24,7 @@
"messaging.system" => "outbox"
}).and_yield
expect(::OpenTelemetry.propagation).to receive(:extract).with(a_hash_including(headers))
expect(Sbmt::Outbox.logger).to receive(:with_tags).with(hash_including(trace_id: "trace-id")).once
described_class.new.call(inbox_item) {}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
describe Sbmt::Outbox::Middleware::Sentry::TracingBatchProcessMiddleware do
let(:job) { OpenStruct.new(log_tags: {}) }
let(:scope) { double("scope") } # rubocop:disable RSpec/VerifiedDoubles
let(:trace_id) { "trace-id" }

it "skips tracing if sentry is not initialized" do
expect(::Sentry).to receive(:initialized?).and_return(false)
Expand All @@ -17,8 +16,7 @@
it "sets up sentry transaction" do
expect(::Sentry).to receive(:initialized?).and_return(true)
expect(::Sentry).to receive(:get_current_scope).and_return(scope)
expect(scope).to receive(:tags).and_return({trace_id: trace_id})
expect(scope).to receive(:set_tags).with hash_including(:trace_id)
expect(scope).to receive(:set_tags)
expect(::Sentry).to receive(:start_transaction)

expect { described_class.new.call(job) {} }.not_to raise_error
Expand Down

0 comments on commit 93467c2

Please sign in to comment.