Skip to content

Breaking change: Reject a trace sample rate outside 0.0 to 1.0 instead of keeping every trace - #2818

Merged
alexmojaki merged 2 commits into
pydantic:mainfrom
Yasser-Ameur:fix/trace-sample-rate-out-of-range
Nov 18, 2026
Merged

alexmojaki merged 2 commits into
pydantic:mainfrom
Yasser-Ameur:fix/trace-sample-rate-out-of-range

Conversation

@Yasser-Ameur

@Yasser-Ameur Yasser-Ameur commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

A head sample rate outside 0 to 1 is not reported. LOGFIRE_TRACE_SAMPLE_RATE=1.5, nan or inf keeps every trace, because the sampler is only installed when head < 1 and that comparison is false for all three. A negative rate reaches OpenTelemetry and fails with a bare "Probability must be in range" that does not name the setting. #2685 gave the not a number case a LogfireConfigError and left ranges for later; this is that follow up.

Change: the rate is checked once in the Config constructor, so the env var, the pyproject.toml setting and SamplingOptions(head=...) all raise a LogfireConfigError that names trace_sample_rate. The same kind of check already exists at _tail_sampling.py:132 and main.py:883.

Behaviour to be aware of: values above 1 used to behave like 1.0 and now raise. For the env var and the pyproject setting the error surfaces at import logfire, since the default config is built there; SamplingOptions fails at configure(). If you would rather not break a deployment that has 2 set, I can change values above 1 to a warning that keeps every trace and raise only for negative, nan and inf.

Tests: 12 new cases in tests/test_configure.py (out of range values through each of the three inputs, and 0, 0.5 and 1 still accepted). 9 of them fail on main.

View guided diff

…y trace

A head sample rate of nan, inf or 1.5 skipped the sampler because the check
`head < 1` is false for them, so every trace was exported. A negative rate
reached OpenTelemetry's bare "Probability must be in range" error with no
mention of the setting.

Check the rate once in the Config constructor, so LOGFIRE_TRACE_SAMPLE_RATE,
the pyproject setting and SamplingOptions(head=...) all raise a
LogfireConfigError that names trace_sample_rate, like the not-a-number error
from pydantic#2685.
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-sdk/logfire/_internal
  config.py
Project Total  

This report was generated by python-coverage-comment-action

@Yasser-Ameur
Yasser-Ameur marked this pull request as ready for review November 16, 2026 22:27
@veria-ai

veria-ai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

View guided diff | Re-trigger cubic

Comment thread logfire-sdk/logfire/_internal/config.py Outdated
)
head = sampling.head
if isinstance(head, (int, float)) and not 0.0 <= head <= 1.0:
raise LogfireConfigError(f'Expected trace_sample_rate to be between 0.0 and 1.0, got {head!r}')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise LogfireConfigError(f'Expected trace_sample_rate to be between 0.0 and 1.0, got {head!r}')
raise LogfireConfigError(f'Expected sampling.head (LOGFIRE_TRACE_SAMPLE_RATE) to be between 0.0 and 1.0, got {head!r}')

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread tests/test_configure.py Resolved
@alexmojaki
alexmojaki marked this pull request as draft November 18, 2026 14:38
@alexmojaki
alexmojaki marked this pull request as ready for review November 18, 2026 14:54
@alexmojaki alexmojaki changed the title Reject a trace sample rate outside 0.0 to 1.0 instead of keeping every trace Breaking change: Reject a trace sample rate outside 0.0 to 1.0 instead of keeping every trace Nov 18, 2026
@alexmojaki
alexmojaki added this pull request to the merge queue Nov 18, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

View guided diff | Re-trigger cubic

Merged via the queue into pydantic:main with commit 0b51de5 Nov 18, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants