instrument_sampler_attribute (instrument v1.0.0)
View SourceAttribute-aware sampler for fine-grained sampling control.
This sampler allows configuring sampling rates based on span attributes, enabling sophisticated sampling strategies like: - Lower sampling for high-volume read operations - Higher sampling for writes and mutations - Force sampling for critical tables/topics
IMPORTANT: Sampling Timing
Sampling decisions are made at span start, BEFORE your code executes. Only attributes passed in the span options at creation time can influence the sampling decision. Attributes set later via set_attribute/2 have NO effect on sampling.
For error sampling based on execution results, use tail-based sampling or custom span processors instead of attribute rules.
Configuration
instrument_sampler:set_sampler(instrument_sampler_attribute, #{
default_ratio => 0.1,
attribute_rules => [
%% {AttributeName, Value, SamplingRatio}
%% These attributes MUST be passed at span creation time
{<<"db.operation">>, <<"SELECT">>, 0.01},
{<<"db.operation">>, <<"DELETE">>, 0.5},
{<<"db.sql.table">>, <<"audit_log">>, 1.0}
]
}).Rule Matching
- Rules are evaluated in order - First matching rule determines the sampling rate - If no rules match, default_ratio is used - Attribute values can be binaries, atoms, integers, or booleans
Example Use Cases
Database tracing:
#{
default_ratio => 0.001, %% 0.1% baseline
attribute_rules => [
{<<"db.operation">>, <<"SELECT">>, 0.001},
{<<"db.operation">>, <<"INSERT">>, 0.01},
{<<"db.operation">>, <<"UPDATE">>, 0.01},
{<<"db.operation">>, <<"DELETE">>, 0.05},
{<<"db.sql.table">>, <<"payments">>, 1.0}
]
}HTTP client tracing:
#{
default_ratio => 0.1,
attribute_rules => [
{<<"http.method">>, <<"GET">>, 0.01},
{<<"http.method">>, <<"POST">>, 0.1}
]
}
Summary
Functions
Returns the sampler description.
Samples based on span attributes with configurable rules.
Types
Functions
Returns the sampler description.
-spec should_sample(Config :: 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()}]}.
Samples based on span attributes with configurable rules.