-
Notifications
You must be signed in to change notification settings - Fork 232
feat: surface X-Logfire-Warning / X-Logfire-Error response headers #1906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adriangb
wants to merge
8
commits into
main
Choose a base branch
from
add-server-response-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9fbfb44
feat: surface X-Logfire-Warning / X-Logfire-Error response headers
adriangb 278a76c
feat: add AdvancedOptions.transport_response_hook to customize/opt out
adriangb 9aadfa8
fix: mark transport_response_hook docstring example as skip-run
adriangb 4808b4f
fix: pass transport_response_hook through lazy variable provider init
adriangb 44be40a
ServerResponseCallbackHelper
alexmojaki 878b000
server_response_hook
alexmojaki c57e3ba
docstring
alexmojaki 80334c4
remove server-side error header, keep warning
adriangb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """Surface out-of-band signals the Logfire backend wants every SDK request to know about. | ||
|
|
||
| The server attaches custom headers to API responses: | ||
|
|
||
| * `X-Logfire-Warning`: an out-of-band warning the server wants the user to see. | ||
| Surfaced via `warnings.warn(..., LogfireServerWarning)`. Python's standard | ||
| "default" filter dedupes identical messages, so a chatty server only warns once. | ||
| * `X-Logfire-Error`: an out-of-band error the server wants the SDK to raise. | ||
| Always raised as `LogfireServerError`. Callers that want to keep working past | ||
| it (the OTLP pipeline, the variables provider) already swallow exceptions from | ||
| their HTTP calls; CRUD/CLI propagate the error to the user. | ||
|
|
||
| `install_logfire_response_hook(session)` wires this into a `requests.Session` as | ||
| a response hook so every Logfire-bound HTTP response is inspected. Callers can | ||
| pass a custom `hook` to replace the default behavior (see | ||
| `AdvancedOptions.server_response_hook`). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| import requests | ||
|
|
||
| from logfire.types import ServerResponseCallback, ServerResponseCallbackHelper | ||
|
|
||
|
|
||
| def install_logfire_response_hook( | ||
| session: requests.Session, | ||
| hook: ServerResponseCallback | None = None, | ||
| ) -> None: | ||
| """Install a `requests` response hook on `session` for every Logfire API response. | ||
|
|
||
| By default, calls ServerResponseCallbackHelper.default_hook(), which emits warnings and raises errors based | ||
| on the presence of `X-Logfire-Warning` and `X-Logfire-Error` response headers. | ||
|
|
||
| Pass a custom callable to replace the default behavior (e.g. opt out by passing `lambda _: None`). | ||
| """ | ||
|
|
||
| def _hook(response: requests.Response, *args: Any, **kwargs: Any) -> requests.Response: | ||
| helper = ServerResponseCallbackHelper(response, args, kwargs) | ||
| if hook: | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
|
||
| hook(helper) | ||
| else: | ||
| helper.default_hook() | ||
| return response | ||
|
|
||
| response_hooks: list[Any] = session.hooks.setdefault('response', []) | ||
| response_hooks.append(_hook) | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.