Skip to content

Instantly share code, notes, and snippets.

@nickva
Created June 20, 2018 17:56
Show Gist options
  • Select an option

  • Save nickva/51fe603a92d3e11659488ed8ec3ec7c8 to your computer and use it in GitHub Desktop.

Select an option

Save nickva/51fe603a92d3e11659488ed8ec3ec7c8 to your computer and use it in GitHub Desktop.
% works with https://gist.github.com/davisp/626acba57bb0f6da84a1d0de4d7237c3
maybe_trace(MochiReq) ->
case MochiReq:get_header_value("x-couchdb-trace") of
"true" ->
case is_tracer_running() of
true ->
couch_log:info("Tracer updating scope with request pid:~p", [self()]),
% https://github.com/rabbitmq/looking_glass/blob/master/src/lg.erl#L125
Children = supervisor:which_children(looking_glass_sup),
{default, PoolPid, supervisor, _} = lists:keyfind(default, 1, Children),
Tracers = lg_tracer_pool:tracers(PoolPid),
TracersMap = maps:from_list(lists:zip(lists:seq(0, length(Tracers) - 1), Tracers)),
TracerState = #{mode => profile, tracers => TracersMap},
TraceFlags = [call, procs, timestamp, arity, return_to, set_on_spawn],
erlang:trace(self(), true, [{tracer, lg_tracer, TracerState} | TraceFlags]);
false ->
couch_log:info("Trace initializing...", []),
lg:trace(['_', {scope, [self()]}], lg_file_tracer, "traces.lz4", #{mode => profile})
end;
"false" ->
case is_tracer_running() of false -> ok; true ->
couch_log:info("Trace stopping...", []),
lg:stop()
end;
_ ->
ok
end.
is_tracer_running() ->
case whereis(looking_glass_sup) of
undefined ->
false;
Pid when is_pid(Pid) ->
Children = supervisor:which_children(Pid),
lists:keymember(default, 1, Children)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment