Skip to content

Commit bbfb651

Browse files
authored
Add event processor in OpenTelemetry SpanProcessor to link errors and transactions (#1983)
1 parent b73abea commit bbfb651

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
end
1616
```
1717

18+
- Add global event processor in OpenTelemetry `SpanProcessor` to link errors with transactions [#1983](https://github.com/getsentry/sentry-ruby/pull/1983)
19+
1820

1921
### Bug Fixes
2022

sentry-opentelemetry/lib/sentry/opentelemetry/span_processor.rb

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class SpanProcessor < ::OpenTelemetry::SDK::Trace::SpanProcessor
1818

1919
def initialize
2020
@span_map = {}
21+
setup_event_processor
2122
end
2223

2324
def on_start(otel_span, parent_context)
@@ -152,6 +153,19 @@ def update_span_with_otel_data(sentry_span, otel_span)
152153
sentry_span.set_op(op)
153154
sentry_span.set_description(description)
154155
end
156+
157+
def setup_event_processor
158+
Sentry.add_global_event_processor do |event, _hint|
159+
span_context = ::OpenTelemetry::Trace.current_span.context
160+
next event unless span_context.valid?
161+
162+
sentry_span = @span_map[span_context.hex_span_id]
163+
next event unless sentry_span
164+
165+
event.contexts[:trace] ||= sentry_span.get_trace_context
166+
event
167+
end
168+
end
155169
end
156170
end
157171
end

sentry-opentelemetry/spec/sentry/opentelemetry/span_processor_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@
7878
it 'raises error on instantiation' do
7979
expect { described_class.new }.to raise_error(NoMethodError)
8080
end
81+
82+
context 'global event processor' do
83+
let(:event_processor) { Sentry::Scope.global_event_processors.first }
84+
let(:event) { Sentry::Event.new(configuration: Sentry.configuration) }
85+
let(:hint) { {} }
86+
87+
before { subject.on_start(root_span, empty_context) }
88+
89+
it 'sets trace context on event' do
90+
OpenTelemetry::Context.with_current(root_parent_context) do
91+
event_processor.call(event, hint)
92+
expect(event.contexts).to include(:trace)
93+
expect(event.contexts[:trace][:trace_id]).to eq(root_span.context.hex_trace_id)
94+
expect(event.contexts[:trace][:span_id]).to eq(root_span.context.hex_span_id)
95+
end
96+
end
97+
end
8198
end
8299

83100
describe '#on_start' do

0 commit comments

Comments
 (0)