Browser to Backend Tracing

Continue a trace started by the Sophonz Browser SDK in a Python backend and into PostgreSQL — tracePropagationTargets, CORS for traceparent, tracestate, and baggage, session.id on server spans, and how to verify the link.

One user action can be followed as one trace: the browser's fetch span, the Python server span it caused, the database queries, and the spans PostgreSQL records for them. Two headers carry it across the network. traceparent links the server span to the browser span, and baggage carries the browser's session.id, which the SDK puts on every server span. If any step is missing, nothing fails: the request succeeds and the server simply starts a new trace.

The path

StepWhereWhat has to be true
1Browser SDKThe request URL matches tracePropagationTargets, so traceparent and baggage are added.
2BrowserThe CORS preflight response allows traceparent, tracestate, and baggage.
3Python serverThe tracecontext and baggage propagators are active (the default), so the server span continues the browser's trace.
4Python SDKBaggage is copied onto every span; session.id is promoted to its own attribute.
5Database driverSQLCommenter appends traceparent to each statement.
6PostgreSQLpg_tracing records server-side spans under the query span.

Steps 5 and 6 are covered in Database Tracing. This page covers the rest.

1. Browser: choose where headers go

The Browser SDK adds the headers only to requests whose URL matches tracePropagationTargets:

SophonzSDK.init({
  collectorUrl: '{{SOPHONZ_TRACES_COLLECTOR_URL}}',
  appName: 'shop-web',
  appKey: '{{YOUR_APP_KEY}}',
  project: 'shop',
  tracePropagationTargets: ['api.example.com'],
});
  • A string is a host. The scheme, port, and path are ignored, and the host is matched from the start of the URL, so api.example.com matches https://api.example.com/v1/orders but not https://evil.example/?q=api.example.com.
  • To match by path, use a regular expression, for example /^https:\/\/example\.com\/api\//.
  • An empty list, or no option, sends the headers to every origin.

List only your own APIs. The baggage header carries the session ID, which identifies one person's visit, and goes to exactly the origins that receive traceparent.

The fetch and XHR instrumentations send:

traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
baggage: session.id=8f2c1d0e6b7a4c3f9e5d2a1b0c9d8e7f

tracestate is sent as well when the trace has one.

2. Server: allow the headers in CORS

traceparent and baggage are not CORS-safelisted headers, so a cross-origin request that carries them is preflighted. If the preflight response does not allow them, the browser blocks the request itself, not just the tracing. Allow all three headers, next to the ones your API already needs.

pip install flask-cors
from flask_cors import CORS
 
CORS(
    app,
    origins=["https://shop.example.com"],
    supports_credentials=True,
    allow_headers=["content-type", "authorization", "traceparent", "tracestate", "baggage"],
)

The preflight OPTIONS request itself carries no traceparent; browsers do not send custom headers on it. Its server span is a separate, one-span trace. That is expected.

NOTE — Allowing every origin

The Sophonz sample servers reflect any origin and any requested header, because a public test page calls them. For a production API, list the origins that call it.

3. Server: continue the trace

Nothing has to be configured. OpenTelemetry's default propagators are tracecontext,baggage, and the server instrumentation for Flask, FastAPI, and Django reads both headers from each request. The server span gets the browser span as its parent, and its context carries the baggage.

If you set OTEL_PROPAGATORS, keep both entries:

export OTEL_PROPAGATORS=tracecontext,baggage

With only tracecontext, the trace still continues, but session.id no longer reaches the server spans.

The default sampler, parentbased_always_on, keeps every trace whose traceparent has the sampled flag set, so a trace the browser recorded is never cut at the server. A different OTEL_TRACES_SAMPLER applies only to traces that start on the server, as long as it is one of the parentbased_* samplers.

4. session.id on every span

For each span, the SDK copies every baggage entry to baggage.<key> and the entries in promote_baggage_keys (default ["session.id"]) to their own name. With the request above, the server span, a span you start in the handler, and each database span all carry:

AttributeValue
session.id8f2c1d0e6b7a4c3f9e5d2a1b0c9d8e7f
baggage.session.id8f2c1d0e6b7a4c3f9e5d2a1b0c9d8e7f

On browser telemetry session.id is a resource attribute; on server spans it is a span attribute with the same name. A query on session.id returns both halves of the session.

The framework helpers additionally put http.client.ip and http.user_agent on every span of the request. Behind a reverse proxy the peer address is the proxy's; trust X-Forwarded-For with ProxyFix in Flask, --proxy-headers in uvicorn, or a middleware that sets REMOTE_ADDR in Django. See the Framework Guides.

Match the service namespace

Dashboards scope by service.namespace. The Browser SDK sends its project option as service.namespace; give the backend the same value, or its spans drop out of namespace-filtered views even when traces join correctly:

export SOPHONZ_SERVICE_NAMESPACE=shop
# or, as the sample servers do
export OTEL_RESOURCE_ATTRIBUTES=service.namespace=shop

Verifying

Test the server on its own first, with a header you made up:

curl -i https://api.example.com/api/users \
  -H "traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" \
  -H "baggage: session.id=test-session-1"

Run the server with SOPHONZ_DEBUG_PAYLOAD=true. The server span in the console output should show:

  • trace ID 4bf92f3577b34da6a3ce929d0e0e4736;
  • parent span ID 00f067aa0ba902b7;
  • session.id set to test-session-1, also on the database spans.

If that works, the server side is correct. Then check the browser: in the developer tools network tab, the API request should carry traceparent and baggage, and its preflight should have succeeded. A server span that starts a new trace for browser requests, but not for the curl request, means the browser did not send the header: check tracePropagationTargets and the CORS response.

To check a preflight from the command line:

curl -i -X OPTIONS https://api.example.com/api/users \
  -H "Origin: https://shop.example.com" \
  -H "Access-Control-Request-Method: GET" \
  -H "Access-Control-Request-Headers: traceparent,tracestate,baggage"

The response must include Access-Control-Allow-Headers naming all three and Access-Control-Allow-Origin with your origin.

Next steps

TIP — This is just the beginning

Now ship your app. Sophonz surfaces the problems real users hit — so your app evolves on its own.