Custom Metric Attributes

We provide default attributes for every metric, but it’s often necessary to add extra context. To achieve this, we support both static and dynamic values based on headers and request context.

Available since Router version 0.126.0.

Please note that each attribute increases cardinality. Furthermore, attributes based on request header values may potentially leak sensitive information when stored on the telemetry storage provider. You should only attach headers that have been sanitized by your infrastructure.

In certain scenarios, it may be necessary to add additional attributes to your metrics. A common example is including details from a request header or adding context information, such as the services involved in a GraphQL request. This can be easily accomplished by modifying a few lines of configuration.

Prometheus

All attributes are also added to the Prometheus metrics. Since Prometheus does not support multi-value labels like graphql_error_codes, a separate metric series is emitted for each attribute value. This simplifies querying the data in Prometheus.

Static Attribute Value

telemetry: 
  metrics: 
    attributes:
      - key: "myAttribute"
        default: "router"

This configuration will add the attribute myAttribute=router to every metric export.

Dynamic Attribute Value

We also support setting attributes based on client request headers or request context information. To configure this, specify the mapping in the value_from property.

Headers are pre-evaluated before metrics are emitted, while request context fields are determined at different stages. Consequently, request context fields may not be included in all metrics.

cors:
  allow_headers:
    - "X-My-Header"

telemetry:
  metrics:
    attributes:
      - key: "myAttribute"
        default: "myValue"
        value_from:
          request_header: "X-My-Header"
        
      - key: "error_ocdes"
        value_from:
          context_field: graphql_error_codes

This will add the attribute myAttribute=myValue unless the client provides the request header X-My-Header. Ensure that this header is included in the allowlist in the CORS configuration to prevent CORS issues on the frontend.

Additionally, the unique list of subgraph error codes will be appended to all emitted metric values. This enables correlation between error cases and corresponding metrics.

For a complete list of available options, please refer to the relevant documentation section.

Last updated