Skip to content

Commit

Permalink
create the on_start/on_end fun on tracer init
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Nov 30, 2019
1 parent 975dba9 commit cbc4c16
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/ot_span_sweeper.erl
Expand Up @@ -33,6 +33,7 @@

-include_lib("opentelemetry_api/include/opentelemetry.hrl").
-include("ot_span_ets.hrl").
-include("ot_tracer.hrl").
-include_lib("kernel/include/logger.hrl").

-record(data, {interval :: integer() | infinity,
Expand Down Expand Up @@ -161,6 +162,5 @@ expired_match_spec(Time, Return) ->
end_span(Span=#span{tracestate=Tracestate}) ->
%% hack to not lose tracestate when ending without span ctx
Span1 = ot_span_utils:end_span(Span#span{tracestate=Tracestate}),
Tracer = opentelemetry:get_tracer(),
Processors = ot_tracer_default:on_end(Tracer),
{_, #tracer{on_end_processors=Processors}} = opentelemetry:get_tracer(),
Processors(Span1).
3 changes: 2 additions & 1 deletion src/ot_tracer.hrl
Expand Up @@ -7,6 +7,7 @@
module :: module(),
span_module :: module(),
ctx_module :: module(),
processors :: [{module(), term()}],
on_start_processors :: fun((opentelemetry:span()) -> opentelemetry:span()),
on_end_processors :: fun((opentelemetry:span()) -> boolean() | {error, term()}),
sampler :: ot_sampler:sampler(),
resource :: term() | undefined}).
13 changes: 2 additions & 11 deletions src/ot_tracer_default.erl
Expand Up @@ -23,7 +23,6 @@
with_span/2,
with_span/3,
end_span/1,
on_end/1,
current_span_ctx/1,
b3_propagators/0,
w3c_propagators/0]).
Expand All @@ -44,8 +43,7 @@ start_span(Tracer, Name, Opts) when is_map_key(sampler, Opts) ->
start_span(Tracer={_, #tracer{sampler=Sampler}}, Name, Opts) ->
start_span(Tracer, Name, Opts#{sampler => Sampler}).

start_span_(Tracer, Name, Opts) when is_map_key(parent, Opts) ->
Processors = on_start(Tracer),
start_span_({_, #tracer{on_start_processors=Processors}}, Name, Opts) when is_map_key(parent, Opts) ->
case maps:get(parent, Opts) of
{ParentSpanCtx, _}=Ctx ->
SpanCtx1 = ot_span_ets:start_span(Name, Opts#{parent => ParentSpanCtx}, Processors),
Expand All @@ -64,12 +62,6 @@ start_span_(Tracer, Name, Opts) ->
start_span_(Tracer, Name, Opts#{parent => undefined})
end.

on_start({_, #tracer{processors=Processors}}) ->
fun(Span) -> [P:on_start(Span, Config) || {P, Config} <- Processors] end.

on_end({_, #tracer{processors=Processors}}) ->
fun(Span) -> [P:on_end(Span, Config) || {P, Config} <- Processors] end.

-spec with_span(opentelemetry:tracer(), opentelemetry:span_ctx()) -> ok.
with_span(_Tracer, SpanCtx) ->
ot_ctx:set_value(?TRACER_KEY, ?SPAN_CTX, {SpanCtx, undefined}).
Expand Down Expand Up @@ -120,9 +112,8 @@ w3c_propagators() ->
%% @end
%%--------------------------------------------------------------------
-spec end_span(opentelemetry:tracer()) -> ok.
end_span(Tracer) ->
end_span({_, #tracer{on_end_processors=Processors}}) ->
{SpanCtx, ParentCtx} = current_ctx(),
Processors = on_end(Tracer),
ot_span_ets:end_span(SpanCtx, Processors),
ot_ctx:set_value(?TRACER_KEY, ?SPAN_CTX, ParentCtx),
ok.
Expand Down
11 changes: 10 additions & 1 deletion src/ot_tracer_server.erl
Expand Up @@ -44,7 +44,8 @@ init(Opts) ->

Tracer = #tracer{module=ot_tracer_default,
sampler=SamplerFun,
processors=Processors,
on_start_processors=on_start(Processors),
on_end_processors=on_end(Processors),
span_module=ot_span_ets,
ctx_module=ot_ctx_pdict},
opentelemetry:set_default_tracer({ot_tracer_default, Tracer}),
Expand All @@ -60,3 +61,11 @@ handle_call(_Msg, _From, State) ->

handle_cast(_Msg, State) ->
{noreply, State}.

%%

on_start(Processors) ->
fun(Span) -> [P:on_start(Span, Config) || {P, Config} <- Processors] end.

on_end(Processors) ->
fun(Span) -> [P:on_end(Span, Config) || {P, Config} <- Processors] end.

0 comments on commit cbc4c16

Please sign in to comment.