Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions lib/sagittarius/middleware/grpc/open_telemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
15 changes: 6 additions & 9 deletions spec/lib/sagittarius/middleware/grpc/open_telemetry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down