perf(span-processor): defer span_formatter serialization behind DEBUG guard#1779
Open
samib6 wants to merge 1 commit into
Open
perf(span-processor): defer span_formatter serialization behind DEBUG guard#1779samib6 wants to merge 1 commit into
samib6 wants to merge 1 commit into
Conversation
… guard on_end evaluated span_formatter(span) via an f-string on every span regardless of log level, running a full serialization that was discarded when DEBUG was disabled. Guard it with isEnabledFor(DEBUG). Adds regression tests asserting span_formatter is not called when the logger is above DEBUG. Fixes langfuse/langfuse#15339
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.
What does this PR do?
LangfuseSpanProcessor.on_endlogs the full span at DEBUG level with an f-string:Because f-strings are built before the call,
span_formatter(span)(a full span serialization) runs on every span even when DEBUG logging is off — andlogger.debugthen discards the string. Sinceon_endruns once per span, that's wasted work on the hot path when debug isn't enabled. See #15339 for the profiling details.This guards the log behind
isEnabledFor(logging.DEBUG)so the serialization only happens when the output would actually be emitted. I kept the change to this one hot call — the other debug logs inon_endare in rarely-hit early-return/error branches, so I left them alone to keep the diff small.Fixes langfuse/langfuse#15339
Type of change
Verification
uv run --frozen pytest tests/unit/test_span_processor.py uv run --frozen ruff check . uv run --frozen mypy langfuse --no-error-summaryAdded two tests: one checks
span_formatterisn't called when the logger is above DEBUG (fails without this change), the other checks it still runs when DEBUG is on.Checklist
code_review.md..env.templateif needed. — n/a, internal-only changeGreptile Summary
This PR avoids unnecessary span serialization when DEBUG logging is disabled.
isEnabledFor(logging.DEBUG).Confidence Score: 5/5
The PR appears safe to merge with no actionable defects identified.
The new guard uses the standard logger’s effective-level check and preserves formatting whenever DEBUG is enabled while removing unnecessary serialization otherwise.
Reviews (1): Last reviewed commit: "perf(span-processor): defer span_formatt..." | Re-trigger Greptile