Skip to content

Commit

Permalink
rename default tracer and move setup to before root sup
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Aug 21, 2019
1 parent bf1b8fc commit 89d610d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/opentelemetry.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
stdlib,
wts
]},
{env, [{tracer, #{span => {ot_span_ets, []},
ctx => {ot_ctx_pdict, []}}}]},
{env, [{tracer, {ot_tracer_default, #{span => {ot_span_ets, []},
ctx => {ot_ctx_pdict, []}}}}]},
{modules, []},

{licenses, ["Apache 2.0"]},
Expand Down
7 changes: 3 additions & 4 deletions src/opentelemetry_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@

start(_StartType, _StartArgs) ->
Opts = application:get_all_env(opentelemetry),
{ok, Pid} = opentelemetry_sup:start_link(Opts),

%% if the span impl needs to have a process supervised it must be
%% setup after the supervision tree has started.
{tracer, TracerOpts} = lists:keyfind(tracer, 1, Opts),
ot_tracer_sdk:setup(TracerOpts),
{tracer, {Tracer, TracerOpts}} = lists:keyfind(tracer, 1, Opts),
Children = ot_tracer:setup(Tracer, TracerOpts),

{ok, Pid}.
opentelemetry_sup:start_link(Children, Opts).

stop(_State) ->
ok.
Expand Down
10 changes: 5 additions & 5 deletions src/opentelemetry_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@

-behaviour(supervisor).

-export([start_link/1]).
-export([start_link/2]).

-export([init/1]).

-define(SERVER, ?MODULE).

start_link(Opts) ->
supervisor:start_link({local, ?SERVER}, ?MODULE, [Opts]).
start_link(Children, Opts) ->
supervisor:start_link({local, ?SERVER}, ?MODULE, [Children, Opts]).

init([Opts]) ->
init([Children, Opts]) ->
SupFlags = #{strategy => one_for_one,
intensity => 0,
period => 1},
ChildSpecs = [#{id => ot_span_sup,
start => {ot_span_sup, start_link, [Opts]},
type => supervisor}],
type => supervisor} | Children],
{ok, {SupFlags, ChildSpecs}}.

%% internal functions
1 change: 0 additions & 1 deletion src/ot_span.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

-export_type([start_opts/0]).

-callback setup(map()) -> ok.
-callback start_span(opentelemetry:span_name(), start_opts()) -> opentelemetry:span_ctx().
-callback finish_span(opentelemetry:span_ctx()) -> ok.
-callback get_ctx(opentelemetry:span()) -> opentelemetry:span_ctx().
Expand Down
8 changes: 1 addition & 7 deletions src/ot_span_ets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
handle_call/3,
handle_cast/2]).

-export([setup/1,
start_span/2,
-export([start_span/2,
finish_span/1,
get_ctx/1,
is_recording_events/1,
Expand All @@ -44,11 +43,6 @@
%% table to store active spans
-define(SPAN_TAB, otel_span_table).

setup(_Opts) ->
{ok, _} = ot_span_sup:start_child(#{id => ?MODULE,
start => {?MODULE, start_link, [[]]}}),
ok.

start_link(Opts) ->
gen_server:start_link(?MODULE, Opts, []).

Expand Down
10 changes: 5 additions & 5 deletions src/ot_span_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ start_span(Name, Opts) ->
%% TODO: support overriding the sampler
_Sampler = maps:get(sampler, Opts, undefined),

new_span_(Name, Parent, Kind, Attributes, Links).
new_span(Name, Parent, Kind, Attributes, Links).

%% if parent is undefined, first run sampler
new_span_(Name, undefined, Kind, Attributes, Links) ->
new_span(Name, undefined, Kind, Attributes, Links) ->
TraceId = opentelemetry:generate_trace_id(),
Span = #span_ctx{trace_id=TraceId,
trace_options=0},
TraceOptions = update_trace_options(should_sample, Span),
new_span_(Name, Span#span_ctx{trace_options=TraceOptions}, Kind, Attributes, Links);
new_span(Name, Span#span_ctx{trace_options=TraceOptions}, Kind, Attributes, Links);
%% if parent is remote, first run sampler
%% new_span_(Name, Span=#span_ctx{}, Kind, Attributes) %% when RemoteParent =:= true
%% ->
%% TraceOptions = update_trace_options(should_sample, Span),
%% new_span_(Name, Span#span_ctx{trace_options=TraceOptions}, Kind, Attributes);
new_span_(Name, Parent=#span_ctx{trace_id=TraceId,
new_span(Name, Parent=#span_ctx{trace_id=TraceId,
trace_options=TraceOptions,
tracestate=Tracestate,
span_id=ParentSpanId}, Kind, Attributes, Links)
Expand All @@ -80,7 +80,7 @@ new_span_(Name, Parent=#span_ctx{trace_id=TraceId,
attributes=Attributes,
links=Links},
{Parent#span_ctx{span_id=SpanId}, Span};
new_span_(_Name, Parent, _Kind, _, _) ->
new_span(_Name, Parent, _Kind, _, _) ->
SpanId = opentelemetry:generate_span_id(),
%% since discarded by sampler, create no span
{Parent#span_ctx{span_id=SpanId}, undefined}.
Expand Down
10 changes: 8 additions & 2 deletions src/ot_tracer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
%%%-------------------------------------------------------------------------
-module(ot_tracer).

-export([start_span/1,
-export([setup/2,
start_span/1,
start_span/2,
start_span/3,
with_span/1,
Expand All @@ -29,7 +30,7 @@

-include("opentelemetry.hrl").

-define(tracer, (persistent_term:get({opentelemetry, tracer}, ot_tracer_sdk))).
-define(tracer, (persistent_term:get({?MODULE, tracer}))).
-define(CURRENT_TRACER, {?MODULE, current_tracer}).

-callback start_span(opentelemetry:span_name(), ot_span:start_opts()) -> opentelemetry:span_ctx().
Expand All @@ -40,6 +41,11 @@
-callback get_binary_format() -> binary().
-callback get_http_text_format() -> opentelemetry:http_headers().

-spec setup(module(), map()) -> [supervisor:child_spec()].
setup(Tracer, TracerOpts) ->
persistent_term:put({?MODULE, tracer}, Tracer),
Tracer:setup(TracerOpts).

-spec start_span(opentelemetry:span_name()) -> opentelemetry:span_ctx().
start_span(Name) ->
start_span(Name, #{}).
Expand Down
27 changes: 14 additions & 13 deletions src/ot_tracer_sdk.erl → src/ot_tracer_default.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
%% @doc
%% @end
%%%-------------------------------------------------------------------------
-module(ot_tracer_sdk).
-module(ot_tracer_default).

-behaviour(ot_tracer).

Expand All @@ -37,19 +37,20 @@

-type pdict_trace_ctx() :: {opentelemetry:span_ctx(), pdict_trace_ctx() | undefined}.

-spec setup(map()) -> [supervisor:child_spec()].
setup(Opts) ->
setup_impl(ctx, ?CTX_IMPL_KEY, Opts),
setup_impl(span, ?SPAN_IMPL_KEY, Opts).

setup_impl(ConfigKey, PersistentKey, Opts) ->
{Impl, Args} = maps:get(ConfigKey, Opts),
case erlang:function_exported(Impl, setup, 1) of
true ->
ok = Impl:setup(Args);
false ->
ok
end,
persistent_term:put(PersistentKey, Impl).
lists:filtermap(fun({ConfigKey, PersistentKey}) ->
{Module, Args} = maps:get(ConfigKey, Opts),
persistent_term:put(PersistentKey, Module),
case erlang:function_exported(Module, start_link, 1) of
true ->
{true, #{id => Module,
start => {Module, start_link, [Args]}}};
false ->
false
end
end, [{ctx, ?CTX_IMPL_KEY},
{span, ?SPAN_IMPL_KEY}]).

-spec start_span(opentelemetry:span_name(), ot_span:start_opts()) -> opentelemetry:span_ctx().
start_span(Name, Opts) ->
Expand Down

0 comments on commit 89d610d

Please sign in to comment.