-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add http propagators for trace context
- Loading branch information
1 parent
21d75b9
commit b9024d1
Showing
7 changed files
with
316 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
%%%------------------------------------------------------------------------ | ||
%% Copyright 2019, OpenTelemetry Authors | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
%% | ||
%% @doc | ||
%% @end | ||
%%%----------------------------------------------------------------------- | ||
-module(ot_propagation_http_b3). | ||
|
||
-export([inject/2, | ||
extract/2]). | ||
|
||
-include_lib("opentelemetry_api/include/opentelemetry.hrl"). | ||
|
||
-define(B3_TRACE_ID, <<"X-B3-TraceId">>). | ||
-define(B3_SPAN_ID, <<"X-B3-SpanId">>). | ||
-define(B3_SAMPLED, <<"X-B3-Sampled">>). | ||
|
||
-define(IS_SAMPLED(S), S =:= "1" orelse S =:= <<"1">> orelse S =:= "true" orelse S =:= <<"true">>). | ||
|
||
-spec inject(ot_propagation:http_headers(), | ||
{opencensus:span_ctx(), opentelemetry:span_ctx() | undefined} | undefined) | ||
-> ot_propagation:http_headers(). | ||
inject(_, {#span_ctx{trace_id=TraceId, | ||
span_id=SpanId}, _}) when TraceId =:= 0 | ||
; SpanId =:= 0 -> | ||
[]; | ||
inject(_, {#span_ctx{trace_id=TraceId, | ||
span_id=SpanId, | ||
trace_flags=TraceOptions}, _}) -> | ||
Options = case TraceOptions band 1 of 1 -> "1"; _ -> "0" end, | ||
EncodedTraceId = io_lib:format("~32.16.0b", [TraceId]), | ||
EncodedSpanId = io_lib:format("~16.16.0b", [SpanId]), | ||
[{?B3_TRACE_ID, EncodedTraceId}, | ||
{?B3_SPAN_ID, EncodedSpanId}, | ||
{?B3_SAMPLED, Options}]; | ||
inject(_, undefined) -> | ||
[]. | ||
|
||
-spec extract(ot_propagation:http_headers(), term()) -> opentelemetry:span_ctx()| undefined. | ||
extract(Headers, _) when is_list(Headers) -> | ||
try | ||
TraceId = trace_id(Headers), | ||
SpanId = span_id(Headers), | ||
Sampled = lookup(?B3_SAMPLED, Headers), | ||
{#span_ctx{trace_id=string_to_integer(TraceId, 16), | ||
span_id=string_to_integer(SpanId, 16), | ||
trace_flags=case Sampled of True when ?IS_SAMPLED(True) -> 1; _ -> 0 end}, undefined} | ||
catch | ||
throw:invalid -> | ||
undefined; | ||
|
||
%% thrown if _to_integer fails | ||
error:badarg -> | ||
undefined | ||
end; | ||
extract(_, _) -> | ||
undefined. | ||
|
||
trace_id(Headers) -> | ||
case lookup(?B3_TRACE_ID, Headers) of | ||
TraceId when is_list(TraceId) orelse is_binary(TraceId) -> | ||
case string:length(TraceId) =:= 32 orelse string:length(TraceId) =:= 16 of | ||
true -> | ||
TraceId; | ||
_ -> | ||
throw(invalid) | ||
end; | ||
_ -> | ||
throw(invalid) | ||
end. | ||
|
||
span_id(Headers) -> | ||
case lookup(?B3_SPAN_ID, Headers) of | ||
SpanId when is_list(SpanId) orelse is_binary(SpanId) -> | ||
case string:length(SpanId) =:= 16 of | ||
true -> | ||
SpanId; | ||
_ -> | ||
throw(invalid) | ||
end; | ||
_ -> | ||
throw(invalid) | ||
end. | ||
|
||
%% find a header in a list, ignoring case | ||
lookup(_, []) -> | ||
undefined; | ||
lookup(Header, [{H, Value} | Rest]) -> | ||
case string:equal(Header, H, true, none) of | ||
true -> | ||
Value; | ||
false -> | ||
lookup(Header, Rest) | ||
end. | ||
|
||
string_to_integer(S, Base) when is_binary(S) -> | ||
binary_to_integer(S, Base); | ||
string_to_integer(S, Base) when is_list(S) -> | ||
list_to_integer(S, Base). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
%%%------------------------------------------------------------------------ | ||
%% Copyright 2019, OpenTelemetry Authors | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
%% | ||
%% @doc | ||
%% @end | ||
%%%----------------------------------------------------------------------- | ||
-module(ot_propagation_http_w3c). | ||
|
||
-export([inject/2, | ||
encode/1, | ||
extract/2, | ||
decode/1]). | ||
|
||
-include_lib("opentelemetry_api/include/opentelemetry.hrl"). | ||
|
||
-define(VERSION, "00"). | ||
|
||
-define(ZERO_TRACEID, <<"00000000000000000000000000000000">>). | ||
-define(ZERO_SPANID, <<"0000000000000000">>). | ||
|
||
-define(HEADER_KEY, <<"traceparent">>). | ||
-define(STATE_HEADER_KEY, <<"tracestate">>). | ||
|
||
-spec inject(ot_propagation:http_headers(), | ||
{opentelemetry:span_ctx(), opentelemetry:span_ctx() | undefined} | undefined) | ||
-> ot_propagation:http_headers(). | ||
inject(_, {#span_ctx{trace_id=TraceId, | ||
span_id=SpanId}, _}) | ||
when TraceId =:= 0 orelse SpanId =:= 0 -> | ||
[]; | ||
inject(_, {SpanCtx=#span_ctx{}, _}) -> | ||
EncodedValue = encode(SpanCtx), | ||
[{?HEADER_KEY, EncodedValue} | encode_tracestate(SpanCtx)]; | ||
inject(_, undefined) -> | ||
[]. | ||
|
||
-spec encode(opencensus:span_ctx()) -> iolist(). | ||
encode(#span_ctx{trace_id=TraceId, | ||
span_id=SpanId, | ||
trace_flags=TraceOptions}) -> | ||
Options = case TraceOptions band 1 of 1 -> <<"01">>; _ -> <<"00">> end, | ||
EncodedTraceId = io_lib:format("~32.16.0b", [TraceId]), | ||
EncodedSpanId = io_lib:format("~16.16.0b", [SpanId]), | ||
[?VERSION, "-", EncodedTraceId, "-", EncodedSpanId, "-", Options]. | ||
|
||
encode_tracestate(#span_ctx{tracestate=undefined}) -> | ||
[]; | ||
encode_tracestate(#span_ctx{tracestate=Entries}) -> | ||
StateHeaderValue = lists:join($,, [[Key, $=, Value] || {Key, Value} <- Entries]), | ||
[{?STATE_HEADER_KEY, StateHeaderValue}]. | ||
|
||
-spec extract(ot_propagation:http_headers(), term()) -> opentelemetry:span_ctx() | undefined. | ||
extract(Headers, _) when is_list(Headers) -> | ||
case lists:keyfind(?HEADER_KEY, 1, Headers) of | ||
{_, Value} -> | ||
case decode(Value) of | ||
undefined -> | ||
undefined; | ||
SpanCtx -> | ||
Tracestate = tracestate_from_headers(Headers), | ||
SpanCtx#span_ctx{tracestate=Tracestate} | ||
end; | ||
_ -> | ||
undefined | ||
end; | ||
extract(_, _) -> | ||
undefined. | ||
|
||
tracestate_from_headers(Headers) -> | ||
%% could be multiple tracestate headers. Combine them all with comma separators | ||
case combine_headers(?STATE_HEADER_KEY, Headers) of | ||
[] -> | ||
undefined; | ||
FieldValue -> | ||
tracestate_decode(FieldValue) | ||
end. | ||
|
||
combine_headers(Key, Headers) -> | ||
lists:foldl(fun({K, V}, Acc) -> | ||
case string:equal(K, Key) of | ||
true -> | ||
[V, $, | Acc]; | ||
false -> | ||
Acc | ||
end | ||
end, [], Headers). | ||
|
||
tracestate_decode(Value) -> | ||
%% TODO: the 512 byte limit should not include optional white space that can | ||
%% appear between list members. | ||
case iolist_size(Value) of | ||
Size when Size =< 512 -> | ||
[split(Pair) || Pair <- string:lexemes(Value, [$,])]; | ||
_ -> | ||
undefined | ||
end. | ||
|
||
split(Pair) -> | ||
case string:split(Pair, "=") of | ||
[Key, Value] -> | ||
{iolist_to_binary(Key), iolist_to_binary(Value)}; | ||
[Key] -> | ||
{iolist_to_binary(Key), <<>>} | ||
end. | ||
|
||
decode(TraceContext) when is_list(TraceContext) -> | ||
decode(list_to_binary(TraceContext)); | ||
decode(<<?VERSION, "-", TraceId:32/binary, "-", SpanId:16/binary, _/binary>>) | ||
when TraceId =:= ?ZERO_TRACEID orelse SpanId =:= ?ZERO_SPANID -> | ||
undefined; | ||
decode(<<?VERSION, "-", TraceId:32/binary, "-", SpanId:16/binary, "-", Opts:2/binary, _/binary>>) -> | ||
try | ||
#span_ctx{trace_id=binary_to_integer(TraceId, 16), | ||
span_id=binary_to_integer(SpanId, 16), | ||
trace_flags=case Opts of <<"01">> -> 1; _ -> 0 end} | ||
catch | ||
%% to integer from base 16 string failed | ||
error:badarg -> | ||
undefined | ||
end; | ||
decode(_) -> | ||
undefined. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters