Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
format b3 headers as 16 chars when 64 bit
Browse files Browse the repository at this point in the history
  • Loading branch information
castengo committed Jul 29, 2020
1 parent 7fb276f commit 345c017
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/oc_propagation_http_b3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ to_headers(#span_ctx{trace_id=TraceId,
trace_options=TraceOptions}) ->
Options = case TraceOptions band 1 of 1 -> "1"; _ -> "0" end,
%% iolist_to_binary only needed for versions before otp-21
EncodedTraceId = iolist_to_binary(io_lib:format("~32.16.0b", [TraceId])),
EncodedTraceId = encode_trace_id(TraceId),
EncodedSpanId = iolist_to_binary(io_lib:format("~16.16.0b", [SpanId])),
[{?B3_TRACE_ID, EncodedTraceId},
{?B3_SPAN_ID, EncodedSpanId},
{?B3_SAMPLED, Options}];
to_headers(undefined) ->
[].

encode_trace_id(TraceID) when bit_size(TraceID) == 64 ->
iolist_to_binary(io_lib:format("~16.16.0b", [TraceID]));
encode_trace_id(TraceID) ->
iolist_to_binary(io_lib:format("~32.16.0b", [TraceID])).

-spec from_headers(list() | map()) -> maybe(opencensus:span_ctx()).
from_headers(Headers) when is_map(Headers) ->
from_headers(maps:to_list(Headers));
Expand Down
9 changes: 9 additions & 0 deletions test/oc_span_ctx_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ b3_encode_decode_headers(_Config) ->
compare_b3_headers(Encoded, Headers),
?assertEqual(Decoded, oc_propagation_http_b3:from_headers(Encoded)),

%% TraceId: 4bf92f3577b34da6
%% SpanId: 00f067aa0ba902b7
%% Enabled: true
Headers = ?B3_HEADERS(<<"4bf92f3577b34da6">>, <<"00f067aa0ba902b7">>, <<"1">>),
Decoded = oc_propagation_http_b3:from_headers(Headers),
Encoded = oc_propagation_http_b3:to_headers(Decoded),
compare_b3_headers(Encoded, Headers),
?assertEqual(Decoded, oc_propagation_http_b3:from_headers(Encoded)),

%% TraceId: 4bf92f3577b34da6a3ce929d0e0e4736
%% SpanId: 00f067aa0ba902b7
%% Enabled: false
Expand Down

0 comments on commit 345c017

Please sign in to comment.