Skip to content

Commit

Permalink
update ctx implementations for latest api
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Nov 15, 2019
1 parent d7037ba commit 21d75b9
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/opentelemetry_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

start(_StartType, _StartArgs) ->
Opts = application:get_all_env(opentelemetry),
opentelemetry:set_default_context_manager(ot_ctx_pdict),
opentelemetry_sup:start_link(Opts).

stop(_State) ->
Expand Down
65 changes: 55 additions & 10 deletions src/ot_ctx_pdict.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,63 @@

-behaviour(ot_ctx).

-export([with_value/2,
get/2]).
-export([set_value/3,
get_value/2,
get_value/3,
get_current/1,
set_current/2,
clear/1,
remove/2]).

-spec get(term(), term()) -> term().
get(Key, Default) ->
case erlang:get(Key) of
-spec set_value(term(), term(), term()) -> ok.
set_value(Namespace, Key, Value) ->
case erlang:get(Namespace) of
Map when is_map(Map) ->
erlang:put(Namespace, Map#{Key => Value}),
ok;
_ ->
erlang:put(Namespace, #{Key => Value}),
ok
end.

-spec get_value(term(), term()) -> term().
get_value(Namespace, Key) ->
get_value(Namespace, Key, undefined).

-spec get_value(term(), term(), term()) -> term().
get_value(Namespace, Key, Default) ->
case erlang:get(Namespace) of
undefined ->
Default;
Value ->
Value
Map when is_map(Map) ->
maps:get(Key, Map, Default);
_ ->
Default
end.

-spec clear(term()) -> ok.
clear(Namespace) ->
erlang:erase(Namespace).

-spec remove(term(), term()) -> ok.
remove(Namespace, Key) ->
case erlang:get(Namespace) of
Map when is_map(Map) ->
erlang:put(Namespace, maps:remove(Key, Map)),
ok;
_ ->
ok
end.

-spec get_current(term()) -> map().
get_current(Namespace) ->
case erlang:get(Namespace) of
Map when is_map(Map) ->
Map;
_ ->
#{}
end.

-spec with_value(term(), term()) -> ok.
with_value(Key, Value) ->
erlang:put(Key, Value).
-spec set_current(term(), map()) -> ok.
set_current(Namespace, Ctx) ->
erlang:put(Namespace, Ctx).
109 changes: 89 additions & 20 deletions src/ot_ctx_seqtrace.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,108 @@
%%%-------------------------------------------------------------------------
-module(ot_ctx_seqtrace).

-behaviour(ot_ctx).
-export([set_value/3,
get_value/2,
get_value/3,
get_current/1,
set_current/2,
clear/1,
remove/2]).

-export([get/2,
with_value/2]).
-behaviour(ot_ctx).

-include_lib("kernel/include/logger.hrl").

%% needed until type specs for seq_trace are fixed
-dialyzer({nowarn_function, get/2}).
-dialyzer({nowarn_function, with_value/2}).
%% -dialyzer({nowarn_function, get_value/2}).
%% -dialyzer({nowarn_function, set_value/2}).

-spec get(term(), term()) -> term().
get(Key, Default) ->
case seq_trace:get_token(label) of
{label, Label} when is_map(Label) ->
maps:get(Key, Label, Default);
[] ->
undefined;
{label, _Label} ->
log_warning(),
undefined
-spec set_value(term(), term(), term()) -> ok.
set_value(Namespace, Key, Value) ->
case get_context() of
undefined ->
ok;
Context ->
case maps:get(Namespace, Context, undefined) of
NamespaceContext when is_map(NamespaceContext) ->
NamespaceContext1 = maps:put(Key, Value, NamespaceContext),
seq_trace:set_token(label, Context#{Namespace => NamespaceContext1}),
ok;
_ ->
seq_trace:set_token(label, Context#{Namespace => #{Key => Value}}),
ok
end
end.

-spec get_value(term(), term()) -> term().
get_value(Namespace, Key) ->
get_value(Namespace, Key, undefined).

-spec get_value(term(), term(), term()) -> term().
get_value(Namespace, Key, Default) ->
case get_context() of
undefined ->
ok;
Context ->
NamespaceContext = maps:get(Namespace, Context, #{}),
maps:get(Key, NamespaceContext, Default)
end.

-spec with_value(term(), term()) -> ok.
with_value(Key, Value) ->
-spec clear(term()) -> ok.
clear(Namespace) ->
case get_context() of
undefined ->
ok;
Context ->
seq_trace:set_token(label, maps:remove(Namespace, Context)),
ok
end.

-spec remove(term(), term()) -> ok.
remove(Namespace, Key) ->
case get_context() of
undefined ->
ok;
Context ->
case maps:get(Namespace, Context, undefined) of
NamespaceContext when is_map(NamespaceContext) ->
NamespaceContext1 = maps:remove(Key, NamespaceContext),
seq_trace:set_token(label, Context#{Namespace => NamespaceContext1}),
ok;
_ ->
ok
end
end.

-spec get_current(term()) -> map().
get_current(Namespace) ->
case get_context() of
undefined ->
#{};
Context ->
case maps:get(Namespace, Context) of
Map when is_map(Map) ->
Map;
_ ->
#{}
end
end.

-spec set_current(term(), map()) -> ok.
set_current(Namespace, Ctx) ->
erlang:put(Namespace, Ctx).

%% internal functions

get_context() ->
case seq_trace:get_token(label) of
{label, Label} when is_map(Label) ->
seq_trace:set_token(label, Label#{Key => Value});
Label;
[] ->
seq_trace:set_token(label, #{Key => Value});
#{};
{label, _Label} ->
log_warning(),
ok
undefined
end.

log_warning() ->
Expand Down
18 changes: 10 additions & 8 deletions src/ot_tracer_default.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@

-include("ot_tracer.hrl").

-define(TRACER_KEY, '$__ot_tracer_ctx_key').

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

-spec start_span(opentelemetry:tracer(), opentelemetry:span_name(), ot_span:start_opts())
-> opentelemetry:span_ctx().
start_span(Tracer, Name, Opts) when is_map_key(sampler, Opts) ->
case ot_ctx:get(?ctx, ?SPAN_CTX) of
case ot_ctx:get_value(?TRACER_KEY, ?SPAN_CTX) of
{SpanCtx, _}=Ctx ->
Processors = on_start(Tracer),
SpanCtx1 = ot_span_ets:start_span(Name, Opts#{parent => SpanCtx}, Processors),
ot_ctx:with_value(?ctx, ?SPAN_CTX, {SpanCtx1, Ctx}),
ot_ctx:set_value(?TRACER_KEY, ?SPAN_CTX, {SpanCtx1, Ctx}),
SpanCtx1;
_ ->
Processors = on_start(Tracer),
SpanCtx = ot_span_ets:start_span(Name, Opts#{parent => undefined}, Processors),
ot_ctx:with_value(?ctx, ?SPAN_CTX, {SpanCtx, undefined}),
ot_ctx:set_value(?TRACER_KEY, ?SPAN_CTX, {SpanCtx, undefined}),
SpanCtx
end;
start_span(Tracer={_, #tracer{sampler=Sampler}}, Name, Opts) ->
Expand All @@ -62,15 +64,15 @@ on_end({_, #tracer{processors=Processors}}) ->

-spec with_span(opentelemetry:tracer(), opentelemetry:span_ctx()) -> ok.
with_span(_Tracer, SpanCtx) ->
ot_ctx:with_value(?ctx, ?SPAN_CTX, {SpanCtx, undefined}).
ot_ctx:set_value(?TRACER_KEY, ?SPAN_CTX, {SpanCtx, undefined}).

-spec with_span(opentelemetry:tracer(), opentelemetry:span_ctx(), fun()) -> ok.
with_span(_Tracer, SpanCtx, Fun) ->
ot_ctx:with_value(?ctx, ?SPAN_CTX, {SpanCtx, undefined}, Fun).
ot_ctx:set_value(?TRACER_KEY, ?SPAN_CTX, {SpanCtx, undefined}, Fun).

-spec current_span_ctx(opentelemetry:tracer()) -> opentelemetry:span_ctx().
current_span_ctx(_Tracer) ->
case ot_ctx:get(?ctx, ?SPAN_CTX) of
case ot_ctx:get_value(?TRACER_KEY, ?SPAN_CTX) of
{SpanCtx, _ParentPdictSpanCtx} ->
SpanCtx;
_ ->
Expand All @@ -82,7 +84,7 @@ current_span_ctx(_Tracer) ->
%% parent trace context, which contains its parent and so on.
-spec current_ctx() -> pdict_trace_ctx().
current_ctx() ->
ot_ctx:get(?ctx, ?SPAN_CTX).
ot_ctx:get_value(?TRACER_KEY, ?SPAN_CTX).

span_module({_, #tracer{span_module=SpanModule}}) ->
SpanModule.
Expand All @@ -98,7 +100,7 @@ end_span(Tracer) ->
{SpanCtx, ParentCtx} = current_ctx(),
Processors = on_end(Tracer),
ot_span_ets:end_span(SpanCtx, Processors),
ot_ctx:with_value(?ctx, ?SPAN_CTX, ParentCtx),
ot_ctx:set_value(?TRACER_KEY, ?SPAN_CTX, ParentCtx),
ok.

-spec get_binary_format(opentelemetry:tracer()) -> binary().
Expand Down

0 comments on commit 21d75b9

Please sign in to comment.