Skip to content

Logfire Sampling Configuration & Options

Types for configuring sampling. See the sampling guide.

SpanLevel

A convenience class for comparing span/log levels.

Can be compared to log level names (strings) such as ‘info’ or ‘error’ using <, >, <=, or >=, so e.g. level >= 'error' is valid.

Will raise an exception if compared to a non-string or an invalid level name.

Attributes

number

The raw numeric value of the level. Higher values are more severe.

Type: int

name

The human-readable name of the level, or None if the number is invalid.

Type: LevelName | None

Methods

from_span

@classmethod

def from_span(cls, span: ReadableSpan) -> SpanLevel

Create a SpanLevel from an OpenTelemetry span.

If the span has no level set, defaults to ‘info’.

Returns

SpanLevel

TailSamplingSpanInfo

Argument passed to the SamplingOptions.tail callback.

Attributes

span

Raw span object being started or ended.

Type: ReadableSpan

context

Second argument of SpanProcessor.on_start or None for SpanProcessor.on_end.

Type: context.Context | None

event

'start' if the span is being started, 'end' if it’s being ended.

Type: Literal[‘start’, ‘end’]

level

The log level of the span.

Type: SpanLevel

duration

The time in seconds between the start of the trace and the start/end of this span.

Type: float

SamplingOptions

Options for logfire.configure(sampling=...).

See the sampling guide.

Attributes

head

Head sampling options.

If it’s a float, it should be a number between 0.0 and 1.0. This is the probability that an entire trace will randomly included.

Alternatively you can pass a custom OpenTelemetry Sampler.

Type: float | Sampler Default: 1.0

tail

An optional tail sampling callback which will be called for every span.

It should return a number between 0.0 and 1.0, the probability that the entire trace will be included. Use SamplingOptions.level_or_duration for a common use case.

Every span in a trace will be stored in memory until either the trace is included by tail sampling or it’s completed and discarded, so large traces may consume a lot of memory.

Type: Callable[[TailSamplingSpanInfo], float] | None Default: None

Methods

level_or_duration

@classmethod

def level_or_duration(
    cls,
    head: float | Sampler = 1.0,
    level_threshold: LevelName | None = 'notice',
    duration_threshold: float | None = 5.0,
    background_rate: float = 0.0,
) -> Self

Returns a SamplingOptions instance that tail samples traces based on their log level and duration.

If a trace has at least one span/log that has a log level greater than or equal to level_threshold, or if the duration of the whole trace is greater than duration_threshold seconds, then the whole trace will be included. Otherwise, the probability is background_rate.

The head parameter is the same as in the SamplingOptions constructor.

Returns

Self