Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix root span sampling #263

Merged
merged 10 commits into from
Aug 20, 2021
16 changes: 4 additions & 12 deletions apps/opentelemetry/src/otel_span_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ start_span(Ctx, Name, Opts) ->
new_span(Ctx, Name, Sampler, StartTime, Kind, Attributes, Links).

new_span(Ctx, Name, Sampler, StartTime, Kind, Attributes, Links) ->
ParentTraceId = trace_id(Ctx),
{TraceFlags, IsRecording, SamplerAttributes, TraceState} =
sample(Ctx, Sampler, ParentTraceId, Links, Name, Kind, Attributes),

{NewSpanCtx, ParentSpanId} = new_span_ctx(Ctx),

TraceId = NewSpanCtx#span_ctx.trace_id,
SpanId = NewSpanCtx#span_ctx.span_id,

{TraceFlags, IsRecording, SamplerAttributes, TraceState} =
sample(Ctx, Sampler, TraceId, Links, Name, Kind, Attributes),

Span = #span{trace_id=TraceId,
span_id=SpanId,
tracestate=TraceState,
Expand All @@ -60,14 +60,6 @@ new_span(Ctx, Name, Sampler, StartTime, Kind, Attributes, Links) ->
is_valid=true,
is_recording=IsRecording}, Span}.

trace_id(Ctx) ->
case otel_tracer:current_span_ctx(Ctx) of
#span_ctx{trace_id=TraceId} ->
TraceId;
_ ->
undefined
end.

-spec new_span_ctx(otel_ctx:t()) -> {opentelemetry:span_ctx(), opentelemetry:span_id()}.
new_span_ctx(Ctx) ->
case otel_tracer:current_span_ctx(Ctx) of
Expand Down
39 changes: 36 additions & 3 deletions apps/opentelemetry/test/opentelemetry_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ all() ->

all_testcases() ->
[disable_auto_registration, registered_tracers, with_span, macros, child_spans,
update_span_data, tracer_instrumentation_library,
tracer_previous_ctx, stop_temporary_app, reset_after, attach_ctx, default_sampler,
record_but_not_sample, record_exception_works, record_exception_with_message_works].
update_span_data, tracer_instrumentation_library, tracer_previous_ctx, stop_temporary_app,
reset_after, attach_ctx, default_sampler, root_span_sampling_always_on,
root_span_sampling_always_off,record_but_not_sample, record_exception_works,
record_exception_with_message_works].

groups() ->
[{w3c, [], [propagation]},
Expand Down Expand Up @@ -457,6 +458,38 @@ default_sampler(_Config) ->

ok.

root_span_sampling_always_off(_Config) ->
Tracer = opentelemetry:get_tracer(),

Sampler = otel_sampler:setup(always_off),
dvic marked this conversation as resolved.
Show resolved Hide resolved

SpanCtx1 = otel_tracer:start_span(Tracer, <<"span-1">>, #{sampler => Sampler}),
?assertMatch(false, SpanCtx1#span_ctx.is_recording),
?assertMatch(0, SpanCtx1#span_ctx.trace_flags),

otel_tracer:set_current_span(SpanCtx1),
SpanCtx2 = otel_tracer:start_span(Tracer, <<"span-2">>, #{}),
?assertMatch(false, SpanCtx2#span_ctx.is_recording),
?assertMatch(0, SpanCtx2#span_ctx.trace_flags),

ok.

root_span_sampling_always_on(_Config) ->
Tracer = opentelemetry:get_tracer(),

Sampler = otel_sampler:setup(always_on),
dvic marked this conversation as resolved.
Show resolved Hide resolved

SpanCtx1 = otel_tracer:start_span(Tracer, <<"span-1">>, #{sampler => Sampler}),
?assertMatch(true, SpanCtx1#span_ctx.is_recording),
?assertMatch(1, SpanCtx1#span_ctx.trace_flags),

otel_tracer:set_current_span(SpanCtx1),
SpanCtx2 = otel_tracer:start_span(Tracer, <<"span-2">>, #{}),
?assertMatch(true, SpanCtx2#span_ctx.is_recording),
?assertMatch(1, SpanCtx1#span_ctx.trace_flags),

ok.

record_but_not_sample(Config) ->
ct:comment("Test that a Span that the sampler returns RECORD_ONLY for gets created"
"as a valid recorded span but is not sent to the exporter."),
Expand Down