Skip to content
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: 2 additions & 0 deletions apps/opentelemetry/src/otel_span_ets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ get_ctx(#span{trace_id=TraceId,
tracestate=TraceState,
is_recording=IsRecording}) ->
#span_ctx{trace_id=TraceId,
hex_trace_id=otel_utils:encode_hex(<<TraceId:128>>),
span_id=SpanId,
hex_span_id=otel_utils:encode_hex(<<SpanId:64>>),
tracestate=TraceState,
is_recording=IsRecording}.

Expand Down
14 changes: 11 additions & 3 deletions apps/opentelemetry/src/otel_span_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ new_span_ctx(Ctx, IdGeneratorModule) ->
{root_span_ctx(IdGeneratorModule), undefined, undefined};
ParentSpanCtx=#span_ctx{span_id=ParentSpanId, is_remote=ParentIsRemote} ->
%% keep the rest of the parent span ctx, simply need to update the span_id
{ParentSpanCtx#span_ctx{span_id=IdGeneratorModule:generate_span_id()}, ParentSpanId, ParentIsRemote}
SpanId = IdGeneratorModule:generate_span_id(),
HexSpanId = otel_utils:encode_hex(<<SpanId:64>>),
{ParentSpanCtx#span_ctx{span_id=SpanId, hex_span_id=HexSpanId}, ParentSpanId, ParentIsRemote}
end.

root_span_ctx(IdGeneratorModule) ->
#span_ctx{trace_id=IdGeneratorModule:generate_trace_id(),
span_id=IdGeneratorModule:generate_span_id(),
TraceId = IdGeneratorModule:generate_trace_id(),
HexTraceId = otel_utils:encode_hex(<<TraceId:128>>),
SpanId = IdGeneratorModule:generate_span_id(),
HexSpanId = otel_utils:encode_hex(<<SpanId:64>>),
#span_ctx{trace_id=TraceId,
hex_trace_id=HexTraceId,
span_id=SpanId,
hex_span_id=HexSpanId,
is_valid=true,
trace_flags=0}.

Expand Down
11 changes: 11 additions & 0 deletions apps/opentelemetry/test/opentelemetry_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,17 @@ no_exporter(_Config) ->

ok.


generate_trace_id() -> 41394.
generate_span_id() -> 50132.
pregenerate_hex_ids(_Config) ->
HexTraceId = <<"0000000000000000000000000000a1b2">>,
HexSpanId = <<"000000000000c3d4">>,
SpanCtx = otel_span_utils:root_span_ctx(?MODULE),
?assertEqual(HexTraceId, SpanCtx#span_ctx.hex_trace_id),
?assertEqual(HexSpanId, SpanCtx#span_ctx.hex_span_id),
ok.

%%

assert_all_exported(Tid, SpanCtxs) ->
Expand Down
4 changes: 4 additions & 0 deletions apps/opentelemetry_api/include/opentelemetry.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@
-record(span_ctx, {
%% 128 bit int trace id
trace_id :: opentelemetry:trace_id(),
%% 128 bit int trace id encoded as hex binary string
hex_trace_id :: opentelemetry:hex_trace_id(),
%% 64 bit int span id
span_id :: opentelemetry:span_id(),
%% 64 bit int span id encoded as hex binary string
hex_span_id :: opentelemetry:hex_span_id(),
%% 8-bit integer, lowest bit is if it is sampled
trace_flags = 0 :: integer(),
%% Tracestate represents tracing-system specific context in a list of key-value pairs.
Expand Down
18 changes: 4 additions & 14 deletions apps/opentelemetry_api/src/otel_span.erl
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,12 @@ hex_span_ctx(_) ->
#{}.

-spec hex_trace_id(opentelemetry:span_ctx()) -> opentelemetry:hex_trace_id().
hex_trace_id(#span_ctx{trace_id=TraceId}) ->
case otel_utils:format_binary_string("~32.16.0b", [TraceId]) of
{ok, Binary} ->
Binary;
_ ->
<<>>
end.
hex_trace_id(#span_ctx{hex_trace_id=HexTraceId}) ->
HexTraceId.

-spec hex_span_id(opentelemetry:span_ctx()) -> opentelemetry:hex_span_id().
hex_span_id(#span_ctx{span_id=SpanId}) ->
case otel_utils:format_binary_string("~16.16.0b", [SpanId]) of
{ok, Binary} ->
Binary;
_ ->
<<>>
end.
hex_span_id(#span_ctx{hex_span_id=HexSpanId}) ->
HexSpanId.

-spec tracestate(opentelemetry:span_ctx() | undefined) -> otel_tracestate:t().
tracestate(#span_ctx{tracestate=Tracestate}) ->
Expand Down
4 changes: 4 additions & 0 deletions apps/opentelemetry_api/src/otel_tracer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ with_span(Ctx, Tracer={Module, _}, SpanName, Opts, Fun) when is_atom(Module) ->
-> opentelemetry:span_ctx().
non_recording_span(TraceId, SpanId, Traceflags) ->
#span_ctx{trace_id=TraceId,
hex_trace_id=otel_utils:encode_hex(<<TraceId:128>>),
span_id=SpanId,
hex_span_id=otel_utils:encode_hex(<<SpanId:64>>),
is_recording=false,
trace_flags=Traceflags}.

Expand All @@ -100,7 +102,9 @@ non_recording_span(TraceId, SpanId, Traceflags) ->
-> opentelemetry:span_ctx().
from_remote_span(TraceId, SpanId, Traceflags) ->
#span_ctx{trace_id=TraceId,
hex_trace_id=otel_utils:encode_hex(<<TraceId:128>>),
span_id=SpanId,
hex_span_id=otel_utils:encode_hex(<<SpanId:64>>),
is_valid=true,
is_recording=false,
is_remote=true,
Expand Down
2 changes: 2 additions & 0 deletions apps/opentelemetry_api/src/otel_tracer_noop.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
-include("otel_tracer.hrl").

-define(NOOP_SPAN_CTX, #span_ctx{trace_id=0,
hex_trace_id= <<"00000000000000000000000000000000">>,
span_id=0,
hex_span_id= <<"0000000000000000">>,
trace_flags=0,
tracestate=otel_tracestate:new(),
is_valid=false,
Expand Down
15 changes: 14 additions & 1 deletion apps/opentelemetry_api/src/otel_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
format_binary_string/2,
format_binary_string/3,
assert_to_binary/1,
unicode_to_binary/1]).
unicode_to_binary/1,
encode_hex/1]).

-if(?OTP_RELEASE >= 24).
format_exception(Kind, Reason, StackTrace) ->
Expand Down Expand Up @@ -55,3 +56,15 @@ unicode_to_binary(String) ->
_ ->
{error, bad_binary_conversion}
end.

-spec encode_hex(binary()) -> binary().
-if(?OTP_RELEASE >= 26).
encode_hex(Bin) ->
binary:encode_hex(Bin, lowercase).
-elif(?OTP_RELEASE >= 24).
encode_hex(Bin) ->
string:lowercase(binary:encode_hex(Bin)).
-else.
encode_hex(Bin) ->
string:lowercase(<< <<Enc>> || <<HByte:4>> <= Bin, Enc <- integer_to_list(HByte, 16) >>).
-endif.
2 changes: 1 addition & 1 deletion apps/opentelemetry_api/test/opentelemetry_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ noop_with_span(_Config) ->
hex_trace_ids(_Config) ->
HexTraceId = <<"0000000000000000000000000000a1b2">>,
HexSpanId = <<"000000000000c3d4">>,
SpanCtx=#span_ctx{trace_id=41394, span_id=50132},
SpanCtx=#span_ctx{trace_id=41394, hex_trace_id=HexTraceId, span_id=50132, hex_span_id=HexSpanId},
?assertEqual(HexTraceId, otel_span:hex_trace_id(SpanCtx)),
?assertEqual(HexSpanId, otel_span:hex_span_id(SpanCtx)),
?assertEqual(#{otel_trace_id => HexTraceId,
Expand Down
6 changes: 6 additions & 0 deletions apps/opentelemetry_api/test/otel_propagators_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ rewrite(_Config) ->
otel_ctx:clear(),

RecordingSpanCtx = #span_ctx{trace_id=21267647932558653966460912964485513216,
hex_trace_id= <<"10000000000000000000000000000000">>,
span_id=1152921504606846976,
hex_span_id= <<"1000000000000000">>,
is_valid=true,
is_recording=true},
otel_tracer:set_current_span(RecordingSpanCtx),
Expand Down Expand Up @@ -117,7 +119,9 @@ invalid_span_no_sdk_propagation(_Config) ->
otel_ctx:clear(),

InvalidSpanCtx = #span_ctx{trace_id=0,
hex_trace_id= <<"00000000000000000000000000000000">>,
span_id=0,
hex_span_id= <<"0000000000000000">>,
trace_flags=0,
is_valid=false,
is_recording=false},
Expand All @@ -142,7 +146,9 @@ nonrecording_no_sdk_propagation(_Config) ->
otel_ctx:clear(),

NonRecordingSpanCtx = #span_ctx{trace_id=21267647932558653966460912964485513216,
hex_trace_id= <<"10000000000000000000000000000000">>,
span_id=1152921504606846976,
hex_span_id= <<"1000000000000000">>,
is_valid=true,
is_recording=false},
?set_current_span(NonRecordingSpanCtx),
Expand Down