Skip to content

Commit

Permalink
wip: restructure tracer around registry
Browse files Browse the repository at this point in the history
This is to be split into separate OTP applications to make the
distinction clearer. For now the SDK registry is started when
opentelemetry application is started. The registry is responsible
for tracking the tracers, the sampler and span processors.

The registry also currently sets a ctx and span module to use. I
think this will change when context is split out.  I'd also like
to consider not having the span module be configurable in the
SDK but always use ot_span_ets and if the user needs a fancy form
of span handling they can replace the SDK if they can't get by
with using span processors.
  • Loading branch information
tsloughter committed Nov 7, 2019
1 parent 7b0ae7f commit 134defb
Show file tree
Hide file tree
Showing 22 changed files with 649 additions and 395 deletions.
10 changes: 5 additions & 5 deletions src/opentelemetry.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
stdlib,
wts
]},
{env, [{tracer, {ot_tracer_default, #{span => {ot_span_ets, []},
ctx => {ot_ctx_pdict, []}}}},
{sampler, {always_on, #{}}},
{exporter, [{exporters, []},
{scheduled_delay_ms, 30000}]}]},
{env, [{sampler, {always_on, #{}}},
{processors, [%% #{id => my_processor,
%% module => ot_batch_processor,
%% config => #{}}
]}]},
{modules, []},

{licenses, ["Apache 2.0"]},
Expand Down
14 changes: 13 additions & 1 deletion src/opentelemetry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
%%%-------------------------------------------------------------------------
-module(opentelemetry).

-export([generate_trace_id/0,
-export([set_default_tracer_registry/1,
get_tracer/0,
get_tracer/1,
generate_trace_id/0,
generate_span_id/0]).

-include("opentelemetry.hrl").
Expand Down Expand Up @@ -93,6 +96,15 @@

-type http_headers() :: [{unicode:unicode_binary(), unicode:unicode_binary()}].

set_default_tracer_registry(TracerRegistry) ->
persistent_term:put({?MODULE, default_tracer_registry}, TracerRegistry).

get_tracer() ->
(persistent_term:get({?MODULE, default_tracer_registry}, ot_registry_api)):get().

get_tracer(Name) ->
(persistent_term:get({?MODULE, default_tracer_registry}, ot_registry_api)):get(Name).

%%--------------------------------------------------------------------
%% @doc
%% Generates a 128 bit random integer to use as a trace id.
Expand Down
13 changes: 1 addition & 12 deletions src/opentelemetry_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,7 @@

start(_StartType, _StartArgs) ->
Opts = application:get_all_env(opentelemetry),

{sampler, {Sampler, SamplerOpts}} = lists:keyfind(sampler, 1, Opts),
SamplerFun = ot_sampler:setup(Sampler, SamplerOpts),

%% The default sampler implementation must be passed to the tracer chosen.
%% Since the tracer is started after the rest of the supervision tree has started
%% and we only get its children, we must instantiate the sampler before anything else.
%% The sampler turns out to be instantiated before any supervision tree is started.
{tracer, {Tracer, TracerOpts}} = lists:keyfind(tracer, 1, Opts),
TracerChildren = ot_tracer:setup(Tracer, TracerOpts, SamplerFun),

opentelemetry_sup:start_link(TracerChildren, Opts).
opentelemetry_sup:start_link(Opts).

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

-behaviour(supervisor).

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

-export([init/1]).

-define(SERVER, ?MODULE).

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

init([Children, Opts]) ->
init([Opts]) ->
SupFlags = #{strategy => one_for_one,
intensity => 0,
period => 1},

ExporterOpts = proplists:get_value(exporter, Opts, []),
Exporter = #{id => ot_exporter,
start => {ot_exporter, start_link, [ExporterOpts]},
restart => permanent,
shutdown => 1000,
type => worker,
modules => [ot_exporter]},

ChildSpecs = [#{id => ot_span_sup,
start => {ot_span_sup, start_link, [Opts]},
type => supervisor},
Exporter | Children],
intensity => 1,
period => 5},

RegistrySup = #{id => ot_registry_sup,
start => {ot_registry_sup, start_link, [Opts]},
restart => permanent,
shutdown => 1000,
type => supervisor,
modules => [ot_registry_sup]},

ChildSpecs = [RegistrySup],
{ok, {SupFlags, ChildSpecs}}.

%% internal functions
Loading

0 comments on commit 134defb

Please sign in to comment.