Skip to main content

Trace Requests

A trace follows one request through the gateway. Each step is a span. The router and the policy engine pass the trace from one to the next with the W3C traceparent and tracestate headers.

The demonstration stack is an OpenTelemetry Collector and Jaeger. The gateway sends spans to the collector on gRPC port 4317 or HTTP port 4318, and the collector forwards them to Jaeger. You can send the same spans to any OTLP backend instead.

Prerequisites​

Enable Tracing​

Add the tracing settings to configs/config.toml:

[tracing]
enabled = true # Set to true to enable tracing
endpoint = "otel-collector:4317" # OTLP collector gRPC endpoint
insecure = true # Plaintext connection to the collector
service_version = "1.0.0" # Service version
batch_timeout = "1s" # Batch timeout for exporting spans
max_export_batch_size = 512 # Maximum spans per batch
sampling_rate = 1.0 # Sample rate (1.0 = 100%, 0.5 = 50%)

The gateway reads config.toml only when it starts. Start the tracing services and recreate the gateway containers so that tracing turns on:

docker compose --profile tracing up -d --force-recreate

This starts the OpenTelemetry Collector and Jaeger, and recreates gateway-controller and gateway-runtime with tracing on.

To run the gateway without that stack:

docker compose up -d
note

If tracing is enabled and the collector is not running, the components log export warnings. Set enabled = false to turn tracing off. Jaeger keeps traces in memory, so stopping it drops them unless you add a storage backend.

To stop only the tracing services, leave the gateway running:

docker compose --profile tracing stop otel-collector jaeger
warning

docker compose --profile tracing down stops and removes every container in the project, including the gateway.

View Traces in Jaeger​

  1. Open http://localhost:16686.
  2. Send a request through the gateway so that a trace exists.
  3. Select a service, such as router.
  4. Optionally select an operation, or leave it as all.
  5. Set the lookback (5m, 15m, 1h, 6h, 12h, 1d, 2d, or custom). The default is the last hour.
  6. Optionally filter by tags (for example http.status_code=500), duration, or result limit. The default limit is 20.
  7. Click Find Traces.

Open a trace to see the timeline, how long each span took, and the parent and child spans. Open a span for its operation name, duration, tags, logs, and process.

  • Slow requests: set a minimum duration, such as 1000ms, then click Find Traces.
  • Errors: filter with error=true or http.status_code=500.
  • One request: search by the trace ID from a traceparent header.

Select traces and click Compare Traces to compare them. Click Dependencies to see which services called which.

Change What Is Exported​

Sample fewer requests:

[tracing]
sampling_rate = 0.1 # Sample 10% of requests

1.0 samples every request. 0.5 samples half, 0.1 samples 10%, and 0.01 samples 1%. A lower rate costs less and can miss traces.

Set the service name that the router reports:

[router]
tracing_service_name = "router-prod-us-east-1"

Change how spans are batched:

[tracing]
batch_timeout = "5s" # Wait up to 5s before exporting
max_export_batch_size = 1024 # Export up to 1024 spans per batch

A shorter timeout shows traces sooner. A longer timeout sends fewer, larger batches.

Send Traces Somewhere Else​

Keep the gateway endpoint pointed at an OTLP collector, and change the collector exporters. The collector file is observability/otel-collector/config.yaml. Several exporters below, such as awsxray, googlecloud, azuremonitor, and datadog, exist only in the OpenTelemetry Collector Contrib image. The bundled collector image does not include them, so switch the otel-collector image to the Contrib image first. Tempo and the Datadog Agent also publish port 4317, the same host port as the bundled collector. Stop the bundled collector first, or change one host port.

Moesif​

exporters:
# Export to Moesif
otlphttp:
endpoint: https://api.moesif.net/v1/traces
headers:
X-Moesif-Application-Id: 'your-moesif-application-id'

service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, batch, resource]
exporters: [otlphttp] # Send to Moesif

Use the otlphttp exporter and HTTPS. The X-Moesif-Application-Id header is required.

To get the application ID, sign in at moesif.com, open Settings → Installation or API Keys, and copy the Collector Application ID.

To pass the ID as an environment variable:

exporters:
otlphttp:
endpoint: https://api.moesif.net/v1/traces
headers:
X-Moesif-Application-Id: '${MOESIF_APPLICATION_ID}'
otel-collector:
image: otel/opentelemetry-collector:latest
environment:
- MOESIF_APPLICATION_ID=${MOESIF_APPLICATION_ID}
# ... rest of configuration
export MOESIF_APPLICATION_ID=your-moesif-application-id
docker compose --profile tracing up -d

To send to Jaeger and Moesif together:

exporters:
# Local Jaeger for development
otlp/jaeger:
endpoint: jaeger:4317
tls:
insecure: true

# Moesif for analytics
otlphttp/moesif:
endpoint: https://api.moesif.net/v1/traces
headers:
X-Moesif-Application-Id: '${MOESIF_APPLICATION_ID}'

service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, batch, resource]
exporters: [otlp/jaeger, otlphttp/moesif]

Zipkin​

Replace Jaeger with Zipkin:

zipkin:
image: openzipkin/zipkin:latest
ports:
- "9411:9411"
networks:
- gateway-network

Update the collector configuration to export to Zipkin:

exporters:
zipkin:
endpoint: http://zipkin:9411/api/v2/spans

Open Zipkin at http://localhost:9411.

Grafana Tempo​

tempo:
image: grafana/tempo:latest
command: ["-config.file=/etc/tempo.yaml"]
volumes:
- ./observability/tempo/tempo.yaml:/etc/tempo.yaml
- tempo-data:/tmp/tempo
ports:
- "3200:3200" # Tempo HTTP
- "4317:4317" # OTLP gRPC
networks:
- gateway-network

Point the gateway at Tempo:

[tracing]
endpoint = "tempo:4317"

AWS X-Ray​

exporters:
awsxray:
region: us-east-1
no_verify_ssl: false

Or use the AWS Distro for OpenTelemetry collector:

otel-collector:
image: public.ecr.aws/aws-observability/aws-otel-collector:latest
command: ["--config=/etc/otel-collector-config.yaml"]
environment:
- AWS_REGION=us-east-1

Google Cloud Trace​

exporters:
googlecloud:
project: your-gcp-project-id
use_insecure: false

Azure Monitor​

exporters:
azuremonitor:
instrumentation_key: "your-instrumentation-key"

Datadog​

Configure the collector to export to Datadog:

exporters:
datadog:
api:
key: ${DD_API_KEY}
site: datadoghq.com

Or use the Datadog Agent:

datadog-agent:
image: datadog/agent:latest
environment:
- DD_API_KEY=${DD_API_KEY}
- DD_APM_ENABLED=true
- DD_APM_NON_LOCAL_TRAFFIC=true
- DD_OTLP_CONFIG_RECEIVER_PROTOCOLS_GRPC_ENDPOINT=0.0.0.0:4317
ports:
- "4317:4317"
networks:
- gateway-network
[tracing]
endpoint = "datadog-agent:4317"

New Relic​

exporters:
otlphttp:
endpoint: https://otlp.nr-data.net:4317
headers:
api-key: ${NEW_RELIC_LICENSE_KEY}

Honeycomb​

exporters:
otlp:
endpoint: api.honeycomb.io:443
headers:
x-honeycomb-team: ${HONEYCOMB_API_KEY}

Lightstep​

exporters:
otlp:
endpoint: ingest.lightstep.com:443
headers:
lightstep-access-token: ${LIGHTSTEP_ACCESS_TOKEN}

Collector Configuration​

observability/otel-collector/config.yaml has receivers, processors, exporters, and a pipeline that connects them.

Receive OTLP:

receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318

Process spans:

processors:
# Batch spans for efficiency
batch:
timeout: 1s
send_batch_size: 1024

# Memory limiter to prevent out-of-memory errors
memory_limiter:
check_interval: 1s
limit_mib: 512

Export them:

exporters:
# Send to Jaeger
otlp:
endpoint: jaeger:4317
tls:
insecure: true

# Debug output to console
debug:
verbosity: detailed
sampling_initial: 5
sampling_thereafter: 200
service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, batch, resource]
exporters: [otlp, debug]

Send one pipeline to several backends:

exporters:
otlp/jaeger:
endpoint: jaeger:4317
tls:
insecure: true

otlp/tempo:
endpoint: tempo:4317
tls:
insecure: true

datadog:
api:
key: ${DD_API_KEY}

service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, batch]
exporters: [otlp/jaeger, otlp/tempo, datadog]

Keep error traces, slow traces, and a sample of the rest. The tail_sampling processor is available in the OpenTelemetry Collector Contrib distribution:

processors:
tail_sampling:
policies:
- name: error-traces
type: status_code
status_code:
status_codes: [ERROR]
- name: slow-traces
type: latency
latency:
threshold_ms: 1000
- name: probabilistic
type: probabilistic
probabilistic:
sampling_percentage: 10

Pass a Trace Through the Gateway​

If you omit traceparent, the gateway starts a new trace. To continue a trace you already have, send the header with your request:

curl -k https://localhost:8443/<context>/<path> \
-H "API-Key: <API_KEY>" \
-H "traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"

Use the invoke URL and API key from Route LLM Traffic Through a Self-Hosted Gateway. A request that the gateway rejects with 401 still creates a trace.

Choose a Sampling Rate​

Traffic VolumeSampling RateUse Case
Under 100 requests per second100% (1.0)Full visibility, low overhead
100 to 1,000 requests per second10 to 50% (0.1 to 0.5)Balanced visibility and cost
1,000 to 10,000 requests per second1 to 10% (0.01 to 0.1)Cost-effective, statistical sampling
Over 10,000 requests per second0.1 to 1% (0.001 to 0.01)Minimal overhead

Use tail sampling if you want every error trace kept while ordinary traffic is sampled. In production, use TLS on the collector connection and a memory limit on the collector.

Troubleshooting​

No Traces in Jaeger​

  1. Confirm that tracing is on. In configs/config.toml, enabled under [tracing] must be true.

    grep -A8 "\[tracing\]" configs/config.toml
  2. Confirm that the collector and Jaeger are running:

    docker compose --profile tracing ps
    curl http://localhost:16686/
  3. Read the collector and gateway logs:

    docker compose logs otel-collector
    docker compose logs gateway-runtime | grep -i trace
    docker compose logs gateway-controller | grep -i trace
  4. Check that the containers can reach each other:

    docker compose exec gateway-runtime getent hosts otel-collector

If spans are missing, the sampling rate may be too low, a component may not be exporting, or the traceparent header may have been dropped. Clock skew across containers shows up as wrong span times.

Export Uses Too Much CPU or Network​

Lower the sampling rate and export larger batches less often:

[tracing]
sampling_rate = 0.1
batch_timeout = "5s"
max_export_batch_size = 2048

Jaeger Does Not Open​

docker compose logs jaeger
curl http://localhost:16686/

Port 16686 must be reachable on the host.

Turn Tracing Off​

In configs/config.toml:

[tracing]
enabled = false

Then recreate the containers:

docker compose up -d --force-recreate gateway-controller gateway-runtime

Additional Resources​

Was this page helpful?