From f50c08f2314a9cff11b4bb02f223a925357d9f1b Mon Sep 17 00:00:00 2001 From: Tristan Sloughter Date: Fri, 15 Nov 2019 09:52:08 -0700 Subject: [PATCH] separate context propagation otep --- src/opentelemetry.erl | 8 +++ src/ot_baggage.erl | 61 ++++++++++++++++++++++ src/ot_correlations.erl | 47 +++++++++++++++++ src/ot_ctx.erl | 89 ++++++++++++++++++++++++++++++++ src/ot_ctx_noop.erl | 51 ++++++++++++++++++ src/ot_propagation.erl | 37 +++++++++++++ test/opentelemetry_api_SUITE.erl | 13 ++--- 7 files changed, 298 insertions(+), 8 deletions(-) create mode 100644 src/ot_baggage.erl create mode 100644 src/ot_correlations.erl create mode 100644 src/ot_ctx.erl create mode 100644 src/ot_ctx_noop.erl create mode 100644 src/ot_propagation.erl diff --git a/src/opentelemetry.erl b/src/opentelemetry.erl index 642b8f3..7bbc49b 100644 --- a/src/opentelemetry.erl +++ b/src/opentelemetry.erl @@ -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, @@ -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, []}). diff --git a/src/ot_baggage.erl b/src/ot_baggage.erl new file mode 100644 index 0000000..44d9ad9 --- /dev/null +++ b/src/ot_baggage.erl @@ -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. diff --git a/src/ot_correlations.erl b/src/ot_correlations.erl new file mode 100644 index 0000000..ef911ab --- /dev/null +++ b/src/ot_correlations.erl @@ -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. diff --git a/src/ot_ctx.erl b/src/ot_ctx.erl new file mode 100644 index 0000000..b713cbc --- /dev/null +++ b/src/ot_ctx.erl @@ -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). diff --git a/src/ot_ctx_noop.erl b/src/ot_ctx_noop.erl new file mode 100644 index 0000000..9e38b4c --- /dev/null +++ b/src/ot_ctx_noop.erl @@ -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) -> + #{}. diff --git a/src/ot_propagation.erl b/src/ot_propagation.erl new file mode 100644 index 0000000..d45dd57 --- /dev/null +++ b/src/ot_propagation.erl @@ -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]). diff --git a/test/opentelemetry_api_SUITE.erl b/test/opentelemetry_api_SUITE.erl index 445a494..700bb08 100644 --- a/test/opentelemetry_api_SUITE.erl +++ b/test/opentelemetry_api_SUITE.erl @@ -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.