Skip to content

Commit 865187c

Browse files
committed
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
1 parent 337a4ca commit 865187c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@
154154

155155
it 'starts a sentry transaction on otel root span' do
156156
expect(Sentry).to receive(:start_transaction).and_call_original
157+
expect_any_instance_of(Sentry::Transaction).to receive(:set_initial_sample_decision).with(
158+
sampling_context: a_hash_including(
159+
otel: {
160+
attributes: root_span.attributes,
161+
resource: root_span.resource.attribute_enumerator.to_h,
162+
kind: root_span.kind,
163+
instrumentation_scope: {
164+
name: root_span.instrumentation_scope.name,
165+
version: root_span.instrumentation_scope.version
166+
}
167+
}
168+
)
169+
)
170+
157171
subject.on_start(root_span, empty_context)
158172

159173
span_id = root_span.context.hex_span_id

0 commit comments

Comments
 (0)