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

format b3 headers as 16 chars when 64 bit #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 src/oc_propagation_http_b3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ 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 = iolist_to_binary(io_lib:format("~.16b", [TraceId])),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can either do this or check if the integer is between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 in a guard. Thought this might be better?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can also do binary:encode_unsigned(TraceId) and check if its size 64 or 128 before formatting it. Let me know which approach you prefer.

EncodedSpanId = iolist_to_binary(io_lib:format("~16.16.0b", [SpanId])),
[{?B3_TRACE_ID, EncodedTraceId},
{?B3_SPAN_ID, EncodedSpanId},
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
ShortTraceIDHeaders = ?B3_HEADERS(<<"4bf92f3577b34da6">>, <<"00f067aa0ba902b7">>, <<"1">>),
ShortTraceIDDecoded = oc_propagation_http_b3:from_headers(ShortTraceIDHeaders),
ShortTraceIDEncoded = oc_propagation_http_b3:to_headers(ShortTraceIDDecoded),
compare_b3_headers(ShortTraceIDEncoded, ShortTraceIDHeaders),
?assertEqual(ShortTraceIDDecoded, oc_propagation_http_b3:from_headers(ShortTraceIDEncoded)),

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