Add level param to @logfire.instrument()#1871
Open
imp-joshi wants to merge 9 commits intopydantic:mainfrom
Open
Add level param to @logfire.instrument()#1871imp-joshi wants to merge 9 commits intopydantic:mainfrom
imp-joshi wants to merge 9 commits intopydantic:mainfrom
Conversation
- adds _level: LevelName | int | None = None to Logfire.instrument() - level attrs merged into span attributes at decoration time (static) - _level=None leaves the existing fast path untouched - test: warn-level span carries logfire.level_num=13
- extracts level_num at decoration time for O(1) call-time check - guard (level_num < config.min_level) added to all three open_span variants - NoopSpan._span property returns trace_api.INVALID_SPAN - set_user_attributes_on_raw_span exits cleanly via is_recording(); fixes latent crash on record_return=True
- async path works via same open_span closure, verified with snapshot (logfire.level_num: 13) - extract_args + _level test verifies both attrs appear together - shim gets explicit _level=None kwarg
…or py3.9/3.10 compat
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
alexmojaki
reviewed
Apr 22, 2026
- hoist NoopSpan import to top of get_open_span instead of re-importing in each closure - drop duplicated per-call min-level guards from the three open_span variants - return a noop-only open_span early when level is below min_level - drop stale NoopSpan._span comment (is_recording just below already returns False)
decorate-first-configure-later is a valid usage pattern, so the check has to read config.min_level at each call, not once at decoration time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1452.
Logfire's span APIs (
logfire.span,logfire.debug,logfire.warn, etc.) all let you record a span with a log level attached, whichmin_levelthen filters against.@logfire.instrument()was the only one that didn't. This PR closes that gap.What changed
You can now do:
and it behaves exactly like tagging a manual
with logfire.span("my span"):block at warn level. The span gets the warn-level attributes, and if yourmin_levelis above warn, the span is suppressed entirely.The
level=Nonedefault is completely untouched, no overhead on existing code.Implementation note
log_level_attributes(level)runs once at decoration time and merges into the attribute dict. Thelevel_num < config.min_levelcheck runs at each call so it picks up anylogfire.configure()that happens after decoration; if the level is belowmin_level,open_spanreturns aNoopSpanand skips the work.NoopSpan._span (pre-existing bug, fixed here)
When
open_spanreturns aNoopSpan(now an intentional code path, not just an error case),record_return=Truewould crash trying to call.is_recording()on a lambda returned by__getattr__. Added_spanas an explicit property returningtrace_api.INVALID_SPANsoset_user_attributes_on_raw_spanexits cleanly.Tests
test_instrument_with_level- warn level, verifieslogfire.level_num: 13on the spantest_instrument_level_filtered- debug withmin_level="info", nothing exportedtest_instrument_level_filtered_record_return- same but withrecord_return=True(exercises the NoopSpan fix)test_instrument_with_level_async- async pathtest_instrument_with_level_and_extract_args- level attrs and function args coexisttest_instrument_level_filtered_extract_args- filtering withextract_args=Truetest_instrument_level_filtered_extract_args_iterable- filtering withextract_args=('x',)