You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2019/11/06 21:29:14 UTC

[couchdb] branch opentracing-davisp updated: Fix loading of fdb trace data

This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch opentracing-davisp
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/opentracing-davisp by this push:
     new 03f3054  Fix loading of fdb trace data
03f3054 is described below

commit 03f3054e5987bc0c1927bc417c41f3a518391f6b
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Wed Nov 6 15:28:45 2019 -0600

    Fix loading of fdb trace data
---
 src/chttpd/src/chttpd.erl       | 12 +-------
 src/ctrace/src/ctrace.erl       | 32 +++++++++++++++++++--
 support/load-fdb-traces.escript | 61 ++++++++++++-----------------------------
 3 files changed, 48 insertions(+), 57 deletions(-)

diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index 956a635..4712b58 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -152,20 +152,10 @@ stop() ->
     mochiweb_http:stop(?MODULE).
 
 
-ctrace_id() ->
-    TraceId = hexify(ctrace:get_trace_id()),
-    SpanId = hexify(ctrace:get_span_id()),
-    <<TraceId/binary, ":", SpanId/binary>>.
-
-hexify(Value) ->
-    Bin = binary:encode_unsigned(Value, little),
-    fabric2_util:to_hex(Bin).
-
-
 handle_request(MochiReq0) ->
     fabric2_fdb:clear_state(),
     ctrace:start_span('http.request'),
-    erlang:put(erlfdb_trace, ctrace_id()),
+    erlang:put(erlfdb_trace, ctrace:export_span()),
     erlang:put(?REWRITE_COUNT, 0),
     MochiReq = couch_httpd_vhost:dispatch_host(MochiReq0),
     handle_request_int(MochiReq).
diff --git a/src/ctrace/src/ctrace.erl b/src/ctrace/src/ctrace.erl
index 1b7bd88..d6d5d2c 100644
--- a/src/ctrace/src/ctrace.erl
+++ b/src/ctrace/src/ctrace.erl
@@ -37,14 +37,17 @@
     get_trace_id/0,
     get_span_id/0,
     get_tracer/0,
-    get_context/0
+    get_context/0,
+
+    export_span/0,
+    import_span/1
 ]).
 
 
 -include_lib("passage/include/opentracing.hrl").
 
 -define(ENABLED_KEY, '$ctrace_enabled$').
-
+-define(ANCESTORS_KEY, passage_span_ancestors).
 
 -type tags() :: #{atom() => term()}.
 -type log_fields() :: #{atom() => term()}.
@@ -253,3 +256,28 @@ get_context() ->
         false ->
             undefined
     end.
+
+
+-spec export_span() -> binary().
+
+export_span() ->
+    % TODO: Support non-Erlang specific formats
+    Span = passage_pd:current_span(),
+    InjectFun = fun(<<"binary">>, Value, Acc) -> [Value | Acc] end,
+    InitCarrier = [],
+    Carrier = passage:inject_span(Span, binary, InjectFun, InitCarrier),
+    fabric2_util:to_hex(iolist_to_binary(Carrier)).
+
+
+-spec import_span(binary()) -> passage:span().
+
+import_span(Binary) ->
+    IterFun = fun(Carrier) ->
+        case Carrier of
+            [] ->
+                error;
+            [Bin | Rest] ->
+                {ok, <<"binary">>, Bin, Rest}
+        end
+    end,
+    passage:extract_span(jaeger_passage_reporter, binary, IterFun, [Binary]).
diff --git a/support/load-fdb-traces.escript b/support/load-fdb-traces.escript
index e3e1354..ab0def1 100755
--- a/support/load-fdb-traces.escript
+++ b/support/load-fdb-traces.escript
@@ -59,16 +59,18 @@ maybe_create_span(Props) ->
 
 create_span(Type, Props) ->
     %io:format(standard_error, "~p~n", [Props]),
+    OpName = binary_to_atom(Type, utf8),
+    Span = get_span(Props),
     {StartTime, EndTime} = get_time(Props),
-    {TraceId, SpanId} = get_trace_ids(Props),
     Tags = get_tags(Props),
 
-    fake_root(TraceId, SpanId),
-    passage_pd:start_span(binary_to_atom(Type, utf8), [
-        {time, StartTime},
-        {tags, Tags}
-    ]),
-    passage_pd:finish_span([{time, EndTime}]).
+    passage_pd:with_parent_span({child_of, Span}, fun() ->
+        passage_pd:start_span(OpName, [
+            {time, StartTime},
+            {tags, Tags}
+        ]),
+        passage_pd:finish_span([{time, EndTime}])
+    end).
 
 
 get_time(Props) ->
@@ -80,21 +82,17 @@ get_time(Props) ->
 
 
 float_to_time(Val) ->
-    Mega = trunc(Val / 1000000),
-    Secs = trunc(Val) rem 1000000,
-    Micro = trunc(Val * 1000000) rem 1000000,
+    BigVal = trunc(Val * 1000000),
+    Mega = BigVal div 1000000000000,
+    Secs = BigVal div 1000000 rem 1000000,
+    Micro = BigVal rem 1000000,
     {Mega, Secs, Micro}.
 
 
-get_trace_ids(Props) ->
+get_span(Props) ->
     {_, TxId} = lists:keyfind(<<"TransactionID">>, 1, Props),
-    [TraceIdBin, SpanIdBin] = binary:split(TxId, <<":">>),
-    TraceId = mochihex:to_bin(binary_to_list(TraceIdBin)),
-    SpanId = mochihex:to_bin(binary_to_list(SpanIdBin)),
-    {
-        binary:decode_unsigned(TraceId, little),
-        binary:decode_unsigned(SpanId, little)
-    }.
+    DeHexed = mochihex:to_bin(binary_to_list(TxId)),
+    ctrace:import_span(DeHexed).
 
 
 get_tags(Props) ->
@@ -127,29 +125,4 @@ start_tracer() ->
         {agent_port, 6831},
         {default_service_name, 'fdb-client'}
     ],
-    ok = jaeger_passage:start_tracer(main, Sampler, Options).
-
-
-fake_root(TraceId, SpanId) ->
-    Root = [{child_of, {
-        passage_span,
-        main,
-        foo,
-        {0, 0, 0},
-        undefined,
-        [],
-        #{},
-        [],
-        {
-            passage_span_context,
-            {
-                jaeger_passage_span_context,
-                TraceId,
-                SpanId,
-                true,
-                undefined
-            },
-            #{}
-        }
-    }}],
-    put(?ANCESTORS_KEY, Root).
\ No newline at end of file
+    ok = jaeger_passage:start_tracer(jaeger_passage_reporter, Sampler, Options).