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

convert annotations to string values in zipkin reporter #104

Merged
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
38 changes: 37 additions & 1 deletion src/oc_reporter_zipkin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,46 @@ zipkin_span(Span, LocalEndpoint) ->
<<"shared">> => false, %% TODO: get from attributes?
<<"localEndpoint">> => LocalEndpoint,
%% <<"remoteEndpoint">> => %% TODO: get from attributes?
<<"annotations">> => [],
<<"annotations">> => to_annotations(Span#span.time_events),
<<"tags">> => to_tags(Span#span.attributes) %% TODO: merge with oc_tags?
}.

to_annotations(TimeEvents) ->
to_annotations(TimeEvents, []).

to_annotations([], Annotations) ->
Annotations;
to_annotations([{Timestamp, #annotation{description=Description,
attributes=Attributes}} | Rest], Annotations) ->
to_annotations(Rest, [#{<<"timestamp">> => wts:to_absolute(Timestamp),
<<"value">> => annotation_value(Description, Attributes)} | Annotations]);
to_annotations([{Timestamp, MessageEvent=#message_event{}} | Rest], Annotations) ->
to_annotations(Rest, [#{<<"timestamp">> => wts:to_absolute(Timestamp),
<<"value">> => annotation_value(MessageEvent)} | Annotations]).

annotation_value(Description, Attributes) ->
AttrString = lists:join(", ", [[Key, "=", to_string(Value)] ||
{Key, Value} <- maps:to_list(Attributes)]),
iolist_to_binary([Description, " Attributes:{", AttrString, "}"]).

annotation_value(#message_event{type=Type,
id=Id,
uncompressed_size=UncompressedSize,
compressed_size=CompressedSize}) ->
iolist_to_binary(["MessageEvent:{type=", atom_to_binary(Type, utf8),
", id=", integer_to_binary(Id),
Copy link
Contributor

Choose a reason for hiding this comment

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

integer_to_binary - converts to decimal value or binary? real binary may be hard to read.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is more like integer to string. Erlang has a specific binary type. It is used a lot for strings, esp when going to json since regular Erlang strings are just lists of integers and the way most json encoders work in Erlang is to require strings as binaries so it can easily know if you meant a string or an array of integers.

Binaries are also represented with the << and >> syntax you probably see all over the code.

", uncompressed_size=", integer_to_binary(UncompressedSize),
", compressed_size=", integer_to_binary(CompressedSize), "}"]).


to_string(Value) when is_function(Value) ->
to_string(Value());
to_string(Value) when is_list(Value) ;
is_binary(Value) ->
Value;
to_string(Value) ->
io_lib:format("~p", [Value]).

to_tag(_Name, Value) when is_function(Value) ->
Value();
to_tag(_Name, Value) when is_list(Value) ->
Expand Down
16 changes: 15 additions & 1 deletion test/oc_reporters_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ zipkin_reporter(_Config) ->
#{attributes => #{<<"attr1">> => <<"val1">>,
<<"attr_as_function">> =>
fun () -> <<"val2">> end}}),

Annotation = oc_span:annotation( <<"description">>, #{<<"key1">> => <<"value1">>,
<<"key2">> => <<"value2">>}),
MessageEvent = oc_span:message_event('SENT', 5555, 200, 100),
oc_trace:add_time_event(Annotation, Child),
oc_trace:add_time_event(MessageEvent, Child),

oc_trace:finish_span(Child),
oc_trace:finish_span(Parent),

Expand All @@ -138,7 +145,14 @@ zipkin_reporter(_Config) ->
<<"shared">> := false,
<<"tags">> := #{},
<<"traceId">> := ParentTraceId},
#{<<"annotations">> := [],
#{<<"annotations">> :=
[#{<<"timestamp">> := _,
<<"value">> :=
<<"description Attributes:{key1=value1, key2=value2}">>},
#{<<"timestamp">> := _,
<<"value">> :=
<<"MessageEvent:{type=SENT, id=5555, uncompressed_size=200, "
"compressed_size=100}">>}],
<<"debug">> := false,
<<"id">> := ChildSpanId,
<<"localEndpoint">> := #{<<"serviceName">> := "ct-service"},
Expand Down