Tracing sampling is a crucial technique for managing the volume of trace data in production environments. By default, Traceloop traces every request, which works well for debugging or development but can become expensive and noisy in high-traffic production systems.

Sampling helps in several ways: it reduces noise in traces, helping you focus on important traces while maintaining visibility into your system. It also helps with cost management in terms of storage, processing, and network bandwidth, making it essential for production deployments.

Sampling Strategies

OpenTelemetry supports several built-in samplers, but in practice, two cover most use cases:

1. TraceIdRatioBased Sampler

Samples a fixed percentage of root traces. This sampler makes sampling decisions independently for each trace.

import os
from traceloop.sdk import Traceloop
from opentelemetry.sdk.trace.sampling import TraceIdRatioBased

# Initialize Traceloop
TFY_API_KEY = os.environ.get("TFY_API_KEY")
Traceloop.init(
    api_endpoint="https://platform.live-demo.truefoundry.cloud/api/otel",
    headers = {
        "Authorization": f"Bearer {TFY_API_KEY}",
        "TFY-Tracing-Project": "your-tracing-project-fqn",
    },
    # configure sampling (10% of traces)
    sampler = TraceIdRatioBased(0.1)
)

Pros: Simple to configure, predictable sampling rate, deterministic behavior

Cons: May create partial traces if child spans are sampled differently, especially when spans are spread across multiple microservices or services with different sampling configurations.

Samples a fixed percentage of root traces and ensures that child spans follow the parent’s sampling decision, maintaining complete trace integrity.

import os
from traceloop.sdk import Traceloop
from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased

# Initialize Traceloop
TFY_API_KEY = os.environ.get("TFY_API_KEY")
Traceloop.init(
    api_endpoint="https://platform.live-demo.truefoundry.cloud/api/otel",
    headers = {
        "Authorization": f"Bearer {TFY_API_KEY}",
        "TFY-Tracing-Project": "your-tracing-project-fqn",
    },
    # configure sampling (10% of traces)
    sampler = ParentBased(
        TraceIdRatioBased(0.1) 
    )
)

Pros: Maintains trace integrity, prevents partial traces, ensures complete trace visibility when sampled

Cons: Slightly more complex configuration, but worth the additional setup for production environments

Troubleshooting

Partial Traces

If you see partial traces (missing spans in the middle of a trace), ensure you’re using ParentBased sampler:

# ❌ May create partial traces across services
sampler = TraceIdRatioBased(0.1)

# ✅ Maintains trace integrity across all services
sampler = ParentBased(TraceIdRatioBased(0.1))

Too Much Data

If you’re still collecting too much data or experiencing high costs, reduce the sampling rate:

# Reduce from 10% to 5% for high-traffic environments
sampler = ParentBased(TraceIdRatioBased(0.05))

# For very high traffic, consider 1% sampling
sampler = ParentBased(TraceIdRatioBased(0.01))

Too Little Data

If you need more visibility for debugging or monitoring, increase the sampling rate:

# Increase from 10% to 25% for better visibility
sampler = ParentBased(TraceIdRatioBased(0.25))

# For critical debugging, consider 50% or higher
sampler = ParentBased(TraceIdRatioBased(0.5))