CLOUD CONCEPT

Trace Sampling

Trace sampling selects which distributed requests have their tracing data recorded or retained, balancing diagnostic coverage against telemetry cost and overhead.

Cloud Concepts Hub →

What is Trace Sampling

Distributed tracing follows a request across services, while sampling determines which requests contribute detailed trace data. A well-designed sampling policy controls telemetry volume without losing too much visibility into failures, latency, and important application workflows.

Trace sampling controls the volume of distributed tracing data collected from an application. A trace represents a request or operation across service boundaries, and spans represent individual units of work within that trace. Recording every request can generate substantial processing, network, and storage costs, particularly in high-throughput applications. Sampling preserves selected traces rather than retaining detailed execution data for every request.

Head sampling makes its decision near the beginning of a request, before the outcome is known. A probability-based sampler might select approximately 5% of incoming requests, while a rule-based sampler might use the request path, service name, or other attributes available at that point. For example, an application could sample a larger proportion of checkout requests than health checks. A 5% probability is a long-term expectation, not a guarantee that exactly five out of every hundred requests will be captured.

Tail sampling delays the retention decision until spans have reached a collector and enough information is available to evaluate the trace. It can preferentially retain traces containing errors or high latency. This improves diagnostic coverage for unusual outcomes, but requires buffering, additional processing, and routing related spans to the same decision-making collector. Tail sampling cannot recover spans that an upstream head sampler already discarded, and incomplete or late-arriving spans can affect its decisions.

Sampling should generally be coordinated across the request path. Services propagate trace identifiers, parent relationships, and sampling information through supported trace-context headers. Downstream instrumentation commonly follows the upstream sampling decision so that selected requests produce connected traces. Independently sampling every service can produce fragmented traces that show only part of a transaction. For asynchronous processing, verify that producers, messaging integrations, and consumers preserve the tracing context required by the instrumentation.

On AWS, AWS X-Ray supports centralized sampling rules that compatible instrumentation uses to decide which requests to trace. Rules can match request properties and combine a reservoir target with a fixed sampling rate for additional matching traffic. The reservoir helps retain visibility into low-volume operations, while the rate controls sampling as traffic grows. Rule priority matters when multiple rules match. Centralized reservoir allocation also differs from local fallback sampling, where independently operating application instances can each apply their own reservoir allowance.

A practical implementation might enable X-Ray tracing on an Amazon API Gateway REST API, instrument application code using OpenTelemetry with an appropriate exporter, and propagate context to downstream services. If the REST API invokes AWS Lambda, active tracing allows Lambda to contribute tracing data, while instrumentation inside the function adds detail about application operations and downstream calls. Lambda follows an upstream sampling decision when one is provided; its own sampling behavior is not configured by assigning it arbitrary X-Ray sampling rules. Enabling tracing alone does not guarantee that every request is captured or every internal operation is instrumented.

Sampling is not a substitute for metrics, structured logs, or audit records. A rare failure may never appear in a low-rate sample, and error-focused tail sampling creates a dataset that is intentionally biased toward failures. Use request metrics for reliable traffic counts and error rates, and correlate logs with trace identifiers where supported. During troubleshooting, temporarily increasing sampling for a narrowly scoped route can help, but monitor ingestion cost and collector capacity. If traces remain missing, also check instrumentation, context propagation, export configuration, network access, and IAM permissions rather than assuming sampling is responsible.

Key aspects of Trace Sampling

Effective trace sampling coordinates selection across services while balancing diagnostic coverage, statistical bias, telemetry cost, and collection capacity.

Early versus outcome-aware decisions

Head sampling makes an inexpensive decision before execution completes. Tail sampling can consider errors and latency, but requires collectors to buffer and evaluate incoming spans.

Trace-wide consistency

Propagate trace context and sampling information across service boundaries. Compatible downstream instrumentation should usually honor the upstream decision to avoid fragmented traces.

Probability, rules, and reservoirs

Probability-based policies control average collection volume, while rules prioritize selected workloads. Reservoir targets can preserve baseline visibility for low-traffic operations, but their scope depends on whether sampling is centrally coordinated or applied locally.

Diagnostic coverage and statistical bias

Lower sampling rates increase the chance of missing rare problems. Policies that favor failures or slow requests improve troubleshooting coverage but make sampled traces unsuitable for directly calculating overall error rates or latency distributions.

Cost and pipeline capacity

Sampling can reduce export traffic, backend ingestion, and storage requirements. Tail sampling still requires upstream recording and transport to the decision point, so it does not eliminate all instrumentation or collection overhead.

Operational verification

Test sampling with known requests and verify connected traces across dependencies. Missing data can also indicate broken context propagation, exporter failures, insufficient permissions, or incomplete instrumentation.

Cloud services for Trace Sampling

API Gateway REST APIs can establish a tracing decision at the request boundary and pass context to Lambda or other instrumented backends. Compatible instrumentation uses X-Ray sampling rules where supported and exports selected trace data to X-Ray. Lambda contributes tracing data according to its tracing configuration and upstream sampling context; it does not provide arbitrary custom sampling-rule control. CloudWatch metrics and logs supply complementary evidence when a request was not sampled. Together, these services support a workflow in which metrics identify a problem, logs provide additional context, and retained traces reveal timing and dependencies across the affected request path.

AWS X Ray image AWS X-Ray

AWS X-Ray provides centralized sampling rules for compatible instrumentation and receives sampled trace data for analysis. Rules use matching criteria, priorities, reservoir targets, and fixed rates to control collection without changing application code for each policy adjustment.

Open AWS X-Ray →
Amazon API Gateway image Amazon API Gateway

API Gateway REST APIs support active X-Ray tracing and can establish a sampling decision at the API entry point. That decision can propagate to supported downstream integrations, helping preserve a connected request trace.

Open Amazon API Gateway →
AWS Lambda image AWS Lambda

Lambda integrates with X-Ray through active tracing and honors an upstream sampling decision when one is present. Instrument function code to capture application-level operations; custom X-Ray sampling rules do not control Lambda's own sampling behavior.

Open AWS Lambda →
Amazon CloudWatch image Amazon CloudWatch

CloudWatch metrics and logs complement sampled traces with operational signals that need not use the same sampling policy. Use these signals to detect overall error or latency changes, then investigate retained traces and correlated logs for request-level detail.

Open Amazon CloudWatch →