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

TD-949: Fixes otel ctx attachment with sampled span #23

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/mg_core/src/mg_core_machine.erl
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ handle_load(ID, Options, ReqCtx) ->
-spec handle_call(_Call, mg_core_worker:call_context(), maybe(request_context()), deadline(), state()) ->
{{reply, _Resp} | noreply, state()}.
handle_call(Call, CallContext, ReqCtx, Deadline, S) ->
%% FIXME Consider adding new pulse beats to wrap 'processing calls' here.
%% NOTE Consider adding new pulse beats to wrap 'processing calls' here.
ok = attach_otel_ctx(ReqCtx),
ParentSpanId = mg_core_otel:current_span_id(otel_ctx:get_current()),
?with_span(?SPAN_NAME(call), ?SPAN_OPTS, fun(SpanCtx) ->
Expand Down
20 changes: 13 additions & 7 deletions apps/mg_core/src/mg_core_otel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ maybe_attach_otel_ctx(NewCtx) ->
_ = otel_ctx:attach(choose_viable_otel_ctx(NewCtx, otel_ctx:get_current())),
ok.

%% lowest bit is if it is sampled
-define(IS_NOT_SAMPLED(SpanCtx), SpanCtx#span_ctx.trace_flags band 2#1 =/= 1).

-spec choose_viable_otel_ctx(T, T) -> T when T :: otel_ctx:t().
choose_viable_otel_ctx(NewCtx, CurrentCtx) ->
case {otel_tracer:current_span_ctx(NewCtx), otel_tracer:current_span_ctx(CurrentCtx)} of
%% Don't attach if new context is without recording span
%% and old context has span defined
{#span_ctx{is_recording = false}, #span_ctx{}} -> CurrentCtx;
%% Don't attach if new context is without sampled span and old
%% context has span defined
{SpanCtx = #span_ctx{}, #span_ctx{}} when ?IS_NOT_SAMPLED(SpanCtx) -> CurrentCtx;
{undefined, #span_ctx{}} -> CurrentCtx;
{_, _} -> NewCtx
end.
Expand Down Expand Up @@ -184,23 +187,26 @@ binary_to_id(Opaque) when is_binary(Opaque) ->
-type testgen() :: {_ID, fun(() -> _)}.
-spec test() -> _.

-define(OTEL_CTX(IsRecording),
-define(IS_SAMPLED, 1).
-define(NOT_SAMPLED, 0).
-define(OTEL_CTX(IsSampled),
otel_tracer:set_current_span(
otel_ctx:new(),
(otel_tracer_noop:noop_span_ctx())#span_ctx{
trace_id = otel_id_generator:generate_trace_id(),
span_id = otel_id_generator:generate_span_id(),
is_valid = true,
is_remote = true,
is_recording = IsRecording
is_recording = false,
trace_flags = IsSampled
}
)
).

-spec choose_viable_otel_ctx_test_() -> [testgen()].
choose_viable_otel_ctx_test_() ->
A = ?OTEL_CTX(true),
B = ?OTEL_CTX(false),
A = ?OTEL_CTX(?IS_SAMPLED),
B = ?OTEL_CTX(?NOT_SAMPLED),
[
?_assertEqual(A, choose_viable_otel_ctx(A, B)),
?_assertEqual(A, choose_viable_otel_ctx(B, A)),
Expand Down
Loading