Logfire Sampling Configuration & Options
Types for configuring sampling. See the sampling guide.
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.
The raw numeric value of the level. Higher values are more severe.
Type: int
The human-readable name of the level, or None if the number is invalid.
@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’.
SpanLevel
Argument passed to the SamplingOptions.tail callback.
Raw span object being started or ended.
Type: ReadableSpan
Second argument of SpanProcessor.on_start or None for SpanProcessor.on_end.
Type: context.Context | None
'start' if the span is being started, 'end' if it’s being ended.
Type: Literal[‘start’, ‘end’]
The log level of the span.
Type: SpanLevel
The time in seconds between the start of the trace and the start/end of this span.
Type: float
Options for logfire.configure(sampling=...).
See the sampling guide.
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
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 every started span has ended and the trace is discarded. Large traces and traces with long-running spans may therefore consume a lot of memory.
Type: Callable[[TailSamplingSpanInfo], float] | None Default: None
@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.