Skip to content

Commit

Permalink
Merge pull request #260 from qdentity/dvic-sampler-new
Browse files Browse the repository at this point in the history
Introduce otel_sampler:new/3 and fix behavior typespec
  • Loading branch information
tsloughter authored Aug 3, 2021
2 parents 3eff2f2 + 99633d7 commit edfd222
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
32 changes: 21 additions & 11 deletions apps/opentelemetry/src/otel_sampler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
%%%-------------------------------------------------------------------------
-module(otel_sampler).

-export([setup/1,
-export([new/3,
setup/1,
get_description/1,
always_on/7,
always_off/7,
Expand All @@ -31,26 +32,35 @@
-include("otel_sampler.hrl").
-include("otel_span.hrl").

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

-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((otel_ctx:t(),
opentelemetry:trace_id(),
opentelemetry:links(),
opentelemetry:span_name(),
opentelemetry:span_kind(),
opentelemetry:attributes(),
term()) -> sampling_result()), description(), term()}.
-type sampler_fun() :: fun((otel_ctx:t(),
opentelemetry:trace_id(),
opentelemetry:links(),
opentelemetry:span_name(),
opentelemetry:span_kind(),
opentelemetry:attributes(),
term()) -> sampling_result()).
-type sampler() :: {sampler_fun(), description(), sampler_opts()}.
-type sampler_opts() :: term().
-opaque t() :: sampler().
-export_type([sampling_result/0,
-export_type([sampler_fun/0,
description/0,
sampling_result/0,
sampling_decision/0,
sampler_opts/0,
t/0]).

-define(MAX_VALUE, 9223372036854775807). %% 2^63 - 1

-spec setup(atom() | {atom() | module(), term()}) -> t().
-spec new(sampler_fun(), description(), sampler_opts()) -> t().
new(DecisionFunction, Description, SamplerOpts) ->
{DecisionFunction, Description, SamplerOpts}.

-spec setup(atom() | {atom() | module(), sampler_opts()}) -> t().
setup({Sampler, Opts}) ->
setup(Sampler, Opts);
setup(Sampler) when is_atom(Sampler) ->
Expand Down
8 changes: 5 additions & 3 deletions apps/opentelemetry/test/static_sampler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

%% sampler returns the value from the Opts map based on the SpanName or `NOT_RECORD'
setup(Opts) ->
{fun(_, _, _, SpanName, _, _, Opts1) ->
{maps:get(SpanName, Opts1, ?DROP), [], []}
end, <<"StaticSampler">>, Opts}.
otel_sampler:new(
fun(_, _, _, SpanName, _, _, Opts1) -> {maps:get(SpanName, Opts1, ?DROP), [], []} end,
<<"StaticSampler">>,
Opts
).

0 comments on commit edfd222

Please sign in to comment.