Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set is_recording to false when span is ended #137

Merged
merged 2 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/opentelemetry/src/otel_span_ets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ end_span(#span_ctx{span_id=SpanId,
tracestate=Tracestate}, Processors) ->
case ets:take(?SPAN_TAB, SpanId) of
[Span] ->
Span1 = otel_span_utils:end_span(Span#span{tracestate=Tracestate}),
Span1 = otel_span_utils:end_span(Span#span{tracestate=Tracestate,
is_recording=false}),
Processors(Span1);
_ ->
false
Expand Down
4 changes: 3 additions & 1 deletion apps/opentelemetry_api/include/otel_tracer.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
-define(set_current_span(SpanCtx),
otel_tracer:set_current_span(SpanCtx)).

%% use tracer call to ensure the current span is overwritten with
%% an ended (`is_recording' set to `false') is put in the context
-define(end_span(),
otel_span:end_span(?current_span_ctx)).
otel_tracer:end_span()).

-define(end_span(SpanCtx),
otel_span:end_span(SpanCtx)).
Expand Down
2 changes: 1 addition & 1 deletion apps/opentelemetry_api/lib/open_telemetry/span.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule OpenTelemetry.Span do
End the Span. Sets the end timestamp for the currently active Span. This has no effect on any
child Spans that may exist of this Span.

The Context is unchanged.
The Span Context is returned with `is_recording` set to `false`.
"""
defdelegate end_span(span_ctx), to: :otel_span

Expand Down
4 changes: 2 additions & 2 deletions apps/opentelemetry_api/lib/open_telemetry/tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ defmodule OpenTelemetry.Tracer do
End the Span. Sets the end timestamp for the currently active Span. This has no effect on any
child Spans that may exist of this Span.

The Context is unchanged.
The Span in the current Context has its `is_recording` set to `false`.
"""
def end_span() do
:otel_span.end_span(:otel_tracer.current_span_ctx())
:otel_tracer.end_span()
end

@doc """
Expand Down
29 changes: 13 additions & 16 deletions apps/opentelemetry_api/src/opentelemetry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
meter/0,
trace_id/0,
span_id/0,
trace_flags/0,
timestamp/0,
span_name/0,
span_ctx/0,
Expand All @@ -89,6 +90,8 @@
-type trace_id() :: non_neg_integer().
-type span_id() :: non_neg_integer().

-type trace_flags() :: non_neg_integer().

-type timestamp() :: integer().

-type span_ctx() :: #span_ctx{}.
Expand Down Expand Up @@ -355,30 +358,24 @@ uniform(X) ->

-spec verify_and_set_term(module() | {module(), term()}, term(), atom()) -> boolean().
verify_and_set_term(Module, TermKey, Behaviour) ->
case verify_behaviour(Module, Behaviour) of
case verify_module_exists(Module) of
true ->
persistent_term:put({?MODULE, TermKey}, Module),
true;
false ->
?LOG_WARNING("Module ~p does not implement behaviour ~p. "
"A noop ~p will be used until a module implementing "
"the behaviour is configured.",
?LOG_WARNING("Module ~p does not exist. "
"A noop ~p will be used until a module is configured.",
[Module, Behaviour, Behaviour]),
false
end.

-spec verify_behaviour(module() | {module(), term()}, atom()) -> boolean().
verify_behaviour({Module, _}, Behaviour) ->
verify_behaviour(Module, Behaviour);
verify_behaviour(Module, Behaviour) ->
try Module:module_info(attributes) of
Attributes ->
case lists:keyfind(behaviour, 1, Attributes) of
{behaviour, Behaviours} ->
lists:member(Behaviour, Behaviours);
_ ->
false
end
-spec verify_module_exists(module() | {module(), term()}) -> boolean().
verify_module_exists({Module, _}) ->
verify_module_exists(Module);
verify_module_exists(Module) ->
try Module:module_info() of
_ ->
true
catch
error:undef ->
false
Expand Down
9 changes: 5 additions & 4 deletions apps/opentelemetry_api/src/otel_span.erl
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ update_name(SpanCtx=#span_ctx{span_sdk={Module, _}}, SpanName) when ?is_recordin
update_name(_, _) ->
false.

-spec end_span(SpanCtx) -> boolean() when
-spec end_span(SpanCtx) -> SpanCtx when
SpanCtx :: opentelemetry:span_ctx().
end_span(SpanCtx=#span_ctx{span_sdk={Module, _}}) when ?is_recording(SpanCtx) ->
Module:end_span(SpanCtx);
end_span(_) ->
false.
_ = Module:end_span(SpanCtx),
SpanCtx#span_ctx{is_recording=false};
end_span(SpanCtx) ->
SpanCtx.
7 changes: 5 additions & 2 deletions apps/opentelemetry_api/src/otel_tracer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ text_map_propagators(Module) ->

%% Span operations

-spec end_span() -> boolean() | {error, term()}.
-spec end_span() -> opentelemetry:span_ctx().
end_span() ->
otel_span:end_span(current_span_ctx()).
EndedSpanCtx = otel_span:end_span(current_span_ctx()),
%% this is done to set `is_recording' to `false' after ending
_ = set_current_span(EndedSpanCtx),
EndedSpanCtx.

-spec set_attribute(Key, Value) -> boolean() when
Key :: opentelemetry:attribute_key(),
Expand Down
2 changes: 2 additions & 0 deletions apps/opentelemetry_api/test/opentelemetry_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ update_span_data(_Config) ->
?assertMatch(SpanCtx1, ?current_span_ctx),
?end_span(),

?assertMatch(#span_ctx{is_recording=false}, ?current_span_ctx),

ok.

noop_with_span(_Config) ->
Expand Down
4 changes: 3 additions & 1 deletion test/otel_tests.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule OtelTests do
require Record
@fields Record.extract(:span, from_lib: "opentelemetry/include/otel_span.hrl")
Record.defrecordp(:span, @fields)
@fields Record.extract(:span_ctx, from_lib: "opentelemetry_api/include/opentelemetry.hrl")
Record.defrecordp(:span_ctx, @fields)

test "use Tracer to set attributes" do
:otel_batch_processor.set_exporter(:otel_exporter_pid, self())
Expand All @@ -28,7 +30,7 @@ defmodule OtelTests do
Span.set_attribute(s, "attr-1", "value-1")
Span.set_attributes(s, [{"attr-2", "value-2"}])

assert :true = Span.end_span(s)
assert span_ctx() = Span.end_span(s)

assert_receive {:span, span(name: "span-2", attributes: [{"attr-1", "value-1"},
{"attr-2", "value-2"}])}
Expand Down