Skip to content

Commit b6f9dce

Browse files
authored
feat: include otel as custom sampling contex (#2683)
* feat: include otel as custom sampling contex Sentry's opentelemetry adapter does not do the greatest job in adapting the opentelemetry span to a semantically correct sentry span, nor does it expose the opentelemetry span in the context for sampling decisions. This PR adds a opentelemetry span as an additional custom sampling context so it can be used for sampling decisions. This gives clients more opportunity to use the entire context of the opentelemetry span for sampling decisions, and allows replicating sampling mechanisms as orignally suggested by the Sentry team via the "Sampling Function". https://docs.sentry.io/platforms/ruby/configuration/sampling/#setting-a-sampling-function * chore: exercise otel context with tracer_sampler
1 parent 3db724c commit b6f9dce

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,16 @@ def on_start(otel_span, parent_context)
4848
parent_sampled: trace_data.parent_sampled,
4949
baggage: trace_data.baggage,
5050
start_timestamp: otel_span.start_timestamp / 1e9,
51-
origin: SPAN_ORIGIN
51+
origin: SPAN_ORIGIN,
52+
custom_sampling_context: {
53+
otel: otel_context_hash(otel_span).merge(
54+
kind: otel_span.kind,
55+
instrumentation_scope: {
56+
name: otel_span.instrumentation_scope.name,
57+
version: otel_span.instrumentation_scope.version
58+
}
59+
)
60+
}
5261
}
5362

5463
Sentry.start_transaction(**options)

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154

155155
it 'starts a sentry transaction on otel root span' do
156156
expect(Sentry).to receive(:start_transaction).and_call_original
157+
157158
subject.on_start(root_span, empty_context)
158159

159160
span_id = root_span.context.hex_span_id
@@ -175,6 +176,32 @@
175176
expect(transaction.baggage).to eq(nil)
176177
end
177178

179+
it 'includes otel context in custom sampling context' do
180+
sampling_context = nil
181+
Sentry.configuration.traces_sampler = lambda do |context|
182+
sampling_context = context
183+
0.5
184+
end
185+
186+
subject.on_start(root_span, empty_context)
187+
188+
expect(sampling_context).to include(:otel)
189+
otel_context = sampling_context[:otel]
190+
191+
expect(otel_context).to include(
192+
kind: root_span.kind,
193+
instrumentation_scope: {
194+
name: root_span.instrumentation_scope.name,
195+
version: root_span.instrumentation_scope.version
196+
}
197+
)
198+
199+
expect(otel_context).to include(attributes: root_span.attributes)
200+
201+
resource_attributes = root_span.resource.attribute_enumerator.to_h
202+
expect(otel_context).to include(resource: resource_attributes)
203+
end
204+
178205
context 'with started transaction' do
179206
let(:transaction) do
180207
subject.on_start(root_span, empty_context)

0 commit comments

Comments
 (0)