Skip to content

Commit c0487cf

Browse files
authored
Merge pull request #263 from qdentity/dvic-root-span-sampling
Fix root span sampling
2 parents ea75b2a + 38b964c commit c0487cf

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

apps/opentelemetry/src/otel_span_utils.erl

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ start_span(Ctx, Name, Opts) ->
3636
new_span(Ctx, Name, Sampler, StartTime, Kind, Attributes, Links).
3737

3838
new_span(Ctx, Name, Sampler, StartTime, Kind, Attributes, Links) ->
39-
ParentTraceId = trace_id(Ctx),
40-
{TraceFlags, IsRecording, SamplerAttributes, TraceState} =
41-
sample(Ctx, Sampler, ParentTraceId, Links, Name, Kind, Attributes),
42-
4339
{NewSpanCtx, ParentSpanId} = new_span_ctx(Ctx),
40+
4441
TraceId = NewSpanCtx#span_ctx.trace_id,
4542
SpanId = NewSpanCtx#span_ctx.span_id,
4643

44+
{TraceFlags, IsRecording, SamplerAttributes, TraceState} =
45+
sample(Ctx, Sampler, TraceId, Links, Name, Kind, Attributes),
46+
4747
Span = #span{trace_id=TraceId,
4848
span_id=SpanId,
4949
tracestate=TraceState,
@@ -60,14 +60,6 @@ new_span(Ctx, Name, Sampler, StartTime, Kind, Attributes, Links) ->
6060
is_valid=true,
6161
is_recording=IsRecording}, Span}.
6262

63-
trace_id(Ctx) ->
64-
case otel_tracer:current_span_ctx(Ctx) of
65-
#span_ctx{trace_id=TraceId} ->
66-
TraceId;
67-
_ ->
68-
undefined
69-
end.
70-
7163
-spec new_span_ctx(otel_ctx:t()) -> {opentelemetry:span_ctx(), opentelemetry:span_id()}.
7264
new_span_ctx(Ctx) ->
7365
case otel_tracer:current_span_ctx(Ctx) of

apps/opentelemetry/test/opentelemetry_SUITE.erl

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ all() ->
2121
all_testcases() ->
2222
[disable_auto_registration, registered_tracers, with_span, macros, child_spans,
2323
update_span_data, tracer_instrumentation_library, tracer_previous_ctx, stop_temporary_app,
24-
reset_after, attach_ctx, default_sampler, non_recording_ets_table, record_but_not_sample,
25-
record_exception_works, record_exception_with_message_works].
24+
reset_after, attach_ctx, default_sampler, non_recording_ets_table,
25+
root_span_sampling_always_on, root_span_sampling_always_off,
26+
record_but_not_sample, record_exception_works, record_exception_with_message_works].
2627

2728
groups() ->
2829
[{w3c, [], [propagation]},
@@ -471,6 +472,38 @@ non_recording_ets_table(_Config) ->
471472
?assertMatch([#span{name = <<"span-1">>}], ets:tab2list(?SPAN_TAB)),
472473
ok.
473474

475+
root_span_sampling_always_off(_Config) ->
476+
Tracer = opentelemetry:get_tracer(),
477+
478+
Sampler = otel_sampler:new(always_off),
479+
480+
SpanCtx1 = otel_tracer:start_span(Tracer, <<"span-1">>, #{sampler => Sampler}),
481+
?assertMatch(false, SpanCtx1#span_ctx.is_recording),
482+
?assertMatch(0, SpanCtx1#span_ctx.trace_flags),
483+
484+
otel_tracer:set_current_span(SpanCtx1),
485+
SpanCtx2 = otel_tracer:start_span(Tracer, <<"span-2">>, #{}),
486+
?assertMatch(false, SpanCtx2#span_ctx.is_recording),
487+
?assertMatch(0, SpanCtx2#span_ctx.trace_flags),
488+
489+
ok.
490+
491+
root_span_sampling_always_on(_Config) ->
492+
Tracer = opentelemetry:get_tracer(),
493+
494+
Sampler = otel_sampler:new(always_on),
495+
496+
SpanCtx1 = otel_tracer:start_span(Tracer, <<"span-1">>, #{sampler => Sampler}),
497+
?assertMatch(true, SpanCtx1#span_ctx.is_recording),
498+
?assertMatch(1, SpanCtx1#span_ctx.trace_flags),
499+
500+
otel_tracer:set_current_span(SpanCtx1),
501+
SpanCtx2 = otel_tracer:start_span(Tracer, <<"span-2">>, #{}),
502+
?assertMatch(true, SpanCtx2#span_ctx.is_recording),
503+
?assertMatch(1, SpanCtx1#span_ctx.trace_flags),
504+
505+
ok.
506+
474507
record_but_not_sample(Config) ->
475508
ct:comment("Test that a Span that the sampler returns RECORD_ONLY for gets created"
476509
"as a valid recorded span but is not sent to the exporter."),

0 commit comments

Comments
 (0)