Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Commit

Permalink
separate context propagation otep
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Nov 15, 2019
1 parent 8d4ef06 commit f50c08f
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/opentelemetry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
-module(opentelemetry).

-export([set_default_tracer/1,
set_default_context_manager/1,
get_context_manager/0,
get_tracer/0,
get_tracer/1,
timestamp/0,
Expand Down Expand Up @@ -106,6 +108,12 @@
set_default_tracer(Tracer) ->
persistent_term:put({?MODULE, default_tracer}, Tracer).

set_default_context_manager(ContextModule) ->
persistent_term:put({?MODULE, context_manager}, ContextModule).

get_context_manager() ->
persistent_term:get({?MODULE, context_manager}, {ot_ctx_noop, []}).

get_tracer() ->
persistent_term:get({?MODULE, default_tracer}, {ot_tracer_noop, []}).

Expand Down
61 changes: 61 additions & 0 deletions src/ot_baggage.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
%%%------------------------------------------------------------------------
%% Copyright 2019, OpenTelemetry Authors
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc
%% @end
%%%-------------------------------------------------------------------------
-module(ot_baggage).

-export([ctx_key/0,
set/2,
get/1,
remove/1,
clear/0,
get_http_extractor/0,
get_http_injector/0]).

-type key() :: string().
-type value() :: string().

-export_type([key/0,
value/0]).

-define(BAGGAGE_KEY, '$__ot_baggage_ctx_key').

ctx_key() ->
?BAGGAGE_KEY.

-spec set(key(), value()) -> ok.
set(Key, Value) ->
ot_ctx:set_value(?BAGGAGE_KEY, Key, Value).

-spec get(key()) -> value().
get(Key)->
ot_ctx:set_value(?BAGGAGE_KEY, Key).

-spec remove(key()) -> ok.
remove(Key) ->
ot_ctx:remove(?BAGGAGE_KEY, Key).

-spec clear() -> ok.
clear() ->
ot_ctx:clear(?BAGGAGE_KEY).

-spec get_http_extractor() -> ot_propagation:extractor().
get_http_extractor() ->
ok.

-spec get_http_injector() -> ot_propagation:injector().
get_http_injector() ->
ok.
47 changes: 47 additions & 0 deletions src/ot_correlations.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
%%%------------------------------------------------------------------------
%% Copyright 2019, OpenTelemetry Authors
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc
%% @end
%%%-------------------------------------------------------------------------
-module(ot_correlations).

-export([ctx_key/0,
set/3,
get_http_extractor/0,
get_http_injector/0]).

-type key() :: string().
-type value() :: string().
-type hop_limit() :: no_propagation | unlimited_propagation.

-export_type([key/0,
value/0]).

-define(CORRELATIONS_KEY, '$__ot_correlations_ctx_key').

ctx_key() ->
?CORRELATIONS_KEY.

-spec set(key(), value(), hop_limit()) -> ok.
set(Key, Value, HopLimit) ->
ot_ctx:set_value(?CORRELATIONS_KEY, Key, {Value, HopLimit}).

-spec get_http_extractor() -> ot_propagation:extractor().
get_http_extractor() ->
ok.

-spec get_http_injector() -> ot_propagation:injector().
get_http_injector() ->
ok.
89 changes: 89 additions & 0 deletions src/ot_ctx.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
%%%------------------------------------------------------------------------
%% Copyright 2019, OpenTelemetry Authors
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc
%% @end
%%%-------------------------------------------------------------------------
-module(ot_ctx).

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

-type ctx() :: map().
-type namespace() :: term().
-type key() :: string().
-type value() :: string().

-callback set_value(namespace(), key(), value()) -> ok.
-callback get_value(namespace(), key()) -> value().
-callback remove(namespace(), key()) -> ok.
-callback clear(namespace()) -> ok.
-callback set_current(namespace(), ctx()) -> ok.
-callback get_current(namespace()) -> ctx().

-export_type([ctx/0,
key/0,
value/0]).

-spec set_value(namespace(), key(), value()) -> ok.
set_value(Namespace, Key, Value) ->
set_value(opentelemetry:get_context_manager(), Namespace, Key, Value).

-spec set_value(module(), namespace(), key(), value()) -> ok.
set_value(CtxModule, Namespace, Key, Value) ->
CtxModule:set_value(Namespace, Key, Value).

-spec get_value(namespace(), key()) -> value().
get_value(Namespace, Key) ->
get_value(opentelemetry:get_context_manager(), Namespace, Key).

-spec get_value(module(), namespace(), key()) -> value().
get_value(Module, Namespace, Key) ->
Module:get_value(Namespace, Key).

-spec remove(namespace(), key()) -> ok.
remove(Namespace, Key) ->
remove(opentelemetry:get_context_manager(), Namespace, Key).

-spec remove(module(), namespace(), key()) -> ok.
remove(CtxModule, Namespace, Key) ->
CtxModule:remove(Namespace, Key).

-spec clear(namespace()) -> ok.
clear(Namespace) ->
clear(opentelemetry:get_context_manager(), Namespace).

-spec clear(module(), namespace()) -> ok.
clear(CtxModule, Namespace) ->
CtxModule:clear(Namespace).

-spec set_current(namespace(), ctx()) -> ok.
set_current(Namespace, Ctx) ->
set_current(opentelemetry:get_context_manager(), Namespace, Ctx).

-spec set_current(module(), namespace(), ctx()) -> ok.
set_current(CtxModule, Namespace, Ctx) ->
CtxModule:set_current(Namespace, Ctx).

-spec get_current(namespace()) -> ctx().
get_current(Namespace) ->
get_current(opentelemetry:get_context_manager(), Namespace).

-spec get_current(module(), namespace()) -> ctx().
get_current(CtxModule, Namespace) ->
CtxModule:get_current(Namespace).
51 changes: 51 additions & 0 deletions src/ot_ctx_noop.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
%%%------------------------------------------------------------------------
%% Copyright 2019, OpenTelemetry Authors
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc
%% @end
%%%-------------------------------------------------------------------------
-module(ot_ctx_noop).

-behaviour(ot_ctx).

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

-spec set_value(ot_ctx:namespace(), ot_ctx:key(), ot_ctx:value()) -> ok.
set_value(_Namespace, _Key, _Value) ->
ok.

-spec get_value(ot_ctx:namespace(), ot_ctx:key()) -> ot_ctx:value().
get_value(_Namespace, _Key) ->
undefined.

-spec remove(ot_ctx:namespace(), ot_ctx:key()) -> ok.
remove(_Namespace, _Key) ->
ok.

-spec clear(ot_ctx:namespace()) -> ok.
clear(_Namespace) ->
ok.

-spec set_current(ot_ctx:namespace(), ot_ctx:ctx()) -> ok.
set_current(_Namespace, _Ctx) ->
ok.

-spec get_current(ot_ctx:namespace()) -> ot_ctx:ctx().
get_current(_Namespace) ->
#{}.
37 changes: 37 additions & 0 deletions src/ot_propagation.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
%%%------------------------------------------------------------------------
%% Copyright 2019, OpenTelemetry Authors
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc
%% @end
%%%-------------------------------------------------------------------------
-module(ot_propagation).

-export([]).

-type extractor() :: fun().
-type injector() :: fun().
-type http_headers() :: [{string(), string()}].

-callback http_inject(ot_ctx:ctx()) -> http_headers().
-callback http_extract(ot_ctx:ctx(), http_headers()) -> ot_ctx:ctx().

-callback set_http_injector(injector()) -> ok.
-callback get_http_injector() -> injector().

-callback set_http_extractor(extractor()) -> ok.
-callback get_http_extractor() -> extractor().

-export_type([extractor/0,
injector/0,
http_headers/0]).
13 changes: 5 additions & 8 deletions test/opentelemetry_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,16 @@ update_span_data(_Config) ->
SpanCtx1 = otel:start_span(<<"span-1">>, #{links => Links}),
otel:set_attribute(<<"key-1">>, <<"value-1">>),

Annotation = #annotation{description = <<"desc">>,
attributes=[]},
Status = #status{code=0,
message = <<"status">>},
TimedEvents = opentelemetry:timed_events([{opentelemetry:timestamp(), <<"timed-event-name">>, []}]),
Status = opentelemetry:status(0, <<"status">>),

%% with spanctx and tracer passed as an argument
Tracer = opentelemetry:get_tracer(),
ot_span:set_status(Tracer, SpanCtx1, Status),

TimeEvents = [{wts:timestamp(), Annotation}],
ot_span:add_events(Tracer, SpanCtx1, TimeEvents),
ot_span:add_events(Tracer, SpanCtx1, TimedEvents),

?assertMatch(SpanCtx1, otel:current_span_ctx()),
otel:end_span().
otel:end_span(),

%%
ok.

0 comments on commit f50c08f

Please sign in to comment.