feat: add browser proxy helpers: forward_export_request and logfire_proxy#1697
Merged
alexmojaki merged 27 commits intopydantic:mainfrom Feb 26, 2026
Merged
feat: add browser proxy helpers: forward_export_request and logfire_proxy#1697alexmojaki merged 27 commits intopydantic:mainfrom
forward_export_request and logfire_proxy#1697alexmojaki merged 27 commits intopydantic:mainfrom
Conversation
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
Author
|
@alexmojaki comments are resolved and all tests are passing. |
alexmojaki
reviewed
Feb 18, 2026
| new_headers['Authorization'] = token | ||
|
|
||
| try: | ||
| response = requests.request( |
Collaborator
There was a problem hiding this comment.
ideally this would use a session created in logfire.configure, which in particular would handle retrying. let that be a followup.
| method=method, | ||
| url=url, | ||
| headers=new_headers, | ||
| data=body, |
Collaborator
There was a problem hiding this comment.
possible followup: gzip compression
alexmojaki
reviewed
Feb 23, 2026
alexmojaki
reviewed
Feb 24, 2026
Contributor
Author
forward_request and `instrument_fastapi_proxyforward_export_request and logfire_proxy
Collaborator
|
Thanks! Please also write some documentation. |
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.
Closes #1640
Description
This PR adds experimental helper functions to simplify setting up a backend proxy for browser monitoring.
Problem:
To send traces/logs from a browser (frontend) to Logfire, the browser needs to authenticate. However, exposing the Logfire Write Token in frontend code is insecure. The recommended solution is to send data to the backend, which then forwards it to Logfire with the token injected.
Solution (Updated):
This PR introduces two new functions in
logfire.experimental.forwardingto make this pattern trivial to implement while allowing maximum flexibility for production security:forward_export_request(...): A low-level function that takes request details (method, path, headers, body), strips sensitive client headers, injects the server's configured Logfire token, and securely forwards it to the Logfire API.logfire_proxy: An asynchronous Starlette/FastAPI handler. By exposing this as a standalone handler rather than a baked-in method, developers can mount it to their own routes. This makes it easy to add FastAPI dependencies (like custom authentication, CORS, or rate-limiting) to protect the endpoint.Usage Example
FastAPI
Implementation Details
forward_export_requeststrictly validates that the method isPOSTand the target path starts with/v1/traces,/v1/logs, or/v1/metrics. It also normalizes paths to prevent directory traversal attacks.Host,Content-Length) and sensitive headers (like client-sideAuthorizationorCookie) are stripped before forwarding.logfire_proxyhandler enforces a configurable body size limit (default 50MB) checking both theContent-Lengthheader early and the actual body read late.logfire.suppress_instrumentation()so the proxy doesn't generate infinite traces of its own exported telemetry.Changes
logfire/experimental/forwarding.pyto encapsulate the forwarding logic and ASGI handler, keeping the coreLogfireclass clean.tests/test_experimental_forwarding.pycovering path traversal, limits, method restrictions, explicit configs.