Skip to content

Commit 93467c2

Browse files
committed
Merge branch 'feat/DEX-2551/write-oteltrace_id-to-outgoing-message-logs' into 'master'
[DEX-2551] feat: write otel trace_id to outgoing message logs Closes DEX-2551 See merge request nstmrt/rubygems/outbox!102
2 parents 00f2285 + 9d3e8f1 commit 93467c2

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313

1414
### Fixed
1515

16+
## [6.10.1] - 2024-09-23
17+
18+
### Fixed
19+
20+
- log OTEL `trace_id`
21+
1622
## [6.10.0] - 2024-09-19
1723

1824
### Changed

lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def call(item)
2828
extracted_context = ::OpenTelemetry.propagation.extract(item_options_headers)
2929
::OpenTelemetry::Context.with_current(extracted_context) do
3030
tracer.in_span(span_name(item_class), attributes: span_attributes.compact, kind: :consumer) do
31-
yield
31+
Sbmt::Outbox.logger.with_tags(trace_id: trace_id) do
32+
yield
33+
end
3234
end
3335
end
3436
end
@@ -42,6 +44,12 @@ def tracer
4244
def span_name(item_class)
4345
"#{item_class.box_type}/#{item_class.box_name} process item"
4446
end
47+
48+
def trace_id
49+
context = ::OpenTelemetry::Trace.current_span.context
50+
51+
context.valid? ? context.hex_trace_id : nil
52+
end
4553
end
4654
end
4755
end

lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def call(job)
1616

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

2120
begin
2221
yield

lib/sbmt/outbox/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Sbmt
44
module Outbox
5-
VERSION = "6.10.0"
5+
VERSION = "6.10.1"
66
end
77
end

spec/lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
before do
1010
allow(::Sbmt::Outbox::Instrumentation::OpenTelemetryLoader).to receive(:instance).and_return(instrumentation_instance)
1111
allow(instrumentation_instance).to receive(:tracer).and_return(tracer)
12+
allow(OpenTelemetry::Trace).to receive(:current_span).and_return(double(context: double(valid?: true, hex_trace_id: "trace-id"))) # rubocop:disable RSpec/VerifiedDoubles
1213
end
1314

1415
describe ".call" do
15-
it "injects context into message headers" do
16+
it "injects context into message headers and logs the trace_id" do
1617
expect(tracer).to receive(:in_span).with("inbox/inbox_item process item", kind: :consumer, attributes: {
1718
"messaging.destination" => "InboxItem",
1819
"messaging.destination_kind" => "database",
@@ -23,6 +24,7 @@
2324
"messaging.system" => "outbox"
2425
}).and_yield
2526
expect(::OpenTelemetry.propagation).to receive(:extract).with(a_hash_including(headers))
27+
expect(Sbmt::Outbox.logger).to receive(:with_tags).with(hash_including(trace_id: "trace-id")).once
2628
described_class.new.call(inbox_item) {}
2729
end
2830
end

spec/lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
describe Sbmt::Outbox::Middleware::Sentry::TracingBatchProcessMiddleware do
66
let(:job) { OpenStruct.new(log_tags: {}) }
77
let(:scope) { double("scope") } # rubocop:disable RSpec/VerifiedDoubles
8-
let(:trace_id) { "trace-id" }
98

109
it "skips tracing if sentry is not initialized" do
1110
expect(::Sentry).to receive(:initialized?).and_return(false)
@@ -17,8 +16,7 @@
1716
it "sets up sentry transaction" do
1817
expect(::Sentry).to receive(:initialized?).and_return(true)
1918
expect(::Sentry).to receive(:get_current_scope).and_return(scope)
20-
expect(scope).to receive(:tags).and_return({trace_id: trace_id})
21-
expect(scope).to receive(:set_tags).with hash_including(:trace_id)
19+
expect(scope).to receive(:set_tags)
2220
expect(::Sentry).to receive(:start_transaction)
2321

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

0 commit comments

Comments
 (0)