-
Notifications
You must be signed in to change notification settings - Fork 231
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
4
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 1 commit
Commits
Show all changes
4 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 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,52 @@ | ||
| """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. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import warnings | ||
| from typing import Any | ||
|
|
||
| import requests | ||
|
|
||
| from logfire.exceptions import LogfireServerError, LogfireServerWarning | ||
|
|
||
| WARNING_HEADER_NAME = 'X-Logfire-Warning' | ||
| ERROR_HEADER_NAME = 'X-Logfire-Error' | ||
|
|
||
|
|
||
| def process_logfire_response_headers(response: requests.Response, *_args: Any, **_kwargs: Any) -> requests.Response: | ||
| """Handle `X-Logfire-Warning` / `X-Logfire-Error` headers on a Logfire API response. | ||
|
|
||
| Designed to be installed as a `requests` response hook | ||
| (`session.hooks['response'].append(...)`). | ||
| """ | ||
| warning_message = response.headers.get(WARNING_HEADER_NAME) | ||
| if warning_message: | ||
| warnings.warn(warning_message, LogfireServerWarning, stacklevel=2) | ||
| error_message = response.headers.get(ERROR_HEADER_NAME) | ||
| if error_message: | ||
| raise LogfireServerError(error_message) | ||
| return response | ||
|
|
||
|
|
||
| def install_logfire_response_hook(session: requests.Session) -> None: | ||
| """Install `process_logfire_response_headers` as a response hook on `session`. | ||
|
|
||
| `requests.Session()` always initialises `hooks['response']` to a list, and every | ||
| call site here passes a freshly-built session, so we just append. | ||
| """ | ||
| response_hooks: list[Any] = session.hooks.setdefault('response', []) | ||
| response_hooks.append(process_logfire_response_headers) | ||
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,78 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import warnings | ||
|
|
||
| import pytest | ||
| import requests | ||
| import requests_mock | ||
| from inline_snapshot import snapshot | ||
|
|
||
| from logfire._internal.server_response import ( | ||
| ERROR_HEADER_NAME, | ||
| WARNING_HEADER_NAME, | ||
| process_logfire_response_headers, | ||
| ) | ||
| from logfire.exceptions import LogfireServerError, LogfireServerWarning | ||
|
|
||
|
|
||
| def test_process_response_warning_header_emits_warning(): | ||
| response = requests.Response() | ||
| response.headers[WARNING_HEADER_NAME] = 'The /foo/bar endpoint is deprecated, please use /bar/baz' | ||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter('always') | ||
| process_logfire_response_headers(response) | ||
| assert [(w.category, str(w.message)) for w in caught] == snapshot( | ||
| [(LogfireServerWarning, 'The /foo/bar endpoint is deprecated, please use /bar/baz')] | ||
| ) | ||
|
|
||
|
|
||
| def test_process_response_warning_header_dedupes(): | ||
| """Python's default `warnings` filter should fold repeats of the same message into one entry.""" | ||
| response = requests.Response() | ||
| response.headers[WARNING_HEADER_NAME] = 'a duplicated warning' | ||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter('default') | ||
| for _ in range(5): | ||
| process_logfire_response_headers(response) | ||
| messages = [str(w.message) for w in caught] | ||
| assert messages == ['a duplicated warning'] | ||
|
|
||
|
|
||
| def test_process_response_error_header_raises(): | ||
| response = requests.Response() | ||
| response.headers[ERROR_HEADER_NAME] = 'something is wrong' | ||
| with pytest.raises(LogfireServerError, match='something is wrong'): | ||
| process_logfire_response_headers(response) | ||
|
|
||
|
|
||
| def test_response_hook_installed_on_logfire_client(): | ||
| from logfire._internal.auth import UserToken | ||
| from logfire._internal.client import LogfireClient | ||
|
|
||
| token = UserToken( | ||
| token='pylf_v1_us_xxx', | ||
| base_url='https://logfire-us.pydantic.dev', | ||
| expiration='2099-12-31T23:59:59', | ||
| ) | ||
| client = LogfireClient(user_token=token) | ||
|
|
||
| with requests_mock.Mocker() as m: | ||
| m.get( | ||
| 'https://logfire-us.pydantic.dev/v1/account/me', | ||
| json={'name': 'me'}, | ||
| headers={WARNING_HEADER_NAME: 'deprecated endpoint'}, | ||
| ) | ||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter('always') | ||
| client.get_user_information() | ||
|
|
||
| assert any(isinstance(w.message, LogfireServerWarning) for w in caught) | ||
|
|
||
| with requests_mock.Mocker() as m: | ||
| m.get( | ||
| 'https://logfire-us.pydantic.dev/v1/account/me', | ||
| json={'name': 'me'}, | ||
| headers={ERROR_HEADER_NAME: 'no longer supported'}, | ||
| ) | ||
| with pytest.raises(LogfireServerError, match='no longer supported'): | ||
| client.get_user_information() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I worry that users might have a reason to opt out of this and won't be able to. Maybe that's paranoid.
We could theoretically make this hook configurable, defaulting to this one, but allowing users to take any action for all API responses. Probably overkill.
I'd like to know what concrete use cases we have in mind for both warnings and errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a way to opt out in 278a76c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the PR description:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'd deprecate endpoints by changing code here though, right? or is the point to push users to upgrade their sdk?
hard failures like what? where would the server respond with the error header instead of a 4xx/5xx which would have a similar effect here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes the point is that we can propagate deprecation to old versions of the SDK.
The error part was a bit more just "seems like it could be useful". But I imagine it might be more informative to raise a
LogfireError: Some explanationThan hope the4xxbubbles up correctly in a way that is usable by users.I also wonder if we could emit an error / warn logfire log so it shows up in users projects 🤔