From 8ffe3ad6dd299c234f1ef07467bcbbfdcc465b42 Mon Sep 17 00:00:00 2001 From: dvic Date: Tue, 3 Aug 2021 23:13:49 +0200 Subject: [PATCH 1/3] Introduce otel_sampler:new/3 and fix behavior typespec --- apps/opentelemetry/src/otel_sampler.erl | 32 ++++++++++++++-------- apps/opentelemetry/test/static_sampler.erl | 7 +++-- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/apps/opentelemetry/src/otel_sampler.erl b/apps/opentelemetry/src/otel_sampler.erl index bbae7d5e..9f7d5afb 100644 --- a/apps/opentelemetry/src/otel_sampler.erl +++ b/apps/opentelemetry/src/otel_sampler.erl @@ -19,7 +19,8 @@ %%%------------------------------------------------------------------------- -module(otel_sampler). --export([setup/1, +-export([new/3, + setup/1, get_description/1, always_on/7, always_off/7, @@ -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 decider() :: fun((otel_ctx:t(), + opentelemetry:trace_id(), + opentelemetry:links(), + opentelemetry:span_name(), + opentelemetry:span_kind(), + opentelemetry:attributes(), + term()) -> sampling_result()). +-type sampler() :: {decider(), description(), sampler_opts()}. +-type sampler_opts() :: term(). -opaque t() :: sampler(). --export_type([sampling_result/0, +-export_type([decider/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(decider(), 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) -> diff --git a/apps/opentelemetry/test/static_sampler.erl b/apps/opentelemetry/test/static_sampler.erl index 6d3cca55..6db0a2d3 100644 --- a/apps/opentelemetry/test/static_sampler.erl +++ b/apps/opentelemetry/test/static_sampler.erl @@ -6,6 +6,7 @@ %% 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, <<"StaticSamplerr">>, + Opts + ). From 055afa2cb341d63fa1e89c8054195e59f639c2ba Mon Sep 17 00:00:00 2001 From: dvic Date: Tue, 3 Aug 2021 23:31:02 +0200 Subject: [PATCH 2/3] Rename decider() to sampler_fun() --- apps/opentelemetry/src/otel_sampler.erl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/apps/opentelemetry/src/otel_sampler.erl b/apps/opentelemetry/src/otel_sampler.erl index 9f7d5afb..3e4e649b 100644 --- a/apps/opentelemetry/src/otel_sampler.erl +++ b/apps/opentelemetry/src/otel_sampler.erl @@ -37,17 +37,17 @@ -type sampling_decision() :: ?DROP | ?RECORD_ONLY | ?RECORD_AND_SAMPLE. -type sampling_result() :: {sampling_decision(), opentelemetry:attributes(), opentelemetry:tracestate()}. -type description() :: unicode:unicode_binary(). --type decider() :: fun((otel_ctx:t(), - opentelemetry:trace_id(), - opentelemetry:links(), - opentelemetry:span_name(), - opentelemetry:span_kind(), - opentelemetry:attributes(), - term()) -> sampling_result()). --type sampler() :: {decider(), description(), sampler_opts()}. +-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([decider/0, +-export_type([sampler_fun/0, description/0, sampling_result/0, sampling_decision/0, @@ -56,7 +56,7 @@ -define(MAX_VALUE, 9223372036854775807). %% 2^63 - 1 --spec new(decider(), description(), sampler_opts()) -> t(). +-spec new(sampler_fun(), description(), sampler_opts()) -> t(). new(DecisionFunction, Description, SamplerOpts) -> {DecisionFunction, Description, SamplerOpts}. From 99633d7719c8a94932615a518536c67fd6f870cc Mon Sep 17 00:00:00 2001 From: dvic Date: Tue, 3 Aug 2021 23:37:13 +0200 Subject: [PATCH 3/3] Fix formatting --- apps/opentelemetry/test/static_sampler.erl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/opentelemetry/test/static_sampler.erl b/apps/opentelemetry/test/static_sampler.erl index 6db0a2d3..19bb292d 100644 --- a/apps/opentelemetry/test/static_sampler.erl +++ b/apps/opentelemetry/test/static_sampler.erl @@ -7,6 +7,7 @@ %% sampler returns the value from the Opts map based on the SpanName or `NOT_RECORD' setup(Opts) -> otel_sampler:new( - fun(_, _, _, SpanName, _, _, Opts1) -> {maps:get(SpanName, Opts1, ?DROP), [], []} end, <<"StaticSamplerr">>, + fun(_, _, _, SpanName, _, _, Opts1) -> {maps:get(SpanName, Opts1, ?DROP), [], []} end, + <<"StaticSampler">>, Opts ).