summaryrefslogtreecommitdiff
path: root/support/load-fdb-traces.escript
blob: ab0def1dcb49c29b2feb5a25f1e3749e8dce6ac2 (plain)
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
#!/usr/bin/env escript
%%! -env ERL_LIBS src -noshell -s escript start

-mode(compile).

-export([main/1]).

-define(ANCESTORS_KEY, passage_span_ancestors).
-define(SPAN_TYPES, [
    <<"TransactionTrace_Commit">>,
    <<"TransactionTrace_Get">>,
    <<"TransactionTrace_GetRange">>,
    <<"TransactionTrace_GetVersion">>
]).
-define(TAG_KEYS, [
    {<<"Key">>, key},
    {<<"StartKey">>, 'start-key'},
    {<<"EndKey">>, 'end-key'},
    {<<"ValueSizeBytes">>, 'value-size-bytes'},
    {<<"RangeSizeBytes">>, 'range-size-bytes'},
    {<<"NumMutations">>, 'num-mutations'},
    {<<"CommitSizeBytes">>, 'commit-size-bytes'}
]).


main(Args) ->
    start_apps(),
    start_tracer(),

    lists:foreach(fun(FileName) ->
        load_file(FileName)
    end, Args),
    timer:sleep(1000).


load_file(FileName) ->
    {ok, Data} = file:read_file(FileName),
    Lines = binary:split(Data, <<"\r\n">>, [global]),
    lists:foreach(fun(Line) ->
        case string:trim(Line) of
            <<>> ->
                ok;
            Else ->
                {Props} = jiffy:decode(Else),
                maybe_create_span(Props)
        end
    end, Lines).


maybe_create_span(Props) ->
    {_, Type} = lists:keyfind(<<"Type">>, 1, Props),
    case lists:member(Type, ?SPAN_TYPES) of
        true ->
            create_span(Type, Props);
        false ->
            ok
    end.


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),
    Tags = get_tags(Props),

    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) ->
    {_, EndTimeBin} = lists:keyfind(<<"Time">>, 1, Props),
    {_, LatencyBin} = lists:keyfind(<<"Latency">>, 1, Props),
    EndTimeFloat = binary_to_float(EndTimeBin),
    Latency = binary_to_float(LatencyBin),
    {float_to_time(EndTimeFloat - Latency), float_to_time(EndTimeFloat)}.


float_to_time(Val) ->
    BigVal = trunc(Val * 1000000),
    Mega = BigVal div 1000000000000,
    Secs = BigVal div 1000000 rem 1000000,
    Micro = BigVal rem 1000000,
    {Mega, Secs, Micro}.


get_span(Props) ->
    {_, TxId} = lists:keyfind(<<"TransactionID">>, 1, Props),
    DeHexed = mochihex:to_bin(binary_to_list(TxId)),
    ctrace:import_span(DeHexed).


get_tags(Props) ->
    lists:foldl(fun({BinKey, AtomKey}, Tags) ->
        case lists:keyfind(BinKey, 1, Props) of
            {_, Value} ->
                Tags#{AtomKey => Value};
            false ->
                Tags
        end
    end, #{}, ?TAG_KEYS).


start_apps() ->
    Apps = [
        jiffy,
        passage,
        jaeger_passage
    ],
    lists:foreach(fun(App) ->
        {ok, _} = application:ensure_all_started(App)
    end, Apps).


start_tracer() ->
    Sampler = passage_sampler_all:new(),
    Options = [
        {thrift_format, compact},
        {agent_host, "127.0.0.1"},
        {agent_port, 6831},
        {default_service_name, 'fdb-client'}
    ],
    ok = jaeger_passage:start_tracer(jaeger_passage_reporter, Sampler, Options).