Current section

Files

Jump to
opentelemetry src ot_span_ets.erl
Raw

src/ot_span_ets.erl

%%%------------------------------------------------------------------------
%% 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
%% ETS backed interface for working with spans.
%% @end
%%%-------------------------------------------------------------------------
-module(ot_span_ets).
-behaviour(ot_span).
-behaviour(gen_server).
-export([start_link/1,
init/1,
handle_call/3,
handle_cast/2]).
-export([start_span/3,
start_span/4,
end_span/1,
end_span/2,
get_ctx/1,
set_attribute/3,
set_attributes/2,
add_event/3,
add_events/2,
set_status/2,
update_name/2]).
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
-include("ot_tracer.hrl").
-include("ot_span.hrl").
-include("ot_span_ets.hrl").
-record(state, {}).
start_link(Opts) ->
gen_server:start_link(?MODULE, Opts, []).
start_span(Name, Opts, InstrumentationLibrary) ->
start_span(Name, Opts, fun(Span) -> Span end, InstrumentationLibrary).
%% @doc Start a span and insert into the active span ets table.
-spec start_span(opentelemetry:span_name(), ot_span:start_opts(), fun(),
ot_tracer_server:instrumentation_library()) -> opentelemetry:span_ctx().
start_span(Name, Opts, Processors, InstrumentationLibrary) ->
{SpanCtx, Span} = ot_span_utils:start_span(Name, Opts),
Span1 = Span#span{instrumentation_library=InstrumentationLibrary},
Span2 = Processors(Span1),
_ = storage_insert(Span2),
SpanCtx.
end_span(SpanCtx) ->
end_span(SpanCtx, fun(Span) -> Span end).
%% @doc End a span based on its context and send to reporter.
-spec end_span(opentelemetry:span_ctx(), fun()) -> boolean() | {error, term()}.
end_span(#span_ctx{span_id=SpanId,
tracestate=Tracestate,
trace_flags=TraceOptions}, Processors) when ?IS_SPAN_ENABLED(TraceOptions) ->
case ets:take(?SPAN_TAB, SpanId) of
[Span] ->
Span1 = ot_span_utils:end_span(Span#span{tracestate=Tracestate}),
Processors(Span1);
_ ->
false
end;
end_span(_, _) ->
true.
-spec get_ctx(opentelemetry:span()) -> opentelemetry:span_ctx().
get_ctx(#span{trace_id=TraceId,
span_id=SpanId,
tracestate=TraceState,
is_recording=IsRecording}) ->
#span_ctx{trace_id=TraceId,
span_id=SpanId,
tracestate=TraceState,
is_recording=IsRecording}.
-spec set_attribute(opentelemetry:span_ctx(),
opentelemetry:attribute_key(),
opentelemetry:attribute_value()) -> boolean().
set_attribute(#span_ctx{span_id=SpanId}, Key, Value) ->
set_attributes(#span_ctx{span_id=SpanId}, [{Key, Value}]).
%% Note: Spans are referenced through the current active span context in a process
%% and thus modified only by a single process, so concurrent updates of the same field
%% are not a real concern. This allows `add_events' and `set_attributes' to lookup and
%% update only the specific element of the `span' without worrying about it having been
%% changed by another process between the lookup and update.
-spec set_attributes(opentelemetry:span_ctx(), opentelemetry:attributes()) -> boolean().
set_attributes(#span_ctx{span_id=SpanId}, NewAttributes) ->
try ets:lookup_element(?SPAN_TAB, SpanId, #span.attributes) of
Attributes ->
ets:update_element(?SPAN_TAB, SpanId, {#span.attributes, Attributes++NewAttributes})
catch error:badarg ->
false
end.
-spec add_event(opentelemetry:span_ctx(), unicode:unicode_binary(), opentelemetry:attributes()) -> boolean().
add_event(SpanCtx, Name, Attributes) ->
Events = opentelemetry:events([{Name, Attributes}]),
add_events(SpanCtx, Events).
-spec add_events(opentelemetry:span_ctx(), opentelemetry:events()) -> boolean().
add_events(#span_ctx{span_id=SpanId}, NewEvents) ->
try ets:lookup_element(?SPAN_TAB, SpanId, #span.events) of
Events ->
ets:update_element(?SPAN_TAB, SpanId, {#span.events, Events++NewEvents})
catch error:badarg ->
false
end.
-spec set_status(opentelemetry:span_ctx(), opentelemetry:status()) -> boolean().
set_status(#span_ctx{span_id=SpanId}, Status) ->
ets:update_element(?SPAN_TAB, SpanId, {#span.status, Status}).
-spec update_name(opentelemetry:span_ctx(), opentelemetry:span_name()) -> boolean().
update_name(#span_ctx{span_id=SpanId}, Name) ->
ets:update_element(?SPAN_TAB, SpanId, {#span.name, Name}).
%%
storage_insert(Span) ->
ets:insert(?SPAN_TAB, Span).
init(_Opts) ->
%% ets table is required for other parts to not crash so we create
%% it in init and not in a handle_continue or whatever else
case ets:info(?SPAN_TAB, name) of
undefined ->
ets:new(?SPAN_TAB, [named_table, public,
{write_concurrency, true},
{keypos, #span.span_id}]);
_ ->
ok
end,
{ok, #state{}}.
handle_call(_Msg, _From, State) ->
{noreply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.