Why sampling?
- Reduces network, storage, and processing costs by exporting fewer traces.
- Keeps a representative subset of traces for performance and error analysis.
- Deterministic sampling (based on trace ID) ensures consistent decisions across services that share the same trace context.
Example: TraceIdRatioBased sampler (10%) + Console exporter
Below is a complete example that sets up:- a
TraceIdRatioBasedsampler with 10% sampling, - a
BatchSpanProcessor, - a
ConsoleSpanExporterfor demo output (with an OTLP exporter commented out if you want to send traces to a collector instead).
The
TraceIdRatioBased sampler uses the trace ID to produce a deterministic sampling decision. A ratio of 0.1 will sample approximately 10% of traces; 0.0 samples none and 1.0 samples all.How to use this in your app
- Call
configure_tracer()during application startup (before handling incoming requests). - Run your service (for example, a Flask app exposing a
/chargeendpoint). - Generate requests (e.g., with
curl) and watch application stdout for exported spans when requests are sampled.
TraceIdRatioBased(0.1) sampler.
Sampler options at a glance
| Sampler | Use case | Example |
|---|---|---|
TraceIdRatioBased | Probabilistic sampling using trace ID for consistent sampling across services | TraceIdRatioBased(0.1) |
AlwaysOn | Capture all traces (useful for debugging) | AlwaysOn() |
AlwaysOff | Capture no traces (useful to disable tracing) | AlwaysOff() |
Resource example
When creating resources, include identifying attributes for your service:- Example resource used above:
{ "service.name": "charge-service", "service.version": "0.5.0" }
Best practices & troubleshooting
- Use a conservative sampling ratio in production to control telemetry costs; consider higher sampling for error traces or slow requests.
- If you require guaranteed capture of specific requests (e.g., errors), implement custom sampling logic or use a tail-based sampling approach at the collector.
- If you switch to OTLP, verify your collector/backend is reachable and the endpoint is correctly configured.
- Monitor the rate of sampled traces and adjust the ratio if you need more or fewer traces.
Avoid setting
TraceIdRatioBased too low in early production or when first instrumenting a service—you may miss important traces. For debugging or initial rollout, start with higher sampling and reduce once you have confidence in coverage and cost.Links and references
- Console exporter docs: https://opentelemetry-python.readthedocs.io/en/stable/
- OTLP exporter docs: https://opentelemetry-python.readthedocs.io/en/stable/exporter/otlp/
- OTLP protocol: https://opentelemetry.io/docs/reference/specification/protocol/otlp/
- Flask: https://flask.palletsprojects.com/