Sampling
Types for configuring sampling. See the sampling guide.
SamplingOptions
dataclass
¶
SamplingOptions(
head: float | Sampler = 1.0,
tail: (
Callable[[TailSamplingSpanInfo], float] | None
) = None,
)
Options for logfire.configure(sampling=...)
.
See the sampling guide.
head
class-attribute
instance-attribute
¶
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
.
tail
class-attribute
instance-attribute
¶
tail: Callable[[TailSamplingSpanInfo], float] | None = None
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.
level_or_duration
classmethod
¶
level_or_duration(
*,
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.
Source code in logfire/sampling/_tail_sampling.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
SpanLevel
dataclass
¶
SpanLevel(number: int)
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.
TailSamplingSpanInfo
dataclass
¶
TailSamplingSpanInfo(
span: ReadableSpan,
context: Context | None,
event: Literal["start", "end"],
buffer: TraceBuffer,
)
Argument passed to the SamplingOptions.tail
callback.
context
instance-attribute
¶
context: Context | None
Second argument of SpanProcessor.on_start
or None
for SpanProcessor.on_end
.