Skip to content

Commit 0810cfd

Browse files
committed
Switch to parent context propagation in grpc server
1 parent 54d03ca commit 0810cfd

2 files changed

Lines changed: 22 additions & 28 deletions

File tree

lib/sagittarius/middleware/grpc/open_telemetry.rb

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ def execute(method:, call:, **_, &block)
2727
method_name = found_method&.to_s || '(unknown)'
2828
rpc_method = "#{service_name}/#{method_name}"
2929

30-
links = extract_remote_span_links(call)
30+
parent_context = extract_parent_context(call)
3131
attributes = build_attributes(rpc_method)
3232

33-
self.class.tracer.in_span(rpc_method, links: links, kind: :server, attributes: attributes) do |span|
34-
result = block.call
35-
record_success(span)
36-
result
37-
rescue ::GRPC::BadStatus => e
38-
record_grpc_error(span, e)
39-
raise
40-
rescue StandardError => e
41-
record_exception(span, e)
42-
raise
33+
::OpenTelemetry::Context.with_current(parent_context) do
34+
self.class.tracer.in_span(rpc_method, kind: :server, attributes: attributes) do |span|
35+
result = block.call
36+
record_success(span)
37+
result
38+
rescue ::GRPC::BadStatus => e
39+
record_grpc_error(span, e)
40+
raise
41+
rescue StandardError => e
42+
record_exception(span, e)
43+
raise
44+
end
4345
end
4446
end
4547

@@ -74,15 +76,10 @@ def record_exception(span, error)
7476
span.record_exception(error)
7577
end
7678

77-
def extract_remote_span_links(call)
78-
extracted_context = ::OpenTelemetry.propagation.extract(call.metadata)
79-
remote_span_context = ::OpenTelemetry::Trace.current_span(extracted_context).context
80-
81-
return [] unless remote_span_context.valid?
82-
83-
[::OpenTelemetry::Trace::Link.new(remote_span_context)]
79+
def extract_parent_context(call)
80+
::OpenTelemetry.propagation.extract(call.metadata)
8481
rescue StandardError
85-
[]
82+
::OpenTelemetry::Context.current
8683
end
8784
end
8885
end

spec/lib/sagittarius/middleware/grpc/open_telemetry_spec.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,29 @@ def test(_msg, _call)
128128
end
129129
end
130130

131-
describe 'span links' do
131+
describe 'parent context propagation' do
132132
context 'when valid trace context is present' do
133133
let(:metadata) do
134134
{ 'traceparent' => '00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01' }
135135
end
136136

137-
it 'passes a link to the remote span context' do
137+
it 'sets the remote span as parent' do
138138
# rubocop:disable Lint/EmptyBlock -- the block is part of the api and needs to be given
139139
interceptor.request_response(request: request, call: call, method: method) {}
140140
# rubocop:enable Lint/EmptyBlock
141141

142-
expect(span.links.size).to eq(1)
143-
144-
link = span.links.first
145-
expect(link.span_context.hex_trace_id).to eq('0af7651916cd43dd8448eb211c80319c')
146-
expect(link.span_context.hex_span_id).to eq('b7ad6b7169203331')
142+
expect(span.hex_trace_id).to eq('0af7651916cd43dd8448eb211c80319c')
143+
expect(span.hex_parent_span_id).to eq('b7ad6b7169203331')
147144
end
148145
end
149146

150147
context 'when no trace context is present' do
151-
it 'passes no links' do
148+
it 'creates a new root span' do
152149
# rubocop:disable Lint/EmptyBlock -- the block is part of the api and needs to be given
153150
interceptor.request_response(request: request, call: call, method: method) {}
154151
# rubocop:enable Lint/EmptyBlock
155152

156-
expect(span.links).to be_nil.or be_empty
153+
expect(span.hex_parent_span_id).to eq('0000000000000000')
157154
end
158155
end
159156
end

0 commit comments

Comments
 (0)