From 9d3e8f164f842b175a3d6e85f09d340e906bb82a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=80=D0=B0=D1=81=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=94=D0=B5=D0=BD=D0=B8=D1=81=20=D0=90=D0=BD=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B5=D0=B2=D0=B8=D1=87?= Date: Wed, 25 Sep 2024 19:59:44 +0000 Subject: [PATCH] [DEX-2551] feat: write otel trace_id to outgoing message logs --- CHANGELOG.md | 6 ++++++ .../open_telemetry/tracing_item_process_middleware.rb | 10 +++++++++- .../sentry/tracing_batch_process_middleware.rb | 1 - lib/sbmt/outbox/version.rb | 2 +- .../tracing_item_process_middleware_spec.rb | 4 +++- .../sentry/tracing_batch_process_middleware_spec.rb | 4 +--- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33ba154..7dad1b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware.rb b/lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware.rb index 0160c11..f162d39 100644 --- a/lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware.rb +++ b/lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware.rb @@ -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 @@ -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 diff --git a/lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware.rb b/lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware.rb index 8672bae..7ebb68c 100644 --- a/lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware.rb +++ b/lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware.rb @@ -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 diff --git a/lib/sbmt/outbox/version.rb b/lib/sbmt/outbox/version.rb index 27386fa..435d61a 100644 --- a/lib/sbmt/outbox/version.rb +++ b/lib/sbmt/outbox/version.rb @@ -2,6 +2,6 @@ module Sbmt module Outbox - VERSION = "6.10.0" + VERSION = "6.10.1" end end diff --git a/spec/lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware_spec.rb b/spec/lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware_spec.rb index 9257339..8ba2871 100644 --- a/spec/lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware_spec.rb +++ b/spec/lib/sbmt/outbox/middleware/open_telemetry/tracing_item_process_middleware_spec.rb @@ -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", @@ -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 diff --git a/spec/lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware_spec.rb b/spec/lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware_spec.rb index 058b8a0..7591397 100644 --- a/spec/lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware_spec.rb +++ b/spec/lib/sbmt/outbox/middleware/sentry/tracing_batch_process_middleware_spec.rb @@ -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) @@ -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