Otel.SDK.Trace.Sampler behaviour (otel v0.2.0)

Copy Markdown View Source

Sampler behaviour and dispatch (trace/sdk.md §Sampler L329-L460).

A sampler decides whether a span should be recorded and/or sampled (propagated). Custom samplers implement this behaviour.

All sampler methods are safe for concurrent use, satisfying spec trace/sdk.md L1284 — "Sampler — ShouldSample and GetDescription MUST be safe to be called concurrently."

Public API

Function / CallbackRole
new/1SDK (lifecycle) — initialises a sampler from {module, opts}
should_sample/7SDK (OTel API MUST) — trace/sdk.md §ShouldSample L342-L406
description/1SDK (OTel API MUST) — trace/sdk.md §GetDescription L408-L417
@callback setup/1SDK (lifecycle)
@callback should_sample/7SDK (OTel API MUST)
@callback description/1SDK (OTel API MUST)

Built-in samplers

Deferred Development-status features

  • AlwaysRecord sampler decorator. Spec trace/sdk.md L608-L630 (Status: Development, added v1.51 #4699) defines a sampler decorator that wraps any sampler and forces record_only (recording without propagation) when the inner sampler returns drop. Not implemented — no in-tree consumer. When stabilised it will live as Otel.SDK.Trace.Sampler.AlwaysRecord.

References

  • OTel Trace SDK §Sampler: opentelemetry-specification/specification/trace/sdk.md L329-L460

Summary

Callbacks

Returns a human-readable description of the sampler.

Initializes sampler configuration from options.

Returns a sampling decision for a span to be created.

Functions

Returns the sampler's description.

Creates a sampler from a spec.

Invokes the sampler's should_sample callback.

Types

attributes()

@type attributes() :: %{required(String.t()) => primitive_any()}

config()

@type config() :: term()

description()

@type description() :: String.t()

opts()

@type opts() :: term()

primitive()

@type primitive() ::
  String.t() | {:bytes, binary()} | boolean() | integer() | float() | nil

primitive_any()

@type primitive_any() ::
  primitive() | [primitive_any()] | %{required(String.t()) => primitive_any()}

sampling_decision()

@type sampling_decision() :: :drop | :record_only | :record_and_sample

sampling_result()

@type sampling_result() ::
  {sampling_decision(), attributes(), Otel.API.Trace.TraceState.t()}

t()

@type t() :: {module(), description(), config()}

Callbacks

description(config)

@callback description(config :: config()) :: description()

Returns a human-readable description of the sampler.

setup opts

@callback setup(opts :: opts()) :: config()

Initializes sampler configuration from options.

should_sample(ctx, trace_id, links, name, kind, attributes, config)

@callback should_sample(
  ctx :: Otel.API.Ctx.t(),
  trace_id :: Otel.API.Trace.TraceId.t(),
  links :: [Otel.API.Trace.Link.t()],
  name :: String.t(),
  kind :: Otel.API.Trace.SpanKind.t(),
  attributes :: attributes(),
  config :: config()
) :: sampling_result()

Returns a sampling decision for a span to be created.

Functions

description(arg)

@spec description(sampler :: t()) :: description()

Returns the sampler's description.

new(arg)

@spec new(spec :: {module(), opts()}) :: t()

Creates a sampler from a spec.

Accepts {module, opts} and returns {module, description, config}.

should_sample(arg, ctx, trace_id, links, name, kind, attributes)

@spec should_sample(
  sampler :: t(),
  ctx :: Otel.API.Ctx.t(),
  trace_id :: Otel.API.Trace.TraceId.t(),
  links :: [Otel.API.Trace.Link.t()],
  name :: String.t(),
  kind :: Otel.API.Trace.SpanKind.t(),
  attributes :: attributes()
) :: sampling_result()

Invokes the sampler's should_sample callback.