Skip to content

update sampler to take a context and update some names to match spec #134

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

Merged
merged 2 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions apps/opentelemetry/include/otel_sampler.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
%% @end
%%%-------------------------------------------------------------------------

-define(NOT_RECORD, not_record).
-define(RECORD, record).
-define(RECORD_AND_SAMPLED, record_and_sampled).
-define(DROP, drop).
-define(RECORD_ONLY, record_only).
-define(RECORD_AND_SAMPLE, record_and_sample).
2 changes: 1 addition & 1 deletion apps/opentelemetry/include/otel_span.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
status :: opentelemetry:status() | undefined,

%% 8-bit integer, lowest bit is if it is sampled
trace_options = 1 :: integer() | undefined,
trace_flags = 1 :: integer() | undefined,

%% this field is not propagated and is only here as an implementation optimization
%% If true updates like adding events are done on the span. The same as if the
Expand Down
2 changes: 2 additions & 0 deletions apps/opentelemetry/src/otel_batch_processor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ on_start(_Ctx, Span, _) ->

-spec on_end(opentelemetry:span(), otel_span_processor:processor_config())
-> true | dropped | {error, invalid_span} | {error, no_export_buffer}.
on_end(#span{trace_flags=TraceFlags}, _) when not(?IS_SAMPLED(TraceFlags)) ->
dropped;
on_end(Span=#span{}, _) ->
do_insert(Span);
on_end(_Span, _) ->
Expand Down
8 changes: 4 additions & 4 deletions apps/opentelemetry/src/otel_propagator_http_b3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
-define(B3_SPAN_ID, <<"X-B3-SpanId">>).
-define(B3_SAMPLED, <<"X-B3-Sampled">>).

-define(IS_SAMPLED(S), S =:= "1" orelse S =:= <<"1">> orelse S =:= "true" orelse S =:= <<"true">>).
-define(B3_IS_SAMPLED(S), S =:= "1" orelse S =:= <<"1">> orelse S =:= "true" orelse S =:= <<"true">>).

-spec inject(opentelemetry:span_ctx() | undefined) -> otel_propagator:text_map().
inject(#span_ctx{trace_id=TraceId,
Expand All @@ -54,9 +54,9 @@ extract(Headers, _) when is_list(Headers) ->
TraceId = trace_id(Headers),
SpanId = span_id(Headers),
Sampled = lookup(?B3_SAMPLED, Headers),
#span_ctx{trace_id=string_to_integer(TraceId, 16),
span_id=string_to_integer(SpanId, 16),
trace_flags=case Sampled of True when ?IS_SAMPLED(True) -> 1; _ -> 0 end}
otel_tracer:non_recording_span(string_to_integer(TraceId, 16),
string_to_integer(SpanId, 16),
case Sampled of True when ?B3_IS_SAMPLED(True) -> 1; _ -> 0 end)
catch
throw:invalid ->
undefined;
Expand Down
6 changes: 3 additions & 3 deletions apps/opentelemetry/src/otel_propagator_http_w3c.erl
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ to_span_ctx(Version, TraceId, SpanId, Opts) ->
try
%% verify version is hexadecimal
_ = binary_to_integer(Version, 16),
#span_ctx{trace_id=binary_to_integer(TraceId, 16),
span_id=binary_to_integer(SpanId, 16),
trace_flags=case Opts of <<"01">> -> 1; <<"00">> -> 0; _ -> error(badarg) end}
otel_tracer:non_recording_span(binary_to_integer(TraceId, 16),
binary_to_integer(SpanId, 16),
case Opts of <<"01">> -> 1; <<"00">> -> 0; _ -> error(badarg) end)
catch
%% to integer from base 16 string failed
error:badarg ->
Expand Down
45 changes: 27 additions & 18 deletions apps/opentelemetry/src/otel_sampler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@

-callback setup(module(), map()) -> t().

-type sampling_decision() :: ?NOT_RECORD | ?RECORD | ?RECORD_AND_SAMPLED.
-type sampling_decision() :: ?DROP | ?RECORD_ONLY | ?RECORD_AND_SAMPLE.
-type sampling_result() :: {sampling_decision(), opentelemetry:attributes(), opentelemetry:tracestate()}.
-type description() :: unicode:unicode_binary().
-type sampler() :: {fun((opentelemetry:trace_id(),
opentelemetry:span_ctx() | undefined,
-type sampler() :: {fun((otel_ctx:t(),
opentelemetry:trace_id(),
opentelemetry:links(),
opentelemetry:span_name(),
opentelemetry:kind(),
Expand Down Expand Up @@ -71,11 +71,11 @@ setup(trace_id_ratio_based, Probability) ->
setup(Sampler, Opts) ->
Sampler:setup(Opts).

always_on(_TraceId, SpanCtx, _Links, _SpanName, _Kind, _Attributes, _Opts) ->
{?RECORD_AND_SAMPLED, [], tracestate(SpanCtx)}.
always_on(Ctx, _TraceId, _Links, _SpanName, _Kind, _Attributes, _Opts) ->
{?RECORD_AND_SAMPLE, [], tracestate(Ctx)}.

always_off(_TraceId, SpanCtx, _Links, _SpanName, _Kind, _Attributes, _Opts) ->
{?NOT_RECORD, [], tracestate(SpanCtx)}.
always_off(Ctx, _TraceId, _Links, _SpanName, _Kind, _Attributes, _Opts) ->
{?DROP, [], tracestate(Ctx)}.

-spec get_description(sampler()) -> description().
get_description({_Fun, Description, _Opts}) ->
Expand Down Expand Up @@ -106,22 +106,23 @@ parent_based_config(Opts) ->
?LOG_INFO("no root opt found for sampler parent_based. always_on will be used for root spans"),
parent_based_config(Opts#{root => {always_on, #{}}}).

parent_based(TraceId, SpanCtx, Links, SpanName, Kind, Attributes, Opts) ->
{Sampler, _Description, SamplerOpts} = parent_based_sampler(SpanCtx, Opts),
Sampler(TraceId, SpanCtx, Links, SpanName, Kind, Attributes, SamplerOpts).
parent_based(Ctx, TraceId, Links, SpanName, Kind, Attributes, Opts) ->
ParentSpanCtx = otel_tracer:current_span_ctx(Ctx),
{Sampler, _Description, SamplerOpts} = parent_based_sampler(ParentSpanCtx, Opts),
Sampler(Ctx, TraceId, Links, SpanName, Kind, Attributes, SamplerOpts).

%% remote parent sampled
parent_based_sampler(#span_ctx{trace_flags=TraceFlags,
is_remote=true}, #{remote_parent_sampled := SamplerAndOpts})
when ?IS_SPAN_ENABLED(TraceFlags) ->
when ?IS_SAMPLED(TraceFlags) ->
SamplerAndOpts;
%% remote parent not sampled
parent_based_sampler(#span_ctx{is_remote=true}, #{remote_parent_not_sampled := SamplerAndOpts}) ->
SamplerAndOpts;
%% local parent sampled
parent_based_sampler(#span_ctx{trace_flags=TraceFlags,
is_remote=false}, #{local_parent_sampled := SamplerAndOpts})
when ?IS_SPAN_ENABLED(TraceFlags) ->
when ?IS_SAMPLED(TraceFlags) ->
SamplerAndOpts;
%% local parent not sampled
parent_based_sampler(#span_ctx{is_remote=false}, #{local_parent_not_sampled := SamplerAndOpts}) ->
Expand All @@ -130,20 +131,28 @@ parent_based_sampler(#span_ctx{is_remote=false}, #{local_parent_not_sampled := S
parent_based_sampler(_SpanCtx, #{root := SamplerAndOpts}) ->
SamplerAndOpts.

trace_id_ratio_based(TraceId, SpanCtx, _, _, _, _, IdUpperBound) ->

trace_id_ratio_based(Ctx, undefined, _, _, _, _, _IdUpperBound) ->
{?DROP, [], tracestate(Ctx)};
trace_id_ratio_based(Ctx, 0, _, _, _, _, _IdUpperBound) ->
{?DROP, [], tracestate(Ctx)};
trace_id_ratio_based(Ctx, TraceId, _, _, _, _, IdUpperBound) ->
Lower64Bits = TraceId band ?MAX_VALUE,
case erlang:abs(Lower64Bits) < IdUpperBound of
true ->
{?RECORD_AND_SAMPLED, [], tracestate(SpanCtx)};
{?RECORD_AND_SAMPLE, [], tracestate(Ctx)};
false ->
{?NOT_RECORD, [], tracestate(SpanCtx)}
{?DROP, [], tracestate(Ctx)}
end.

tracestate(#span_ctx{tracestate=undefined}) ->
tracestate(Ctx) ->
tracestate_(otel_tracer:current_span_ctx(Ctx)).

tracestate_(#span_ctx{tracestate=undefined}) ->
[];
tracestate(#span_ctx{tracestate=TraceState}) ->
tracestate_(#span_ctx{tracestate=TraceState}) ->
TraceState;
tracestate(undefined) ->
tracestate_(undefined) ->
[].

description(always_on, _) ->
Expand Down
32 changes: 20 additions & 12 deletions apps/opentelemetry/src/otel_span_ets.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
handle_call/3,
handle_cast/2]).

-export([start_span/6,
-export([start_span/5,
end_span/1,
end_span/2,
get_ctx/1,
Expand All @@ -47,24 +47,30 @@ start_link(Opts) ->
gen_server:start_link(?MODULE, Opts, []).

%% @doc Start a span and insert into the active span ets table.
-spec start_span(otel_ctx:t(), opentelemetry:span_name(), opentelemetry:span_ctx() | undefined,
otel_span:start_opts(), fun(), otel_tracer_server:instrumentation_library())
-> opentelemetry:span_ctx().
start_span(Ctx, Name, ParentSpanCtx, Opts, Processors, InstrumentationLibrary) ->
{SpanCtx, Span} = otel_span_utils:start_span(Name, ParentSpanCtx, Opts),
Span1 = Span#span{instrumentation_library=InstrumentationLibrary},
Span2 = Processors(Ctx, Span1),
_ = storage_insert(Span2),
SpanCtx.
-spec start_span(otel_ctx:t(), opentelemetry:span_name(), otel_span:start_opts(),
fun(), otel_tracer_server:instrumentation_library()) -> opentelemetry:span_ctx().
start_span(Ctx, Name, Opts, Processors, InstrumentationLibrary) ->
case otel_span_utils:start_span(Ctx, Name, Opts) of
{SpanCtx=#span_ctx{is_recording=true}, Span=#span{}} ->
Span1 = Span#span{instrumentation_library=InstrumentationLibrary},
Span2 = Processors(Ctx, Span1),
_ = storage_insert(Span2),
SpanCtx;
{SpanCtx, Span=#span{}} ->
%% span isn't recorded so don't run processors
%% but we do insert to ets table?
_ = storage_insert(Span),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a spec thing or just wondering?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea. It is something we need to figure out next and I gave up on completely solving in this PR.

I'm unsure if we need an actual #span{} in the ets table for a recorded but not sampled span.

SpanCtx
end.

end_span(SpanCtx=#span_ctx{span_sdk={_, OnEndProcessors}}) ->
end_span(SpanCtx, OnEndProcessors).

%% @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) ->
is_recording=true,
tracestate=Tracestate}, Processors) ->
case ets:take(?SPAN_TAB, SpanId) of
[Span] ->
Span1 = otel_span_utils:end_span(Span#span{tracestate=Tracestate}),
Expand Down Expand Up @@ -136,6 +142,8 @@ update_name(#span_ctx{span_id=SpanId}, Name) ->

%%

storage_insert(undefined) ->
ok;
storage_insert(Span) ->
ets:insert(?SPAN_TAB, Span).

Expand Down
80 changes: 47 additions & 33 deletions apps/opentelemetry/src/otel_span_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,24 @@
-include("otel_sampler.hrl").
-include("otel_span.hrl").

%% sampling bit is the first bit in 8-bit trace options
-define(IS_ENABLED(X), (X band 1) =/= 0).

-spec start_span(opentelemetry:span_name(), opentelemetry:span_ctx() | undefined, otel_span:start_opts())
-spec start_span(otel_ctx:t(), opentelemetry:span_name(), otel_span:start_opts())
-> {opentelemetry:span_ctx(), opentelemetry:span() | undefined}.
start_span(Name, Parent, Opts) ->
start_span(Ctx, Name, Opts) ->
Attributes = maps:get(attributes, Opts, []),
Links = maps:get(links, Opts, []),
Kind = maps:get(kind, Opts, ?SPAN_KIND_INTERNAL),
Sampler = maps:get(sampler, Opts),
StartTime = maps:get(start_time, Opts, opentelemetry:timestamp()),
new_span(Name, Parent, Sampler, StartTime, Kind, Attributes, Links).

%% if parent is undefined create a new trace id
new_span(Name, undefined, Sampler, StartTime, Kind, Attributes, Links) ->
TraceId = opentelemetry:generate_trace_id(),
Span = #span_ctx{trace_id=TraceId,
trace_flags=0},
new_span(Name, Span, Sampler, StartTime, Kind, Attributes, Links);
new_span(Name, Parent=#span_ctx{trace_id=TraceId,
span_id=ParentSpanId}, Sampler, StartTime, Kind, Attributes, Links) ->
SpanId = opentelemetry:generate_span_id(),
SpanCtx = Parent#span_ctx{span_id=SpanId},
new_span(Ctx, Name, Sampler, StartTime, Kind, Attributes, Links).

new_span(Ctx, Name, Sampler, StartTime, Kind, Attributes, Links) ->
ParentTraceId = trace_id(Ctx),
{TraceFlags, IsRecording, SamplerAttributes, TraceState} =
sample(Sampler, TraceId, case Parent of
#span_ctx{span_id=undefined} ->
undefined;
_ ->
Parent
end,
Links, Name, Kind, Attributes),
sample(Ctx, Sampler, ParentTraceId, Links, Name, Kind, Attributes),

{NewSpanCtx, ParentSpanId} = new_span_ctx(Ctx),
TraceId = NewSpanCtx#span_ctx.trace_id,
SpanId = NewSpanCtx#span_ctx.span_id,

Span = #span{trace_id=TraceId,
span_id=SpanId,
Expand All @@ -67,10 +53,38 @@ new_span(Name, Parent=#span_ctx{trace_id=TraceId,
name=Name,
attributes=Attributes++SamplerAttributes,
links=Links,
trace_flags=TraceFlags,
is_recording=IsRecording},

{SpanCtx#span_ctx{trace_flags=TraceFlags,
is_recording=IsRecording}, Span}.
{NewSpanCtx#span_ctx{trace_flags=TraceFlags,
is_valid=true,
is_recording=IsRecording}, Span}.

trace_id(Ctx) ->
case otel_tracer:current_span_ctx(Ctx) of
#span_ctx{trace_id=TraceId} ->
TraceId;
_ ->
undefined
end.

-spec new_span_ctx(otel_ctx:t()) -> {opentelemetry:span_ctx(), opentelemetry:span_id()}.
new_span_ctx(Ctx) ->
case otel_tracer:current_span_ctx(Ctx) of
undefined ->
{root_span_ctx(), undefined};
#span_ctx{is_valid=false} ->
{root_span_ctx(), undefined};
ParentSpanCtx=#span_ctx{span_id=ParentSpanId} ->
%% keep the rest of the parent span ctx, simply need to update the span_id
{ParentSpanCtx#span_ctx{span_id=opentelemetry:generate_span_id()}, ParentSpanId}
end.

root_span_ctx() ->
#span_ctx{trace_id=opentelemetry:generate_trace_id(),
span_id=opentelemetry:generate_span_id(),
is_valid=true,
trace_flags=0}.

%%--------------------------------------------------------------------
%% @doc
Expand All @@ -79,22 +93,22 @@ new_span(Name, Parent=#span_ctx{trace_id=TraceId,
%%--------------------------------------------------------------------
-spec end_span(opentelemetry:span()) -> opentelemetry:span().
end_span(Span=#span{end_time=undefined,
trace_options=TraceOptions}) when ?IS_ENABLED(TraceOptions) ->
trace_flags=TraceFlags}) when ?IS_SAMPLED(TraceFlags) ->
EndTime = opentelemetry:timestamp(),
Span#span{end_time=EndTime};
end_span(Span) ->
Span.

%%

sample({Sampler, _Description, Opts}, TraceId, Parent, Links, SpanName, Kind, Attributes) ->
{Decision, NewAttributes, TraceState} = Sampler(TraceId, Parent, Links,
SpanName, Kind, Attributes, Opts),
sample(Ctx, {Sampler, _Description, Opts}, TraceId, Links, SpanName, Kind, Attributes) ->
{Decision, NewAttributes, TraceState} = Sampler(Ctx, TraceId, Links, SpanName,
Kind, Attributes, Opts),
case Decision of
?NOT_RECORD ->
?DROP ->
{0, false, NewAttributes, TraceState};
?RECORD ->
?RECORD_ONLY ->
{0, true, NewAttributes, TraceState};
?RECORD_AND_SAMPLED ->
?RECORD_AND_SAMPLE ->
{1, true, NewAttributes, TraceState}
end.
17 changes: 1 addition & 16 deletions apps/opentelemetry/src/otel_tracer_default.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,15 @@ start_span(Tracer={_, #tracer{on_end_processors=OnEndProcessors}}, Name, Opts) -
start_span(Ctx, Tracer={_, #tracer{on_start_processors=Processors,
on_end_processors=OnEndProcessors,
instrumentation_library=InstrumentationLibrary}}, Name, Opts) ->
ParentSpanCtx = maybe_parent_span_ctx(Ctx),
Opts1 = maybe_set_sampler(Tracer, Opts),
SpanCtx = otel_span_ets:start_span(Ctx, Name, ParentSpanCtx, Opts1, Processors, InstrumentationLibrary),
SpanCtx = otel_span_ets:start_span(Ctx, Name, Opts1, Processors, InstrumentationLibrary),
{SpanCtx#span_ctx{span_sdk={otel_span_ets, OnEndProcessors}}, Ctx}.

maybe_set_sampler(_Tracer, Opts) when is_map_key(sampler, Opts) ->
Opts;
maybe_set_sampler({_, #tracer{sampler=Sampler}}, Opts) ->
Opts#{sampler => Sampler}.

%% returns the span `start_opts' map with the parent span ctx set
%% based on the current tracer ctx, the ctx passed to `start_span'
%% or `start_inactive_span' and in the case none of those are defined it
%% checks the `EXTERNAL_SPAN_CTX' which can be set by a propagator extractor.
-spec maybe_parent_span_ctx(otel_ctx:t()) -> opentelemetry:span_ctx() | undefined.
maybe_parent_span_ctx(Ctx) ->
case otel_tracer:current_span_ctx(Ctx) of
ActiveSpanCtx=#span_ctx{} ->
ActiveSpanCtx;
_ ->
otel_tracer:current_external_span_ctx()
%% otel_ctx:get_value(?EXTERNAL_SPAN_CTX)
end.

-spec with_span(opentelemetry:tracer(), opentelemetry:span_name(), otel_tracer:traced_fun()) -> ok.
with_span(Tracer, SpanName, Fun) ->
with_span(Tracer, SpanName, #{}, Fun).
Expand Down
Loading