Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion logfire/_internal/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ..client import LogfireClient
from ..config import REGIONS, LogfireCredentials, get_base_url_from_token
from ..config_params import ParamManager
from ..server_response import install_logfire_response_hook
from ..tracer import SDKTracerProvider
from .auth import parse_auth, parse_logout
from .prompt import parse_prompt
Expand Down Expand Up @@ -434,8 +435,9 @@ def log_trace_id(response: requests.Response, context: ContextCarrier, *args: An
else:
with tracer.start_as_current_span('logfire._internal.cli'), requests.Session() as session:
context = get_context()
session.hooks = {'response': functools.partial(log_trace_id, context=context)}
session.hooks = {'response': [functools.partial(log_trace_id, context=context)]}
session.headers.update(context)
install_logfire_response_hook(session)
namespace._session = session
namespace.func(namespace)

Expand Down
2 changes: 2 additions & 0 deletions logfire/_internal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from logfire.version import VERSION

from .auth import UserToken, UserTokenCollection
from .server_response import install_logfire_response_hook
from .utils import UnexpectedResponse

UA_HEADER = f'logfire/{VERSION}'
Expand Down Expand Up @@ -38,6 +39,7 @@ def __init__(self, user_token: UserToken) -> None:
self._token = user_token.token
self._session = Session()
self._session.headers.update({'Authorization': self._token, 'User-Agent': UA_HEADER})
install_logfire_response_hook(self._session)

@classmethod
def from_url(cls, base_url: str | None) -> Self:
Expand Down
6 changes: 5 additions & 1 deletion logfire/_internal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
from .logs import ProxyLoggerProvider
from .metrics import ProxyMeterProvider
from .scrubbing import NOOP_SCRUBBER, BaseScrubber, Scrubber, ScrubbingOptions
from .server_response import install_logfire_response_hook
from .stack_info import warn_at_user_stacklevel
from .tracer import OPEN_SPANS, PendingSpanProcessor, ProxyTracerProvider
from .utils import (
Expand Down Expand Up @@ -1129,6 +1130,7 @@ def check_tokens():
base_url = self.advanced.generate_base_url(token)
headers = {'User-Agent': f'logfire/{VERSION}', 'Authorization': token}
session = OTLPExporterHttpSession()
install_logfire_response_hook(session)
span_exporter = BodySizeCheckingOTLPSpanExporter(
endpoint=urljoin(base_url, '/v1/traces'),
session=session,
Expand Down Expand Up @@ -1453,7 +1455,9 @@ def warn_if_not_initialized(self, message: str):
)

def _initialize_credentials_from_token(self, token: str) -> LogfireCredentials | None:
return LogfireCredentials.from_token(token, requests.Session(), self.advanced.generate_base_url(token))
session = requests.Session()
install_logfire_response_hook(session)
return LogfireCredentials.from_token(token, session, self.advanced.generate_base_url(token))

def _ensure_flush_after_aws_lambda(self):
"""Ensure that `force_flush` is called after an AWS Lambda invocation.
Expand Down
52 changes: 52 additions & 0 deletions logfire/_internal/server_response.py
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)
Copy link
Copy Markdown
Collaborator

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.

Copy link
Copy Markdown
Member Author

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to know what concrete use cases we have in mind for both warnings and errors.

From the PR description:

to deprecate endpoints or signal hard failures to any client running this SDK

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Member Author

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.

hard failures like what?

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 explanation Than hope the 4xx bubbles 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 🤔

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)
8 changes: 8 additions & 0 deletions logfire/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@

class LogfireConfigError(ValueError):
"""Error raised when there is a problem with the Logfire configuration."""


class LogfireServerError(Exception):
"""Error raised when the Logfire server returns an `X-Logfire-Error` header on a response."""


class LogfireServerWarning(UserWarning):
"""Warning emitted when the Logfire server returns an `X-Logfire-Warning` header on a response."""
3 changes: 3 additions & 0 deletions logfire/variables/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from logfire._internal.client import UA_HEADER
from logfire._internal.config import VariablesOptions
from logfire._internal.server_response import install_logfire_response_hook
from logfire._internal.utils import UnexpectedResponse
from logfire.variables.abstract import (
ResolvedVariable,
Expand Down Expand Up @@ -69,6 +70,7 @@ def __init__(self, base_url: str, token: str, options: VariablesOptions):
self._token = token
self._session = Session()
self._session.headers.update({'Authorization': f'bearer {token}', 'User-Agent': UA_HEADER})
install_logfire_response_hook(self._session)
self._timeout = options.timeout
self._block_before_first_fetch = block_before_first_resolve
self._polling_interval: timedelta = (
Expand Down Expand Up @@ -197,6 +199,7 @@ def _sse_listener(self): # pragma: no cover
'Cache-Control': 'no-cache',
}
)
install_logfire_response_hook(sse_session)

# Open streaming connection
response = sse_session.get(sse_url, stream=True, timeout=(10, None))
Expand Down
78 changes: 78 additions & 0 deletions tests/test_server_response.py
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()
Loading