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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
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.
number
instance-attribute
¶
number: int
The raw numeric value of the level. Higher values are more severe.
name
property
¶
name: LevelName | None
The human-readable name of the level, or None
if the number is invalid.
from_span
classmethod
¶
from_span(span: ReadableSpan) -> SpanLevel
Create a SpanLevel from an OpenTelemetry span.
If the span has no level set, defaults to 'info'.
Source code in logfire/types.py
36 37 38 39 40 41 42 43 44 45 46 |
|
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
.