Docs
Plugins
Prometheus

Prometheus

Prometheus is a platform for scraping metrics from services and utilities. You can use @graphql-mesh/plugin-prometheus plugin to expose and collect metrics from all phases of your GraphQL execution including internal query planning and outgoing HTTP requests.

This plugin tracks the complete gateway execution flow and exposes the following metrics:

  • Successful requests (requestCount)
  • Request summary (requestSummary)
  • errors (categorized by phase)
  • resolvers tracing and runtime
  • deprecated fields usage
  • count of graphql operations
  • parse execution time
  • validate execution time
  • contextBuilding execution time
  • execute execution time
  • Latency of outgoing HTTP request
  • Latency of the delegation to the individual sources

Getting Started

npm i @graphql-mesh/plugin-prometheus prom-client

Example Configuration

.meshrc.yaml
# ...
plugins:
  - prometheus:
      # all optional, and by default, all set to false, please opt-in to the metrics you wish to get
 
      # requires `execute` to be true
      requestCount: true
      # requires `execute` to be true
      requestSummary: true
      parse: true
      validate: true
      contextBuilding: true
      execute: true
      errors: true
 
      # reports metrics for the delegation to the individual sources
      delegation: true
 
      # reports metrics for the outgoing HTTP requests
      fetchMetrics: true
      # Adds the request headers to the metrics
      fetchRequestHeaders: true
      # Adds the response headers to the metrics
      fetchResponseHeaders: true
 
      # reports metrics for the incoming HTTP requests (this sets a custom name for http)
      # If you pass a string instead of boolean, it will be used as the name of the metric
      http: my-http-duration-metric
      # Adds the request headers to the metrics
      httpRequestHeaders: true
      # Adds the response headers to the metrics
      httpResponseHeaders: true
 
      # by default all fields are reported
      deprecatedFields: true
      # the path of the endpoint to expose the metrics, default is /metrics
      endpoint: /metrics

Note: Tracing resolvers using resolvers: true might have a performance impact on your GraphQL runtime. Please consider to test it locally first and then decide if it’s needed.

Custom Registry

You can customize the client’s registry by passing a custom registry to the registry option.

myRegistry.ts
// myRegistry.ts
import { Registry } from 'prom-client';
 
export default const myRegistry = new Registry();
.meshrc.yaml
# ...
plugins:
  - prometheus:
      # ...
      registry: ./myRegistry.ts

Caveats

Due to Prometheus client API limitations, if this plugin is initialized multiple times, only the metrics configuration of the first initialization will be applied.

If necessary, use a different registry instance for each plugin instance, or clear the registry before plugin initialization.

function usePrometheusWithRegistry() {
  // create a new registry instance for each plugin instance
  const registry = new Registry()
 
  // or just clear the registry if you use only on plugin instance at a time
  registry.clear()
 
  return usePrometheus({
    registry,
    ...
  })
}

Keep in mind that this implies potential data loss in pull mode if some data is produced between last pull and the re-initialization of the plugin.

Config API Reference

  • requestCount - One of:
    • Boolean
    • String
  • requestTotalDuration - One of:
    • Boolean
    • String
  • requestSummary - One of:
    • Boolean
    • String
  • parse - One of:
    • Boolean
    • String
  • validate - One of:
    • Boolean
    • String
  • contextBuilding - One of:
    • Boolean
    • String
  • execute - One of:
    • Boolean
    • String
  • errors - One of:
    • Boolean
    • String
  • deprecatedFields - One of:
    • Boolean
    • String
  • skipIntrospection (type: Boolean)
  • registry (type: String)
  • delegation - One of:
    • Boolean
    • String
  • delegationArgs (type: Boolean)
  • delegationKey (type: Boolean)
  • subgraphExecute - One of:
    • Boolean
    • String
  • fetchMetrics - One of:
    • Boolean
    • String
  • fetchRequestHeaders (type: Boolean)
  • fetchResponseHeaders (type: Boolean)
  • http - One of:
    • Boolean
    • String
  • httpRequestHeaders (type: Boolean)
  • httpResponseHeaders (type: Boolean)
  • endpoint - - The path to the metrics endpoint default: /metrics One of:
    • Boolean
    • String