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

Commit

Permalink
convert annotations to string values in zipkin reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Oct 6, 2018
1 parent 956fb6a commit 8e9b1f3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
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),
", 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
15 changes: 14 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,13 @@ 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

0 comments on commit 8e9b1f3

Please sign in to comment.