12
12
% % See the License for the specific language governing permissions and
13
13
% % limitations under the License.
14
14
% %
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>.
18
32
% % @end
19
33
% %%-------------------------------------------------------------------------
20
34
-module (otel_sampler ).
30
44
t / 0 ]).
31
45
32
46
-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.
33
49
34
50
-callback description (sampler_config ()) -> description ().
51
+ % % Should return the description of the sampler.
35
52
36
53
-callback should_sample (
37
54
otel_ctx :t (),
42
59
opentelemetry :attributes_map (),
43
60
sampler_config ()
44
61
) -> sampling_result ().
62
+ % % Main callback that determines whether a span should be sampled.\
45
63
46
64
-include (" otel_sampler.hrl" ).
47
65
48
66
-type description () :: unicode :unicode_binary ().
67
+ % % The description of the sampler.
68
+
49
69
-type sampler_config () :: term ().
70
+ % % Any term used to configured a given sampler.
71
+
50
72
-type sampler_opts () :: term ().
73
+ % % Any options passed to a sampler.
74
+
51
75
-type builtin_sampler () ::
52
76
always_on
53
77
| always_off
59
83
local_parent_not_sampled => sampler_spec (),
60
84
root => sampler_spec ()
61
85
}}.
86
+ % % A built-in sampler.
87
+
62
88
-type sampler_spec () :: builtin_sampler () | {module (), sampler_opts ()}.
89
+ % % Specification to create a sampler.
90
+
63
91
-type sampling_decision () :: ? DROP | ? RECORD_ONLY | ? RECORD_AND_SAMPLE .
92
+ % % The decision that a sampler can make on a given span.
93
+
64
94
-type sampling_result () :: {
65
95
sampling_decision (),
66
96
opentelemetry :attributes_map (),
67
97
opentelemetry :tracestate () | otel_tracestate :members ()
68
98
}.
99
+ % % The result of a sampling decision.
100
+
69
101
-opaque t () :: {module (), description (), sampler_opts ()}.
102
+ % % A sampler.
70
103
71
- -spec new (sampler_spec ()) -> t ().
104
+ % % @doc Returns a sampler based on the given specification.
105
+ -spec new (SamplerSpec :: sampler_spec ()) -> t ().
72
106
new (always_on ) ->
73
107
new ({otel_sampler_always_on , #{}});
74
108
new (always_off ) ->
@@ -81,6 +115,7 @@ new({Sampler, Opts}) ->
81
115
Config = Sampler :setup (Opts ),
82
116
{Sampler , Sampler :description (Config ), Config }.
83
117
118
+ % % @private
84
119
-spec should_sample (
85
120
t (),
86
121
otel_ctx :t (),
@@ -100,5 +135,6 @@ should_sample({Sampler, _, Config}, Ctx, TraceId, Links, SpanName, Kind, Attribu
100
135
Result
101
136
end .
102
137
138
+ % % @doc Returns the description of the given sampler.
103
139
-spec description (t ()) -> description ().
104
- description ({_ , Description , _ }) -> Description .
140
+ description (_Sampler = {_ , Description , _ }) -> Description .
0 commit comments