-
Notifications
You must be signed in to change notification settings - Fork 244
feat: surface X-Logfire-Warning header #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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
55e70c3
feat: surface X-Logfire-Warning / X-Logfire-Error response headers
adriangb eb3078f
feat: add AdvancedOptions.transport_response_hook to customize/opt out
adriangb 278814c
fix: mark transport_response_hook docstring example as skip-run
adriangb be7a64d
fix: pass transport_response_hook through lazy variable provider init
adriangb 1523cab
ServerResponseCallbackHelper
alexmojaki 90dc841
server_response_hook
alexmojaki 0d44d79
docstring
alexmojaki ee581a9
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
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,44 @@ | ||
| """Surface out-of-band signals the Logfire backend wants every SDK request to know about. | ||
|
|
||
| The server attaches the `X-Logfire-Warning` header to API responses to signal an | ||
| out-of-band warning the server wants the user to see. It is surfaced via | ||
| `warnings.warn(..., LogfireServerWarning)`. Python's standard "default" filter | ||
| dedupes identical messages, so a chatty server only warns once. | ||
|
|
||
| `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 a warning | ||
| if the `X-Logfire-Warning` response header is present. | ||
|
|
||
| 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 is not None: | ||
| 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.