Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

OpenTelemetry Tracing

Enable distributed tracing for end-to-end visibility across your system.

Setting Up Tracing

use grpc_graphql_gateway::{Gateway, TracingConfig, init_tracer, shutdown_tracer};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize the tracer
    let config = TracingConfig::new()
        .with_service_name("my-gateway")
        .with_sample_ratio(1.0);  // Sample all requests

    let _provider = init_tracer(&config);

    let gateway = Gateway::builder()
        .with_descriptor_set_bytes(DESCRIPTORS)
        .enable_tracing()
        .build()?;

    gateway.serve("0.0.0.0:8888").await?;

    // Shutdown on exit
    shutdown_tracer();
    Ok(())
}

Spans Created

SpanKindDescription
graphql.queryServerGraphQL query operation
graphql.mutationServerGraphQL mutation operation
grpc.callClientgRPC backend call

Span Attributes

GraphQL Spans

AttributeDescription
graphql.operation.nameThe operation name if provided
graphql.operation.typequery, mutation, or subscription
graphql.documentThe GraphQL query (truncated)

gRPC Spans

AttributeDescription
rpc.servicegRPC service name
rpc.methodgRPC method name
rpc.grpc.status_codegRPC status code

OTLP Export

Enable OTLP export by adding the feature:

[dependencies]
grpc_graphql_gateway = { version = "0.2", features = ["otlp"] }

Then configure the exporter:

use grpc_graphql_gateway::TracingConfig;

let config = TracingConfig::new()
    .with_service_name("my-gateway")
    .with_otlp_endpoint("http://jaeger:4317");

Jaeger Integration

Run Jaeger locally:

docker run -d --name jaeger \
  -p 4317:4317 \
  -p 16686:16686 \
  jaegertracing/jaeger:1.47

View traces at: http://localhost:16686

Sampling Configuration

Sample RatioDescription
1.0Sample all requests (dev)
0.1Sample 10% (staging)
0.01Sample 1% (production)
TracingConfig::new()
    .with_sample_ratio(0.1)  // 10% sampling

Context Propagation

The gateway automatically propagates trace context:

  • Incoming HTTP headers (traceparent, tracestate)
  • Outgoing gRPC metadata

Enable Header Propagation for distributed tracing headers.