diff --git a/src/oc_reporter_zipkin.erl b/src/oc_reporter_zipkin.erl index 1c2db7b..7294157 100644 --- a/src/oc_reporter_zipkin.erl +++ b/src/oc_reporter_zipkin.erl @@ -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) -> diff --git a/test/oc_reporters_SUITE.erl b/test/oc_reporters_SUITE.erl index 5400828..3396282 100644 --- a/test/oc_reporters_SUITE.erl +++ b/test/oc_reporters_SUITE.erl @@ -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), @@ -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"},