Skip to content

Commit 4bc737a

Browse files
committed
Document sampler modules
1 parent e7df425 commit 4bc737a

File tree

6 files changed

+65
-17
lines changed

6 files changed

+65
-17
lines changed

apps/opentelemetry/src/otel_sampler.erl

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@
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>. For examples of configuring samplers or implementing your own sampler,
23+
%% see <a href="https://opentelemetry.io/docs/languages/erlang/sampling/">the OpenTelemetry
24+
%% Erlang documentation</a>.
1825
%% @end
1926
%%%-------------------------------------------------------------------------
2027
-module(otel_sampler).
@@ -30,8 +37,11 @@
3037
t/0]).
3138

3239
-callback setup(sampler_opts()) -> sampler_config().
40+
%% Called when a sampler is created to set up the sampler.
41+
%% Should return the sampler configuration that is then passed to other callbacks.
3342

3443
-callback description(sampler_config()) -> description().
44+
%% Should return the description of the sampler.
3545

3646
-callback should_sample(
3747
otel_ctx:t(),
@@ -42,12 +52,19 @@
4252
opentelemetry:attributes_map(),
4353
sampler_config()
4454
) -> sampling_result().
55+
%% Main callback that determines whether a span should be sampled.\
4556

4657
-include("otel_sampler.hrl").
4758

4859
-type description() :: unicode:unicode_binary().
60+
%% The description of the sampler.
61+
4962
-type sampler_config() :: term().
63+
%% Any term used to configured a given sampler.
64+
5065
-type sampler_opts() :: term().
66+
%% Any options passed to a sampler.
67+
5168
-type builtin_sampler() ::
5269
always_on
5370
| always_off
@@ -59,15 +76,23 @@
5976
local_parent_not_sampled => sampler_spec(),
6077
root => sampler_spec()
6178
}}.
79+
%% A built-in sampler.
80+
6281
-type sampler_spec() :: builtin_sampler() | {module(), sampler_opts()}.
82+
6383
-type sampling_decision() :: ?DROP | ?RECORD_ONLY | ?RECORD_AND_SAMPLE.
84+
%% The decision that a sampler can make on a given span.
85+
6486
-type sampling_result() :: {
6587
sampling_decision(),
6688
opentelemetry:attributes_map(),
6789
opentelemetry:tracestate() | otel_tracestate:members()
6890
}.
91+
6992
-opaque t() :: {module(), description(), sampler_opts()}.
93+
%% A sampler.
7094

95+
%% @private
7196
-spec new(sampler_spec()) -> t().
7297
new(always_on) ->
7398
new({otel_sampler_always_on, #{}});
@@ -81,6 +106,7 @@ new({Sampler, Opts}) ->
81106
Config = Sampler:setup(Opts),
82107
{Sampler, Sampler:description(Config), Config}.
83108

109+
%% @private
84110
-spec should_sample(
85111
t(),
86112
otel_ctx:t(),
@@ -100,5 +126,6 @@ should_sample({Sampler, _, Config}, Ctx, TraceId, Links, SpanName, Kind, Attribu
100126
Result
101127
end.
102128

129+
%% @private
103130
-spec description(t()) -> description().
104131
description({_, Description, _}) -> Description.

apps/opentelemetry/src/otel_sampler_always_off.erl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
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.
1716
%% @end
1817
%%%-------------------------------------------------------------------------
1918
-module(otel_sampler_always_off).
@@ -25,10 +24,13 @@
2524
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
2625
-include("otel_sampler.hrl").
2726

27+
%% @private
2828
setup(_Opts) -> [].
2929

30+
%% @private
3031
description(_) -> <<"AlwaysOffSampler">>.
3132

33+
%% @private
3234
should_sample(Ctx, _TraceId, _Links, _SpanName, _SpanKind, _Attributes, _Opts) ->
3335
SpanCtx = otel_tracer:current_span_ctx(Ctx),
3436
{?DROP, [], otel_span:tracestate(SpanCtx)}.

apps/opentelemetry/src/otel_sampler_always_on.erl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
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.
1716
%% @end
1817
%%%-------------------------------------------------------------------------
1918
-module(otel_sampler_always_on).
@@ -25,10 +24,13 @@
2524
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
2625
-include("otel_sampler.hrl").
2726

27+
%% @private
2828
setup(_Opts) -> [].
2929

30+
%% @private
3031
description(_) -> <<"AlwaysOnSampler">>.
3132

33+
%% @private
3234
should_sample(Ctx, _TraceId, _Links, _SpanName, _SpanKind, _Attributes, _Opts) ->
3335
SpanCtx = otel_tracer:current_span_ctx(Ctx),
3436
{?RECORD_AND_SAMPLE, [], otel_span:tracestate(SpanCtx)}.

apps/opentelemetry/src/otel_sampler_parent_based.erl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
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()}.
2428
%% @end
2529
%%%-------------------------------------------------------------------------
2630
-module(otel_sampler_parent_based).
@@ -41,7 +45,9 @@
4145
local_parent_not_sampled => otel_sampler:sampler_spec(),
4246
root => otel_sampler:sampler_spec()
4347
}.
48+
%% Options to configure this sampler.
4449

50+
%% @private
4551
setup(Opts = #{root := RootSpec}) ->
4652
RemoteParentSampledSampler = sampler_for_spec(remote_parent_sampled, Opts, always_on),
4753
RemoteParentNotSampledSampler = sampler_for_spec(remote_parent_not_sampled, Opts, always_off),
@@ -63,6 +69,7 @@ sampler_for_spec(Key, Opts, DefaultModule) ->
6369
Spec = maps:get(Key, Opts, DefaultModule),
6470
otel_sampler:new(Spec).
6571

72+
%% @private
6673
description(#{
6774
root := RootSampler,
6875
remote_parent_sampled := RemoteParentSampler,
@@ -76,6 +83,7 @@ description(#{
7683
(otel_sampler:description(LocalParentSampler))/binary, ",localParentNotSampled:",
7784
(otel_sampler:description(LocalParentNotSampler))/binary, "}">>.
7885

86+
%% @private
7987
should_sample(Ctx, TraceId, Links, SpanName, SpanKind, Attributes, Config) ->
8088
ParentSpanCtx = otel_tracer:current_span_ctx(Ctx),
8189
SamplerKey = parent_based_sampler(ParentSpanCtx),

apps/opentelemetry/src/otel_sampler_trace_id_ratio_based.erl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
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.
1920
%% @end
2021
%%%-------------------------------------------------------------------------
2122
-module(otel_sampler_trace_id_ratio_based).
@@ -30,11 +31,15 @@
3031
-include("otel_sampler.hrl").
3132

3233
-type probability() :: float().
34+
%% A probability on whether to sample a span, between `0.0' and `1.0'.
35+
3336
-type config() :: #{probability := probability(), id_upper_bound := integer()}.
37+
%% The configuration for this sampler.
3438

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

42+
%% @private
3843
-spec setup(probability()) -> config().
3944
setup(Probability) ->
4045
IdUpperBound =
@@ -48,9 +53,11 @@ setup(Probability) ->
4853
end,
4954
#{probability => Probability, id_upper_bound => IdUpperBound}.
5055

56+
%% @private
5157
description(#{probability := Probability}) ->
5258
unicode:characters_to_binary(io_lib:format("TraceIdRatioBased{~.6f}", [Probability])).
5359

60+
%% @private
5461
should_sample(Ctx, TraceId, _Links, _SpanName, _SpanKind, _Attributes, #{
5562
id_upper_bound := IdUpperBound
5663
}) ->

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)