Connecting Frontend to Backend for Distributed Tracing
Review how trace propagation works
This is the use case we discussed earlier where we incorporate distributed tracing between the frontend and backend, in our case a Sentry Frontend + OTEL Backend. We will be using the propagateTraceparent configuration in our Sentry SDK to enable this.
propagateTraceparent
The traceparent header contains the same information (Trace ID, Parent Span ID, flags), just in a standardized format that OpenTelemetry can read.
When enabled, Sentry’s SDK sends all three headers:
fetch('http://localhost:3000/api/orders', { headers: { 'sentry-trace': '4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1', 'baggage': 'sentry-trace_id=4bf92f3577b34da6a3ce929d0e0e4736,sentry-environment=production', 'traceparent': '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01', // OpenTelemetry expects this W3C standard header }})Now your OpenTelemetry backend can:
- Extract the
traceparentheader - Continue the same trace with the same Trace ID
- Create backend spans as children of frontend spans
- Send everything to Sentry via OTLP
The result is a unified trace that flows from browser → React → API → Database, all visible in Sentry’s trace view.
Setting Up Your Sentry Project
Before connecting the frontend, you’ll need a Sentry project to send data to. If you already have a project set up, you can skip to the next section.
-
Create a new Sentry project
- Log in to sentry.io
- Navigate to Projects in the main navigation
- Click Create Project in the top right
- Select React as the platform (for the frontend)
- Set Alert Frequency to your preference (we recommend “Alert me on every new issue” for this workshop)
- Give your project a name like
otel-workshop-frontend - Choose or create a Team to own this project
- Click Create Project
-
Copy your DSN
After creating the project, Sentry will show you the DSN (Data Source Name). This is the URL that tells the Sentry SDK where to send data. You can always find it later in: Settings > Projects > [Your Project] > Client Keys (DSN)
-
Upload sourcemaps to Sentry (recommended)
Sourcemaps are used to map compiled code back to the original source files, allowing Sentry to show you the correct line of code in the error stack trace.
Terminal window npx @sentry/wizard@latest -i sourcemaps
Configuring the Frontend
Now that you have a Sentry project and DSN, let’s configure the React frontend.
-
Add your Sentry DSN to environment variables
The frontend is already set up - just add your DSN to the
frontend/.envfile:Add your Sentry DSN:
Terminal window VITE_API_URL=http://localhost:3000/apiVITE_SENTRY_DSN=<paste React project DSN> -
Verify the Sentry configuration
Open
frontend/instrument-sentry.jsand verify it has the correct trace propagation settings:Sentry.init({dsn: import.meta.env.VITE_SENTRY_DSN,environment: import.meta.env.MODE || "development",sendDefaultPii: true,integrations: [Sentry.browserTracingIntegration(),Sentry.replayIntegration(),],// Session Replay settingsreplaysSessionSampleRate: 0.1,replaysOnErrorSampleRate: 1.0,tracesSampleRate: 1.0,// Control for which URLs trace propagation should be enabled// Accepts strings or regextracePropagationTargets: ["localhost","/api",],// 👉 IMPORTANT: Enable trace propagation between frontend and backendpropagateTraceparent: true,}); -
Start the frontend
Terminal window npm run dev:frontendThe frontend will be available at
http://localhost:5173(or5174if 5173 is taken) -
Test the full flow
- Open
http://localhost:5173in your browser - Open Browser DevTools (F12 or right-click > Inspect)
- Go to the Network tab
- Click on any product to view details
- Look for the API request to
localhost:3000/api/products/1 - Click on the request and view the Request Headers
You should see these headers:
sentry-trace: 4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-1baggage: sentry-trace_id=4bf92f3577b34da6a3ce929d0e0e4736,...traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01 - Open
-
Generate real traffic
Browse the app:
- View multiple products
- Add items to cart
- Go through checkout
- Place an order
-
Verify in Sentry
- Go to your Sentry project
- Navigate to Explore > Traces
- Find a recent trace
- Click to open the trace details
The frontend and backend spans are in the same trace!
What You Should See in Sentry
Frontend Transactions
pageload- Initial page loadnavigation- Client-side navigationhttp.clientspans for API calls
Backend Transactions
http.server- Incoming API requestsdb.queryspans for database operationscache.get/cache.setspans for Redis- Custom spans like
payment.process
Connected Spans
All spans with the same Trace ID are grouped together in one view!