instrument_sampler behaviour (instrument v1.0.0)
View SourceOpenTelemetry Sampler behavior and registry.
Samplers determine whether a span should be sampled (recorded and exported) based on various criteria like trace ID, parent context, and span attributes.
Built-in Samplers
- instrument_sampler_always_on - Always sample - instrument_sampler_always_off - Never sample - instrument_sampler_probability - Probability-based sampling - instrument_sampler_parent_based - Defer to parent's sampling decision
Example Usage
%% Set global sampler
instrument_sampler:set_sampler(instrument_sampler_probability, #{ratio => 0.5}).
%% Make a sampling decision
Result = instrument_sampler:should_sample(TraceId, SpanName, SpanKind, Attributes, Links, ParentCtx).
Summary
Functions
Gets the description of the current sampler.
Gets the current global sampler module.
Sets the global sampler module.
Sets the global sampler module with configuration.
Makes a sampling decision for a new span.
Types
-type sampler_config() :: map().
-type sampling_decision() :: drop | record_only | record_and_sample.
Callbacks
-callback get_description(Config :: sampler_config()) -> binary().
-callback should_sample(Config :: sampler_config(), TraceId :: binary(), SpanName :: binary(), SpanKind :: atom(), Attributes :: map(), Links :: list(), ParentCtx :: #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()} | undefined) -> #sampling_result{decision :: drop | record_only | record_and_sample, attributes :: map(), trace_state :: [{binary(), binary()}]}.
Functions
-spec get_description() -> binary().
Gets the description of the current sampler.
-spec get_sampler() -> module().
Gets the current global sampler module.
-spec set_sampler(module()) -> ok.
Sets the global sampler module.
-spec set_sampler(module(), sampler_config()) -> ok.
Sets the global sampler module with configuration.
-spec should_sample(TraceId :: binary(), SpanName :: binary(), SpanKind :: atom(), Attributes :: map(), Links :: list(), ParentCtx :: #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()} | undefined) -> sampling_result().
Makes a sampling decision for a new span.
Returns a sampling_result record containing: - decision: drop | record_only | record_and_sample - attributes: additional attributes to add to the span - trace_state: updated trace state