← All posts
opentelemetry tracing gcp cloud-run quarkus learning

Who Decided Not to Trace That? OpenTelemetry Sampling on Cloud Run

My API sent traces for health checks on every call and for almost none of the real requests. The cause was a parent-based sampler obeying a decision Cloud Run made before my code ever ran.

· 9 min read

I deployed a small Java API to Cloud Run with OpenTelemetry tracing switched on and the sample rate set to 1.0. Every request should have produced a trace.

Then I sent 55 requests and found one trace in the tracing backend.

The health checks, meanwhile, were all there. Every liveness and readiness probe had its own trace. The tracing was working, just for the wrong traffic.

This post covers how OpenTelemetry decides what to keep, who else gets a say in that decision, and the two small mistakes that produced exactly the opposite of what I wanted.

Traces, spans and the sampling decision

A trace is one request’s journey through a system. It has a 128-bit trace ID. It is made of spans, one per unit of work: the HTTP request, the service method it calls, and the database read inside that.

A sampler decides, when a span starts, whether to record it and export it or to drop it. That decision is made once, at the root span, and every child span inherits it. A trace is kept whole or dropped whole. Half a trace is worse than none.

Between services, the decision travels in the W3C traceparent header:

traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
             |  |                                |                |
             |  trace ID                         parent span ID   flags
             version                                              01 = sampled
                                                                  00 = not sampled

The service receiving the request reads that header, continues the same trace ID, and treats the caller’s span as its remote parent.

The three samplers that matter

OpenTelemetry ships a handful of samplers. Three are enough to understand this problem.

The parent-based sampler is a small decision table:

SituationDecision
No parent: this span starts a new traceAsk the root sampler
Remote parent, flag 01Keep
Remote parent, flag 00Drop
Local parent, sampledKeep
Local parent, not sampledDrop

A remote parent came in on a traceparent header. A local parent is a span in the same process, such as the HTTP span that is parent to a service method span.

The important line is the third one. With parentbased(traceidratio(1.0)), the 1.0 is only used when nobody upstream has decided. If a caller says “not sampled”, the trace is dropped, and the rate you configured never gets a vote.

That is a sensible default. If an upstream service decided to keep a trace, you want its downstream spans too. It only goes wrong when the upstream decision is not one you chose.

Cloud Run is an upstream

My client sent no traceparent. I assumed that made my service the root.

It did not. Cloud Run’s serving layer adds trace headers to every request before it reaches the container:

traceparent: 00-<trace-id>-<span-id>-01   (or -00)
X-Cloud-Trace-Context: <trace-id>/<span-id>;o=1   (or o=0)

These headers exist for Google’s own Cloud Trace integration. To keep that cheap, Cloud Run marks only a small number of requests as sampled: about 0.1 requests per second per container instance. That is a rate limit, not a percentage. Each instance marks roughly one request every ten seconds, however much traffic it gets.

So from the application’s point of view, almost every request arrived with a remote parent that said 00. The parent-based sampler obeyed. My burst of 55 requests landed in about six seconds on one instance, and Cloud Run marked one of them. That was my one trace.

This also explains why the problem hides at low traffic. One request every thirty seconds would be sampled almost every time. It shows up when load testing, which is exactly when you want the traces.

How I confirmed it

Cloud Run’s request logs record that decision. Each entry in the run.googleapis.com/requests log has a traceSampled field and a trace field:

gcloud logging read '
  resource.type="cloud_run_revision"
  AND resource.labels.service_name="orders-api"
  AND log_name="projects/my-project/logs/run.googleapis.com%2Frequests"
  AND timestamp>="2026-11-24T23:45:00Z"' \
  --format='value(timestamp,httpRequest.requestUrl,httpRequest.status,traceSampled,trace)'

One of my 55 requests had traceSampled=True, and the rest were blank. The trace ID in its trace field was the same trace ID as the single trace in the backend. Cloud Run’s choice and my backend’s contents lined up exactly.

Two notes from doing this:

The second bug: the health checks

That explained the missing traces. It did not explain why health checks were traced every time.

Health probes come from inside Cloud Run with no traceparent, so they start their own traces. That sends them to the root sampler, traceidratio(1.0), which keeps everything.

The framework was meant to stop that. Quarkus has a setting for it:

quarkus.otel.traces.suppress-application-uris=/health,/health/live,/health/ready,/health/started

To understand why it did nothing, I had to understand how the sampler gets built.

The sampler is built in layers

With the OpenTelemetry SDK’s autoconfigure module, the final sampler goes through a chain. Each step receives the sampler built by the step before:

  1. Autoconfigure builds a sampler from otel.traces.sampler and otel.traces.sampler.arg.
  2. Quarkus wraps it in a DropTargetsSampler, which drops the suppressed URLs and delegates everything else.
  3. Your own AutoConfigurationCustomizerProvider, loaded as a service provider extension, runs after the framework’s customizers.

The original code did this in step 3:

customizer.addSamplerCustomizer(
        (existing, props) -> Sampler.parentBased(Sampler.traceIdRatioBased(sampleRate)));

The lambda receives the sampler built so far, existing, and ignores it. It returns a brand-new sampler, and the health check filter it threw away was inside existing.

It is an easy line to write. It reads like “use this sampler”. What it actually says is “replace whatever everyone else configured”.

The fix

Two changes, both small.

Stop following the parent. Ask autoconfigure for a plain ratio sampler:

customizer.addPropertiesCustomizer(props -> Map.of(
        "otel.traces.sampler", "traceidratio",
        "otel.traces.sampler.arg", Double.toString(sampleRate)));

Delete the sampler customizer. With the property set, autoconfigure builds the right sampler and the framework wraps it. The final sampler becomes DropTargetsSampler(traceidratio(rate)): health checks are dropped, and every other request is kept at the configured rate.

After deploying, the same 55 requests produced every expected span: one HTTP span, one service span and one database span per read for each request. The health check spans stopped as soon as the new revision took traffic.

What it costs

traceidratio ignores the incoming sampled flag in both directions. That has a trade-off:

If you need both, the SDK lets you set each case of the parent-based table separately:

Sampler root = Sampler.traceIdRatioBased(sampleRate);
Sampler sampler = Sampler.parentBasedBuilder(root)
        .setRemoteParentNotSampled(root)
        .build();

That keeps traces the caller asked for and makes your own decision when the caller said no. For me the plain ratio sampler was enough.

Testing a sampler without deploying

Both bugs can be caught in a unit test. Build the SDK through autoconfigure, apply your customizer, and ask the sampler directly.

To catch the first bug, give it a remote parent that says “not sampled”:

Context remoteNotSampled = Context.root().with(Span.wrap(SpanContext.createFromRemoteParent(
        traceId, "00f067aa0ba902b7", TraceFlags.getDefault(), TraceState.getDefault())));

SamplingDecision decision = sampler
        .shouldSample(remoteNotSampled, traceId, "GET /orders/{id}", SpanKind.SERVER,
                Attributes.empty(), List.of())
        .getDecision();

assertThat(decision).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);

To catch the second bug, register a sampler customizer before yours that wraps the sampler, as the framework does. Then assert that the final sampler is still that wrapper. If your code replaces it, the test fails.

The test I had before only checked that the app started with tracing on and that a request was exported. It passed, because locally nothing sat in front of the app adding a traceparent. With no parent, the root sampler kept everything. The test environment was missing exactly the component that caused the bug.

Where the health checks went

Once health checks stopped producing traces, I had to work out where to look when I want to know they are passing. Probe results are in Cloud Run’s system log, not the request log:

log_name="projects/my-project/logs/run.googleapis.com%2Fvarlog%2Fsystem"
textPayload:"probe"

Successes are logged once per revision. A liveness probe logs only its first success. Failures are always logged, at ERROR, for example STARTUP HTTP probe failed 10 times consecutively ... The instance was not started. Filtering on severity>=WARNING shows only the problems.

Traces are for requests that do work. Probes have their own signal already.

One more thing that confused me

Partway through, I searched the last three hours in the tracing backend and found far fewer spans than I expected. That was a second, unrelated effect.

Many tracing backends separate ingested spans from indexed spans. Everything sent is ingested and briefly searchable in a live view. Only a retained subset is kept for historical search. A historical query shows that subset, not everything that arrived. When counting spans to check a sampler, use the live view and a short window.

What I want to remember

The sampler you configure is not the only one with a vote. A parent-based sampler hands the decision to whoever sent the traceparent, and on a managed platform that may be the platform itself.

parentbased(traceidratio(1.0)) does not mean “trace everything”. It means “trace everything that nobody upstream already declined”.

Sampler customizers are a chain. Wrap the sampler you are handed. Don’t replace it unless you mean to drop everything the framework put there.

When a trace count looks wrong, check the incoming request’s sampled flag before you check the exporter. The platform’s request logs often record it.

← Back to all posts