|
| 1 | +# Distributed tracing demo |
| 2 | + |
| 3 | +This project demonstrates distributed Logfire tracing from the browser (React |
| 4 | +app) to the backend (FastAPI Python app using PydanticAI). |
| 5 | + |
| 6 | +# Key concepts |
| 7 | + |
| 8 | +The React application (`frontend/`) is instrumented using the |
| 9 | +`@pydantic/logfire-browser` NPM package. Details about the instrumentation |
| 10 | +specifics can be found in the `ClientInstrumentationProvider.tsx` file. |
| 11 | + |
| 12 | +For security/performance reasons, the browser instrumentation does not send traces to Logfire directly, but |
| 13 | +instead sends them to the backend. The backend then proxies the traces to |
| 14 | +Logfire - the `/client-traces` FastAPI endpoint in `main.py`. In that way, the Logfire write token is not exposed in the client-side bundle. |
| 15 | +In production, you should ensure that the proxy endpoint is Auth protected and only accessible to your frontend. |
| 16 | + |
| 17 | +# Running the demo |
| 18 | + |
| 19 | +To run the demo, you need to have Python 3.10+ and Node.js 22+ installed. First, |
| 20 | +install the dependencies: |
| 21 | + |
| 22 | +```bash |
| 23 | +make install |
| 24 | +``` |
| 25 | + |
| 26 | +Then, export the following environment variables: |
| 27 | + |
| 28 | +```bash |
| 29 | +export LOGFIRE_TOKEN=<your-logfire-token> |
| 30 | +export LOGFIRE_BASE_URL=https://logfire-us.pydantic.dev/ # e.g. https://logfire-eu.pydantic.dev/ or https://logfire-us.pydantic.dev/ |
| 31 | +export OPENAI_API_KEY=<your-openai-api-key> # you can get one from https://platform.openai.com/account/api-keys |
| 32 | +``` |
| 33 | + |
| 34 | +Run the backend: |
| 35 | + |
| 36 | +```bash |
| 37 | +make backend-dev |
| 38 | +``` |
| 39 | + |
| 40 | +Then, in another shell, run the frontend: |
| 41 | + |
| 42 | +```bash |
| 43 | +make frontend-dev |
| 44 | +``` |
| 45 | + |
| 46 | +The App will be available at (http://localhost:5173)[http://localhost:5173]. You |
| 47 | +can open your Logfire live view and interact with the App to see the traces in |
| 48 | +real-time. |
| 49 | + |
| 50 | +# Further reading |
| 51 | + |
| 52 | +- [Distributed tracing with Logfire](https://logfire.pydantic.dev/docs/how-to-guides/distributed-tracing/) |
| 53 | +- [Logfire Python SDK](https://github.com/pydantic/logfire) |
| 54 | +- [Logfire JS](https://github.com/pydantic/logfire-js) |
0 commit comments