Skip to content

Commit 06a3499

Browse files
committed
Call traces_sampler only for root spans
1 parent 02763e2 commit 06a3499

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

lib/sentry/opentelemetry/sampler.ex

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,8 @@ if Code.ensure_loaded?(:otel_sampler) do
4343

4444
case get_trace_sampling_decision(ctx) do
4545
{:inherit, trace_sampled, tracestate} ->
46-
if traces_sampler do
47-
sampling_context =
48-
build_sampling_context(
49-
trace_sampled,
50-
span_name,
51-
span_kind,
52-
attributes,
53-
trace_id
54-
)
55-
56-
make_sampler_decision(traces_sampler, sampling_context, tracestate)
57-
else
58-
decision = if trace_sampled, do: :record_and_sample, else: :drop
59-
{decision, [], tracestate}
60-
end
46+
decision = if trace_sampled, do: :record_and_sample, else: :drop
47+
{decision, [], tracestate}
6148

6249
:no_trace ->
6350
if traces_sampler do

test/sentry/opentelemetry/sampler_test.exs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,16 @@ defmodule Sentry.Opentelemetry.SamplerTest do
374374
Sampler.should_sample(test_ctx, 123, nil, "test span", nil, %{}, drop: [])
375375
end
376376

377-
test "traces_sampler can override parent sampling decision" do
377+
test "child spans inherit parent sampling decision without calling traces_sampler" do
378+
{:ok, sampler_call_count} = Agent.start_link(fn -> 0 end)
379+
380+
sampler_fun = fn _sampling_context ->
381+
Agent.update(sampler_call_count, &(&1 + 1))
382+
false
383+
end
384+
385+
put_test_config(traces_sampler: sampler_fun)
386+
378387
trace_tracestate = [
379388
{"sentry-sample_rate", "1.0"},
380389
{"sentry-sample_rand", "0.5"},
@@ -388,15 +397,40 @@ defmodule Sentry.Opentelemetry.SamplerTest do
388397
token = :otel_ctx.attach(ctx_with_span)
389398

390399
try do
391-
put_test_config(traces_sampler: fn _ -> false end)
392-
393400
result =
394401
Sampler.should_sample(ctx_with_span, 123, nil, "child span", nil, %{}, drop: [])
395402

396-
assert {:drop, [], _tracestate} = result
403+
assert {:record_and_sample, [], returned_tracestate} = result
404+
assert returned_tracestate == trace_tracestate
405+
406+
call_count = Agent.get(sampler_call_count, & &1)
407+
assert call_count == 0
397408
after
398409
:otel_ctx.detach(token)
410+
Agent.stop(sampler_call_count)
411+
end
412+
end
413+
414+
test "traces_sampler is only called for root spans" do
415+
{:ok, sampler_call_count} = Agent.start_link(fn -> 0 end)
416+
417+
sampler_fun = fn _sampling_context ->
418+
Agent.update(sampler_call_count, &(&1 + 1))
419+
true
399420
end
421+
422+
put_test_config(traces_sampler: sampler_fun)
423+
424+
test_ctx = create_test_span_context()
425+
426+
result = Sampler.should_sample(test_ctx, 123, nil, "root span", nil, %{}, drop: [])
427+
428+
assert {:record_and_sample, [], _tracestate} = result
429+
430+
call_count = Agent.get(sampler_call_count, & &1)
431+
assert call_count == 1
432+
433+
Agent.stop(sampler_call_count)
400434
end
401435

402436
test "handles traces_sampler errors gracefully" do

0 commit comments

Comments
 (0)