A nexus-framework plugin for reporting basic GraphQL metrics through Prometheus (via prom-client)
This nexus-framework plugin collects basics GraphQL metrics and reports them to a Prometheus server. The plugin utilizes the prom-client library to communicate with Prometheus and thus requires Nexus' instance of express to be provided to the plugin.
⚠️ This plugin does NOT support Nexus projects utilizing serverless Next.JS implementations!
This module is distributed via npm which is bundled with Node.js.
npm install --save nexus-plugin-prometheus
OR
yarn add nexus-plugin-prometheus
Find a full example in the examples/basic-prometheus folder, which also includes a basic Grafana Dashboard and docker-compose file to fully illustrate the plugin in use.
Due to the middleware architecture of Nexus-framework plugins, it is vital that one use
the nexus-plugin-prometheus first, so that no requests are culled by another plugin.
// app.ts
import { use, server } from 'nexus'
import { prometheus } from 'nexus-plugin-prometheus'
// Enables the Prometheus reporting plugin
use(
prometheus({
express: server.express, // inject Nexus express instance
})
)
Prometheus will now be able to collect metrics from the servers /metrics
url.
Executing prom-client's collectDefaultMetrics
method can be done by providing the plugin with a custom configureRegistry
method.
For more information on configuring prom-client, see it's usage documentation.
// app.ts
import { use, server } from 'nexus'
import { prometheus } from 'nexus-plugin-prometheus'
import { Registry, collectDefaultMetrics } from 'prom-client'
// Define custom configuration of prom-client
function configureRegistry(): Registry[] {
const register = new Registry()
collectDefaultMetrics({ register })
return [register]
}
// Enables the Prometheus reporting plugin
use(
prometheus({
express: server.express,
configureRegistry, // provide custom
})
)
Executing prom-client's collectDefaultMetrics
method with any additional configuration, such as a prefix
, can be done by providing the plugin with a custom configureRegistry
method.
For more information on configuring prom-client, see it's usage documentation.
// app.ts
import { use, server } from 'nexus'
import { prometheus } from 'nexus-plugin-prometheus'
import { Registry, collectDefaultMetrics } from 'prom-client'
// Define custom configuration of prom-client
function configureRegistry(): Registry[] {
const register = new Registry()
const prefix = 'my_application_'
collectDefaultMetrics({ register, prefix })
return [register]
}
// Enables the Prometheus reporting plugin
use(
prometheus({
express: server.express,
configureRegistry, // provide custom
})
)
Please read CONTRIBUTING.md
Authored and maintained by Hays Clark.
To all contributors - Thank you!
Open-source under MIT License.
- Uh oh, something went wrong!
- I wish something was different…
- Can I contribute code?
- My question isn't answered :(
Sorry about that. It is recommended that you enable "debug" and "allowExternalErrors" in your plugin. Additionally, if you are using other Nexus plugins, you may want to do the same for them.
// app.ts
import { use, server } from 'nexus'
import { prometheus } from 'nexus-plugin-prometheus'
// Enables the Prometheus reporting plugin
use(
prometheus({
express: server.express,
debug: true,
allowExternalErrors: true,
})
)
If you are still stuck, please submit a bug report using the GitHub issue tracker.
Keen to hear all ideas! Create an enhancement request using the GitHub issue tracker.
Yes please! See DEVELOPING.md.
Ask away using the GitHub issue tracker.