/
jaeger_passage_span_context.erl
297 lines (265 loc) · 11.3 KB
/
jaeger_passage_span_context.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
%% @copyright 2017 Takeru Ohta <phjgt308@gmail.com>
%%
%% @doc Span context for Jaeger
%%
%% === References ===
%%
%% <ul>
%% <li><a href="https://github.com/jaegertracing/jaeger-client-go/blob/v2.9.0/README.md">jaeger-client-go/README.md</a></li>
%% <li><a href="https://github.com/uber/jaeger-client-go/tree/v2.9.0/context.go">context.go</a></li>
%% <li><a href="https://github.com/uber/jaeger-client-go/tree/v2.9.0/propagation.go">propagation.go</a></li>
%% </ul>
-module(jaeger_passage_span_context).
-behaviour(passage_span_context).
-include("constants.hrl").
%%------------------------------------------------------------------------------
%% Exported API
%%------------------------------------------------------------------------------
-export_type([state/0]).
%%------------------------------------------------------------------------------
%% Application Internal API
%%------------------------------------------------------------------------------
-export([get_trace_id/1]).
-export([get_span_id/1]).
-export([get_parent_span_id/1]).
-export([get_debug_id/1]).
-export([get_flags/1]).
%%------------------------------------------------------------------------------
%% 'passage_span_context' Callback API
%%------------------------------------------------------------------------------
-export([make_span_context_state/1, inject_span_context/4, extract_span_context/3]).
%%------------------------------------------------------------------------------
%% Macros & Records
%%------------------------------------------------------------------------------
-define(FLAG_SAMPLED, 2#01).
-define(FLAG_DEBUG, 2#10).
-define(STATE, ?MODULE).
-record(?STATE,
{
trace_id = 0 :: trace_id(),
span_id = 0 :: span_id(),
parent_span_id = 0 :: span_id(),
is_sampled = false :: boolean(),
debug_id :: binary() | undefined
}).
%%------------------------------------------------------------------------------
%% Exported Types
%%------------------------------------------------------------------------------
-opaque state() :: #?STATE{}.
%% The state of a jaeger span context.
%%------------------------------------------------------------------------------
%% Types
%%------------------------------------------------------------------------------
-type trace_id() :: 0..16#FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.
%% Tracer identifier (128-bit unsigned integer).
%%
%% This represents globally unique ID of the trace.
%% Usually generated as a random number.
-type span_id() :: 0..16#FFFFFFFFFFFFFFFF.
%% Span identifier (64-bit unsigned integer).
%%
%% This represents span ID that must be unique within its trace,
%% but does not have to be globally unique.
%%
%% Note that the ID `0' is used for representing invalid spans.
%%------------------------------------------------------------------------------
%% Application Internal Functions
%%------------------------------------------------------------------------------
%% @private
-spec get_trace_id(passage_span_context:context()) -> trace_id().
get_trace_id(Context) ->
(passage_span_context:get_state(Context))#?STATE.trace_id.
%% @private
-spec get_span_id(passage_span_context:context()) -> span_id().
get_span_id(Context) ->
(passage_span_context:get_state(Context))#?STATE.span_id.
-spec get_parent_span_id(passage_span_context:context()) -> span_id().
get_parent_span_id(Context) ->
(passage_span_context:get_state(Context))#?STATE.parent_span_id.
%% @private
-spec get_debug_id(passage_span_context:context()) -> {ok, binary()} | eror.
get_debug_id(Context) ->
State = passage_span_context:get_state(Context),
case State#?STATE.debug_id of
undefined -> error;
DebugId -> {ok, DebugId}
end.
%% @private
-spec get_flags(passage_span_context:context()) -> 0..16#FFFFFFFF.
get_flags(Context) ->
State = passage_span_context:get_state(Context),
get_flags_from_state(State).
%%------------------------------------------------------------------------------
%% 'passage_span_context' Callback Functions
%%------------------------------------------------------------------------------
%% @private
make_span_context_state([]) ->
#?STATE{
trace_id = rand:uniform(16#FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF),
span_id = rand:uniform(16#FFFFFFFFFFFFFFFF),
is_sampled = true
};
make_span_context_state([{_, Ref} | _]) ->
#?STATE{trace_id = TraceId, span_id = ParentId} =
passage_span_context:get_state(passage_span:get_context(Ref)),
#?STATE{
trace_id = TraceId,
span_id = rand:uniform(16#FFFFFFFFFFFFFFFF),
parent_span_id = ParentId,
is_sampled = true
}.
%% @private
inject_span_context(Context, binary, InjectFun, Carrier) ->
Bin0 = encode_state(passage_span_context:get_state(Context)),
Items = passage_span_context:get_baggage_items(Context),
Bin1 = <<Bin0/binary, (maps:size(Items)):32>>,
Bin2 = encode_baggage_items(Bin1, maps:to_list(Items)),
InjectFun(<<"binary">>, Bin2, Carrier);
inject_span_context(Context, Format, InjectFun, Carrier) ->
State = passage_span_context:get_state(Context),
MapFun =
case Format of
text_map ->
fun({K, V}) -> {?TRACE_BAGGAGE_HEADER(K), V} end;
http_header ->
fun({K, V}) -> {?TRACE_BAGGAGE_HEADER(K), escape(V)} end
end,
Items0 = lists:map(MapFun, maps:to_list(passage_span_context:get_baggage_items(Context))),
Items1 = [{?TRACER_STATE_HEADER_NAME, state_to_string(State)} | Items0],
lists:foldl(fun ({K, V}, Acc) -> InjectFun(K, V, Acc) end, Carrier, Items1).
%% @private
extract_span_context(binary, IterateFun, Carrier) ->
GetBinary =
fun GetBinary (Curr) ->
case IterateFun(Curr) of
error -> error;
{ok, <<"binary">>, V, _} -> {ok, V};
{ok, _, _, Next} -> GetBinary(Next)
end
end,
case GetBinary(Carrier) of
error -> error;
{ok, Bin0} ->
{State, Bin1} = decode_state(Bin0),
BaggageItems = decode_baggage_items(Bin1),
{ok, passage_span_context:make(State, BaggageItems)}
end;
extract_span_context(Format, IterateFun, Carrier) ->
DecodeValue =
case Format of
text_map -> fun (V) -> V end;
http_header -> fun unescape/1
end,
Map = extract_to_map(IterateFun, Carrier, DecodeValue, #{}),
case maps:is_key(state, Map) orelse maps:is_key(debug_id, Map) of
false -> error;
true ->
State0 = maps:get(state, Map, #?STATE{}),
State1 = State0#?STATE{debug_id = maps:get(debug_id, Map, undefined)},
BaggageItems = maps:filter(fun(K, _) -> is_binary(K) end, Map),
{ok, passage_span_context:make(State1, BaggageItems)}
end.
%%------------------------------------------------------------------------------
%% Internal Functions
%%------------------------------------------------------------------------------
-spec extract_to_map(passage_span_context:iterate_fun(),
passage_span_context:carrier(),
fun ((binary()) -> binary()),
Map) -> Map when
Map :: #{state => #?STATE{},
debug_id => binary(),
binary() => binary()}.
extract_to_map(IterateFun, Carrier0, DecodeValue, Map0) ->
case IterateFun(Carrier0) of
error -> Map0;
{ok, K, V, Carrier1} ->
Map1 =
case K of
?TRACER_STATE_HEADER_NAME ->
maps:put(state, state_from_string(DecodeValue(V)), Map0);
?JAEGER_DEBUG_HEADER ->
maps:put(debug_id, DecodeValue(V), Map0);
?JAEGER_BAGGAGE_HEADER ->
Pairs0 =
binary:split(V, [<<",">>, <<" ">>], [global, trim, trim_all]),
Pairs1 =
[{Key, Value} ||
[Key, Value] <-
lists:map(
fun (P) -> binary:split(P, <<"=">>) end,
Pairs0)],
maps:merge(Map0, maps:from_list(Pairs1));
?TRACE_BAGGAGE_HEADER(Name) ->
maps:put(Name, DecodeValue(V), Map0);
_ -> Map0
end,
extract_to_map(IterateFun, Carrier1, DecodeValue, Map1)
end.
-spec get_flags_from_state(#?STATE{}) -> 0..16#FFFFFFFF.
get_flags_from_state(State) ->
FlagSampled =
case State#?STATE.is_sampled of
false -> 0;
true -> ?FLAG_SAMPLED
end,
FlagDebug =
case State#?STATE.debug_id of
undefined -> 0;
_ -> ?FLAG_DEBUG
end,
FlagSampled + FlagDebug.
-spec state_to_string(#?STATE{}) -> binary().
state_to_string(State) ->
#?STATE{trace_id = TraceId, span_id = SpanId, parent_span_id = ParentId} = State,
Flags = get_flags_from_state(State),
list_to_binary(io_lib:format("~.16b:~.16b:~.16b:~.16b",
[TraceId, SpanId, ParentId, Flags])).
-spec state_from_string(binary()) -> #?STATE{}.
state_from_string(Str) ->
{ok, [TraceId, SpanId, ParentSpanId, Flags], _} =
io_lib:fread("~16u:~16u:~16u:~16u", binary_to_list(Str)),
#?STATE{
trace_id = TraceId,
span_id = SpanId,
parent_span_id = ParentSpanId,
is_sampled = (Flags band ?FLAG_SAMPLED) =/= 0
}.
-spec encode_state(#?STATE{}) -> binary().
encode_state(State) ->
#?STATE{trace_id = TraceId, span_id = SpanId, parent_span_id = ParentId} = State,
Flags = get_flags_from_state(State),
<<TraceId:128, SpanId:64, ParentId:64, Flags:32>>.
-spec decode_state(binary()) -> {#?STATE{}, binary()}.
decode_state(<<TraceId:128, SpanId:64, ParentId:64, Flags:32, Bin/binary>>) ->
State =
#?STATE{
trace_id = TraceId,
span_id = SpanId,
parent_span_id = ParentId,
is_sampled = (Flags band ?FLAG_SAMPLED) =/= 0
},
{State, Bin}.
-spec encode_baggage_items(binary(), Items) -> binary() when
Items :: [{passage:baggage_item_name(), passage:baggage_item_value()}].
encode_baggage_items(Bin, []) ->
Bin;
encode_baggage_items(Bin, [{K, V} | Items]) ->
encode_baggage_items(
<<Bin/binary, (byte_size(K)):32, K/binary, (byte_size(V)):32, V/binary>>,
Items).
-spec decode_baggage_items(binary()) -> passage:baggage_items().
decode_baggage_items(<<Count:32, Bin/binary>>) ->
decode_baggage_items(Bin, Count, #{}).
-spec decode_baggage_items(binary(), non_neg_integer(), passage:baggage_items()) ->
passage:baggage_items().
decode_baggage_items(_, 0, Items) ->
Items;
decode_baggage_items(<<KeySize:32, Key:KeySize/binary,
ValueSize:32, Value:ValueSize/binary, Bin/binary>>, Count, Items) ->
decode_baggage_items(Bin, Count - 1, maps:put(Key, Value, Items)).
-spec escape(binary()) -> binary().
escape(Bin) ->
list_to_binary(http_uri:encode(binary_to_list(Bin))).
-spec unescape(binary()) -> binary().
unescape(Bin) ->
list_to_binary(http_uri:decode(binary_to_list(Bin))).