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

Prometheus Metrics

Enable a /metrics endpoint exposing Prometheus-compatible metrics.

Enabling Metrics

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

Available Metrics

MetricTypeLabelsDescription
graphql_requests_totalCounteroperation_typeTotal GraphQL requests
graphql_request_duration_secondsHistogramoperation_typeRequest latency
graphql_errors_totalCountererror_typeTotal GraphQL errors
grpc_backend_requests_totalCounterservice, methodgRPC backend calls
grpc_backend_duration_secondsHistogramservice, methodgRPC latency

Prometheus Scrape Configuration

scrape_configs:
  - job_name: 'graphql-gateway'
    static_configs:
      - targets: ['gateway:8888']
    metrics_path: '/metrics'
    scrape_interval: 15s

Example Metrics Output

# HELP graphql_requests_total Total number of GraphQL requests
# TYPE graphql_requests_total counter
graphql_requests_total{operation_type="query"} 1523
graphql_requests_total{operation_type="mutation"} 234
graphql_requests_total{operation_type="subscription"} 56

# HELP graphql_request_duration_seconds Request duration in seconds
# TYPE graphql_request_duration_seconds histogram
graphql_request_duration_seconds_bucket{operation_type="query",le="0.01"} 1200
graphql_request_duration_seconds_bucket{operation_type="query",le="0.05"} 1480
graphql_request_duration_seconds_bucket{operation_type="query",le="0.1"} 1510
graphql_request_duration_seconds_bucket{operation_type="query",le="+Inf"} 1523

# HELP grpc_backend_requests_total Total gRPC backend calls
# TYPE grpc_backend_requests_total counter
grpc_backend_requests_total{service="UserService",method="GetUser"} 892
grpc_backend_requests_total{service="ProductService",method="GetProduct"} 631

Grafana Dashboard

Create dashboards for:

  • Request rate and latency percentiles
  • Error rates by type
  • gRPC backend health
  • Operation type distribution

Example Queries

Request Rate:

rate(graphql_requests_total[5m])

P99 Latency:

histogram_quantile(0.99, rate(graphql_request_duration_seconds_bucket[5m]))

Error Rate:

rate(graphql_errors_total[5m]) / rate(graphql_requests_total[5m])

Programmatic Access

Use the metrics API directly:

use grpc_graphql_gateway::{GatewayMetrics, RequestTimer};

// Record custom metrics
let timer = GatewayMetrics::global().start_request_timer("query");
// ... process request
timer.observe_duration();

// Record gRPC calls
let grpc_timer = GatewayMetrics::global().start_grpc_timer("UserService", "GetUser");
// ... make gRPC call
grpc_timer.observe_duration();