Skip to content

Commit a5f4b7b

Browse files
authored
Merge pull request #729 from whatyouhide/al/document-samplers
Document sampler modules
2 parents 8512748 + 565a24f commit a5f4b7b

File tree

6 files changed

+92
-19
lines changed

6 files changed

+92
-19
lines changed

apps/opentelemetry/src/otel_sampler.erl

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,23 @@
1212
%% See the License for the specific language governing permissions and
1313
%% limitations under the License.
1414
%%
15-
%% @doc
16-
%% A sampler is a function run on each started span that returns whether to
17-
%% record and propagate, only record or not record the span.
15+
%% @doc Behaviour for defining samplers.
16+
%%
17+
%% A sampler should provide a function run on each started span that returns whether to
18+
%% record and propagate, only record, or not record the span.
19+
%%
20+
%% For more information on the concept of <i>Sampling</i>, see
21+
%% <a href="https://opentelemetry.io/docs/concepts/sampling/">Sampling in the OpenTelemetry
22+
%% documentation</a> or the
23+
%% <a href="https://opentelemetry.io/docs/specs/otel/trace/sdk/#sampling">Sampling spec</a>.
24+
%% For examples of configuring samplers or implementing your own sampler,
25+
%% see <a href="https://opentelemetry.io/docs/languages/erlang/sampling/">the OpenTelemetry
26+
%% Erlang documentation</a>.
27+
%%
28+
%% <h3>Configuration</h3>
29+
%%
30+
%% To configure sampling for the `opentelemetry' application, see
31+
%% <a href="https://hexdocs.pm/opentelemetry/readme.html#samplers">the documentation</a>.
1832
%% @end
1933
%%%-------------------------------------------------------------------------
2034
-module(otel_sampler).
@@ -30,8 +44,11 @@
3044
t/0]).
3145

3246
-callback setup(sampler_opts()) -> sampler_config().
47+
%% Called when a sampler is created to set up the sampler.
48+
%% Should return the sampler configuration that is then passed to other callbacks.
3349

3450
-callback description(sampler_config()) -> description().
51+
%% Should return the description of the sampler.
3552

3653
-callback should_sample(
3754
otel_ctx:t(),
@@ -42,12 +59,19 @@
4259
opentelemetry:attributes_map(),
4360
sampler_config()
4461
) -> sampling_result().
62+
%% Main callback that determines whether a span should be sampled.\
4563

4664
-include("otel_sampler.hrl").
4765

4866
-type description() :: unicode:unicode_binary().
67+
%% The description of the sampler.
68+
4969
-type sampler_config() :: term().
70+
%% Any term used to configured a given sampler.
71+
5072
-type sampler_opts() :: term().
73+
%% Any options passed to a sampler.
74+
5175
-type builtin_sampler() ::
5276
always_on
5377
| always_off
@@ -59,16 +83,26 @@
5983
local_parent_not_sampled => sampler_spec(),
6084
root => sampler_spec()
6185
}}.
86+
%% A built-in sampler.
87+
6288
-type sampler_spec() :: builtin_sampler() | {module(), sampler_opts()}.
89+
%% Specification to create a sampler.
90+
6391
-type sampling_decision() :: ?DROP | ?RECORD_ONLY | ?RECORD_AND_SAMPLE.
92+
%% The decision that a sampler can make on a given span.
93+
6494
-type sampling_result() :: {
6595
sampling_decision(),
6696
opentelemetry:attributes_map(),
6797
opentelemetry:tracestate() | otel_tracestate:members()
6898
}.
99+
%% The result of a sampling decision.
100+
69101
-opaque t() :: {module(), description(), sampler_opts()}.
102+
%% A sampler.
70103

71-
-spec new(sampler_spec()) -> t().
104+
%% @doc Returns a sampler based on the given specification.
105+
-spec new(SamplerSpec :: sampler_spec()) -> t().
72106
new(always_on) ->
73107
new({otel_sampler_always_on, #{}});
74108
new(always_off) ->
@@ -81,6 +115,7 @@ new({Sampler, Opts}) ->
81115
Config = Sampler:setup(Opts),
82116
{Sampler, Sampler:description(Config), Config}.
83117

118+
%% @private
84119
-spec should_sample(
85120
t(),
86121
otel_ctx:t(),
@@ -100,5 +135,6 @@ should_sample({Sampler, _, Config}, Ctx, TraceId, Links, SpanName, Kind, Attribu
100135
Result
101136
end.
102137

138+
%% @doc Returns the description of the given sampler.
103139
-spec description(t()) -> description().
104-
description({_, Description, _}) -> Description.
140+
description(_Sampler = {_, Description, _}) -> Description.

apps/opentelemetry/src/otel_sampler_always_off.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
%% See the License for the specific language governing permissions and
1313
%% limitations under the License.
1414
%%
15-
%% @doc
16-
%% This sampler always decides to neither record nor sample each span.
15+
%% @doc An {@link otel_sampler} that drops all spans.
16+
%%
17+
%% This is one of the
18+
%% <a href="https://opentelemetry.io/docs/specs/otel/trace/sdk/#built-in-samplers">built-in
19+
%% samplers</a> provided by the OpenTelemetry SDK.
1720
%% @end
1821
%%%-------------------------------------------------------------------------
1922
-module(otel_sampler_always_off).
@@ -25,10 +28,13 @@
2528
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
2629
-include("otel_sampler.hrl").
2730

31+
%% @private
2832
setup(_Opts) -> [].
2933

34+
%% @private
3035
description(_) -> <<"AlwaysOffSampler">>.
3136

37+
%% @private
3238
should_sample(Ctx, _TraceId, _Links, _SpanName, _SpanKind, _Attributes, _Opts) ->
3339
SpanCtx = otel_tracer:current_span_ctx(Ctx),
3440
{?DROP, [], otel_span:tracestate(SpanCtx)}.

apps/opentelemetry/src/otel_sampler_always_on.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
%% See the License for the specific language governing permissions and
1313
%% limitations under the License.
1414
%%
15-
%% @doc
16-
%% This sampler always decides to record and sample each span.
15+
%% @doc An {@link otel_sampler} that records and samples all spans.
16+
%%
17+
%% This is one of the
18+
%% <a href="https://opentelemetry.io/docs/specs/otel/trace/sdk/#built-in-samplers">built-in
19+
%% samplers</a> provided by the OpenTelemetry SDK.
1720
%% @end
1821
%%%-------------------------------------------------------------------------
1922
-module(otel_sampler_always_on).
@@ -25,10 +28,13 @@
2528
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
2629
-include("otel_sampler.hrl").
2730

31+
%% @private
2832
setup(_Opts) -> [].
2933

34+
%% @private
3035
description(_) -> <<"AlwaysOnSampler">>.
3136

37+
%% @private
3238
should_sample(Ctx, _TraceId, _Links, _SpanName, _SpanKind, _Attributes, _Opts) ->
3339
SpanCtx = otel_tracer:current_span_ctx(Ctx),
3440
{?RECORD_AND_SAMPLE, [], otel_span:tracestate(SpanCtx)}.

apps/opentelemetry/src/otel_sampler_parent_based.erl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@
1212
%% See the License for the specific language governing permissions and
1313
%% limitations under the License.
1414
%%
15-
%% @doc
16-
%% This sampler makes the decision based on the parent, with the following possibilities:
17-
%% 1) a remote parent that is sampled (by default always_on);
18-
%% 2) a remote parent that is not sampled (by default always_off);
19-
%% 3) a local parent that is sampled (by default always_on);
20-
%% 4) a local parent that is not sampled (by default always_off);
21-
%% 5) no parent (by default always_on).
15+
%% @doc An {@link otel_sampler} that makes the decision based on the parent.
16+
%%
17+
%% This sampler decides with the following possibilities:
18+
%% <ol>
19+
%% <li>a remote parent that is sampled (by default `always_on');</li>
20+
%% <li>a remote parent that is not sampled (by default `always_off');</li>
21+
%% <li>a local parent that is sampled (by default `always_on');</li>
22+
%% <li>a local parent that is not sampled (by default `always_off');</li>
23+
%% <li>no parent (by default `always_on').</li>
24+
%% </ol>
2225
%%
2326
%% For each of these cases a different sampler can be configured.
27+
%% For options, see {@link opts()}.
28+
%%
29+
%% This is one of the
30+
%% <a href="https://opentelemetry.io/docs/specs/otel/trace/sdk/#built-in-samplers">built-in
31+
%% samplers</a> provided by the OpenTelemetry SDK.
2432
%% @end
2533
%%%-------------------------------------------------------------------------
2634
-module(otel_sampler_parent_based).
@@ -41,7 +49,9 @@
4149
local_parent_not_sampled => otel_sampler:sampler_spec(),
4250
root => otel_sampler:sampler_spec()
4351
}.
52+
%% Options to configure this sampler.
4453

54+
%% @private
4555
setup(Opts = #{root := RootSpec}) ->
4656
RemoteParentSampledSampler = sampler_for_spec(remote_parent_sampled, Opts, always_on),
4757
RemoteParentNotSampledSampler = sampler_for_spec(remote_parent_not_sampled, Opts, always_off),
@@ -63,6 +73,7 @@ sampler_for_spec(Key, Opts, DefaultModule) ->
6373
Spec = maps:get(Key, Opts, DefaultModule),
6474
otel_sampler:new(Spec).
6575

76+
%% @private
6677
description(#{
6778
root := RootSampler,
6879
remote_parent_sampled := RemoteParentSampler,
@@ -76,6 +87,7 @@ description(#{
7687
(otel_sampler:description(LocalParentSampler))/binary, ",localParentNotSampled:",
7788
(otel_sampler:description(LocalParentNotSampler))/binary, "}">>.
7889

90+
%% @private
7991
should_sample(Ctx, TraceId, Links, SpanName, SpanKind, Attributes, Config) ->
8092
ParentSpanCtx = otel_tracer:current_span_ctx(Ctx),
8193
SamplerKey = parent_based_sampler(ParentSpanCtx),

apps/opentelemetry/src/otel_sampler_trace_id_ratio_based.erl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
%% See the License for the specific language governing permissions and
1313
%% limitations under the License.
1414
%%
15-
%% @doc
15+
%% @doc An {@link otel_sampler} that samples a configured percentage of spans.
16+
%%
1617
%% This sampler samples a configured percentage of spans, where the sampling
17-
%% decision is deterministic with respect to the span trace id, i.e., it always
18-
%% makes the same decision for the same trace id.
18+
%% decision is <i>deterministic</i> with respect to the span trace ID. That means
19+
%% the sampler always makes the same decision for the same trace ID.
20+
%%
21+
%% This is one of the
22+
%% <a href="https://opentelemetry.io/docs/specs/otel/trace/sdk/#built-in-samplers">built-in
23+
%% samplers</a> provided by the OpenTelemetry SDK.
1924
%% @end
2025
%%%-------------------------------------------------------------------------
2126
-module(otel_sampler_trace_id_ratio_based).
@@ -30,11 +35,15 @@
3035
-include("otel_sampler.hrl").
3136

3237
-type probability() :: float().
38+
%% A probability on whether to sample a span, between `0.0' and `1.0'.
39+
3340
-type config() :: #{probability := probability(), id_upper_bound := integer()}.
41+
%% The configuration for this sampler.
3442

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

46+
%% @private
3847
-spec setup(probability()) -> config().
3948
setup(Probability) ->
4049
IdUpperBound =
@@ -48,9 +57,11 @@ setup(Probability) ->
4857
end,
4958
#{probability => Probability, id_upper_bound => IdUpperBound}.
5059

60+
%% @private
5161
description(#{probability := Probability}) ->
5262
unicode:characters_to_binary(io_lib:format("TraceIdRatioBased{~.6f}", [Probability])).
5363

64+
%% @private
5465
should_sample(Ctx, TraceId, _Links, _SpanName, _SpanKind, _Attributes, #{
5566
id_upper_bound := IdUpperBound
5667
}) ->

apps/opentelemetry_api_experimental/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ _build
1717
*.iml
1818
rebar3.crashdump
1919
*~
20+
21+
/deps

0 commit comments

Comments
 (0)