Implementing Distributed Tracing in Sentry for Microservice Latency Analysis
Learn how to implement Sentry Distributed Tracing using W3C Trace Context to track requests across microservices and identify latency bottlenecks.
25 Nov 2025, 14:47 UTC

The Visibility Gap in Microservices
When a user request fails or slows down in a microservice architecture, a single service log is rarely sufficient. The request typically hops through an API gateway, an authentication service, and multiple backend workers. Without distributed tracing, you see a "slow request" in Service A and a "slow process" in Service B, but you cannot prove they are the same transaction.
The solution is Distributed Tracing, which attaches a unique Trace ID to a request as it enters your system and propagates that ID across every network hop. Sentry implements this using the W3C Trace Context standard, allowing you to visualize the entire request lifecycle as a single timeline.
Prerequisites
- Sentry SDKs installed in all participating services (e.g., Python, Node.js, Go).
- A Sentry project created for each individual service to allow for distinct service naming in the trace view.
- Network connectivity allowing services to send events to the Sentry ingest endpoint.
Configuring the Sentry SDK for Tracing
Distributed tracing is not enabled by default because it generates a higher volume of data than error reporting. You must explicitly configure the traces_sample_rate.
Run the following configuration in the initialization block of each service. Warning: Setting this to 1.0 captures 100% of transactions, which can exhaust your Sentry quota rapidly in high-traffic environments.
# Example for a Python/Flask service
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
dsn="YOUR_SERVICE_SPECIFIC_DSN",
integrations=[FlaskIntegration()],
# Capture 10% of transactions for performance monitoring
traces_sample_rate=0.1,
# Set a profile_sample_rate to get deeper function-level insights
profiles_sample_rate=0.1,
)
How Trace Propagation Works
Sentry relies on the traceparent HTTP header to link services. When Service A calls Service B, the Sentry SDK automatically injects this header into the outgoing request. Service B then reads this header and starts its own span as a child of the original trace.
The Manual Propagation Risk: If you use a custom HTTP client or a low-level library that is not instrumented by the Sentry SDK, the traceparent header will not be sent. This breaks the chain, and Sentry will treat the request in Service B as a new, unrelated transaction.
Comparison: Native SDK vs. OpenTelemetry
| Feature | Sentry Native SDK | OpenTelemetry (OTel) Integration |
|---|---|---|
| Setup Effort | Low (Automatic instrumentation) | Medium (Requires OTel Collector/Config) |
| Overhead | Minimal | Variable based on Collector config |
| Compatibility | Limited to Sentry-supported languages | Polyglot (Any OTel-supported language) |
| Propagation | Automatic via W3C headers | Standardized via OTel propagators |
Verifying the Trace Chain
To ensure your distributed traces are functioning correctly, follow these verification steps:
- Inspect Network Headers: Use a tool like
curl -vor a browser inspector to trigger a request. Look for thetraceparentheader in the request sent from Service A to Service B. It should follow the format00-traceid-spanid-flags. - Check the Performance Dashboard: Navigate to the Performance tab in Sentry. Locate a transaction that spans multiple services.
- Confirm the Distributed Badge: A successful distributed trace will display a "Distributed" badge next to the transaction.
- Analyze the Timeline: Ensure the trace view shows a waterfall chart where spans from different services (identified by their project names) are nested chronologically.
Diagnostic Troubleshooting
If traces are appearing as fragmented single-service transactions rather than a unified chain, check for the following:
- Clock Skew: If servers have unsynchronized system clocks, spans may appear out of order or overlap incorrectly. Ensure all nodes use NTP (Network Time Protocol).
- Middleware Interference: Check if an API Gateway or Load Balancer is stripping unknown HTTP headers. The
traceparentheader must be allowed through to the backend services. - Sampling Mismatch: If Service A samples a request but Service B has
traces_sample_rate=0, the trace will be cut short at Service B.
Rollback and Mitigation
If you observe a performance degradation or a sudden spike in Sentry quota usage after enabling tracing, you can immediately reduce the impact without redeploying code if you use environment variables for configuration:
Update your environment variable to SENTRY_TRACES_SAMPLE_RATE=0 and restart the service. This disables performance monitoring while keeping error reporting active.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.