From 0810cfdbf7a06cfb92a6ba6ff7f853a53559f2ee Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Mon, 20 Jul 2026 21:13:07 +0200 Subject: [PATCH] Switch to parent context propagation in grpc server --- .../middleware/grpc/open_telemetry.rb | 35 +++++++++---------- .../middleware/grpc/open_telemetry_spec.rb | 15 ++++---- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/lib/sagittarius/middleware/grpc/open_telemetry.rb b/lib/sagittarius/middleware/grpc/open_telemetry.rb index b4a0f3f5..1bd471ed 100644 --- a/lib/sagittarius/middleware/grpc/open_telemetry.rb +++ b/lib/sagittarius/middleware/grpc/open_telemetry.rb @@ -27,19 +27,21 @@ def execute(method:, call:, **_, &block) method_name = found_method&.to_s || '(unknown)' rpc_method = "#{service_name}/#{method_name}" - links = extract_remote_span_links(call) + parent_context = extract_parent_context(call) attributes = build_attributes(rpc_method) - self.class.tracer.in_span(rpc_method, links: links, kind: :server, attributes: attributes) do |span| - result = block.call - record_success(span) - result - rescue ::GRPC::BadStatus => e - record_grpc_error(span, e) - raise - rescue StandardError => e - record_exception(span, e) - raise + ::OpenTelemetry::Context.with_current(parent_context) do + self.class.tracer.in_span(rpc_method, kind: :server, attributes: attributes) do |span| + result = block.call + record_success(span) + result + rescue ::GRPC::BadStatus => e + record_grpc_error(span, e) + raise + rescue StandardError => e + record_exception(span, e) + raise + end end end @@ -74,15 +76,10 @@ def record_exception(span, error) span.record_exception(error) end - def extract_remote_span_links(call) - extracted_context = ::OpenTelemetry.propagation.extract(call.metadata) - remote_span_context = ::OpenTelemetry::Trace.current_span(extracted_context).context - - return [] unless remote_span_context.valid? - - [::OpenTelemetry::Trace::Link.new(remote_span_context)] + def extract_parent_context(call) + ::OpenTelemetry.propagation.extract(call.metadata) rescue StandardError - [] + ::OpenTelemetry::Context.current end end end diff --git a/spec/lib/sagittarius/middleware/grpc/open_telemetry_spec.rb b/spec/lib/sagittarius/middleware/grpc/open_telemetry_spec.rb index efe5a23a..1363100e 100644 --- a/spec/lib/sagittarius/middleware/grpc/open_telemetry_spec.rb +++ b/spec/lib/sagittarius/middleware/grpc/open_telemetry_spec.rb @@ -128,32 +128,29 @@ def test(_msg, _call) end end - describe 'span links' do + describe 'parent context propagation' do context 'when valid trace context is present' do let(:metadata) do { 'traceparent' => '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01' } end - it 'passes a link to the remote span context' do + it 'sets the remote span as parent' do # rubocop:disable Lint/EmptyBlock -- the block is part of the api and needs to be given interceptor.request_response(request: request, call: call, method: method) {} # rubocop:enable Lint/EmptyBlock - expect(span.links.size).to eq(1) - - link = span.links.first - expect(link.span_context.hex_trace_id).to eq('0af7651916cd43dd8448eb211c80319c') - expect(link.span_context.hex_span_id).to eq('b7ad6b7169203331') + expect(span.hex_trace_id).to eq('0af7651916cd43dd8448eb211c80319c') + expect(span.hex_parent_span_id).to eq('b7ad6b7169203331') end end context 'when no trace context is present' do - it 'passes no links' do + it 'creates a new root span' do # rubocop:disable Lint/EmptyBlock -- the block is part of the api and needs to be given interceptor.request_response(request: request, call: call, method: method) {} # rubocop:enable Lint/EmptyBlock - expect(span.links).to be_nil.or be_empty + expect(span.hex_parent_span_id).to eq('0000000000000000') end end end