|
| 1 | +--- |
| 2 | +'@graphql-mesh/plugin-prometheus': minor |
| 3 | +--- |
| 4 | + |
| 5 | +Adds a cache for metrics definition (Summary, Histogram and Counter). |
| 6 | + |
| 7 | +Fixes an issue preventing this plugin to be initialized multiple times, leading to metrics |
| 8 | +duplication error (https://github.com/ardatan/graphql-mesh/issues/6545). |
| 9 | + |
| 10 | +## Behavior Breaking Change: |
| 11 | + |
| 12 | +Due to Prometheus client API limitations, a metric is only defined once for a given registry. This |
| 13 | +means that if the configuration of the metrics, it will be silently ignored on plugin |
| 14 | +re-initialization. |
| 15 | + |
| 16 | +This is to avoid potential loss of metrics data produced between the plugin re-initialization and |
| 17 | +the last pull by the prometheus agent. |
| 18 | + |
| 19 | +If you need to be sure metrics configuration is up to date after a plugin re-initialization, you can |
| 20 | +either: |
| 21 | + |
| 22 | +- restart the whole node process instead of just recreating a graphql server at runtime |
| 23 | +- clear the registry using `registry.clear()` before plugin re-initialization: |
| 24 | + ```ts |
| 25 | + function usePrometheusWithReset() { |
| 26 | + registry.clear() |
| 27 | + return usePrometheus({ ... }) |
| 28 | + } |
| 29 | + ``` |
| 30 | +- use a new registry for each plugin instance: |
| 31 | + ```ts |
| 32 | + function usePrometheusWithRegistry() { |
| 33 | + const registry = new Registry() |
| 34 | + return usePrometheus({ |
| 35 | + registry, |
| 36 | + ... |
| 37 | + }) |
| 38 | + } |
| 39 | + ``` |
| 40 | + |
| 41 | +Keep in mind that this implies potential data loss in pull mode. |
| 42 | + |
| 43 | +## New configuration options |
| 44 | + |
| 45 | +Each metrics can now be fully customized. You can use the new factories `createHistogram`, |
| 46 | +`createCounter` and `createSummary` to provide your own configuration for each metrics. |
| 47 | + |
| 48 | +**Example:** |
| 49 | + |
| 50 | +```ts |
| 51 | +import { usePrometheus, createHistogram, createCounter, createSummary } from '@graphql-mesh/plugin-prometheus' |
| 52 | +import { Registry } from 'prom-client' |
| 53 | + |
| 54 | +const registry = new Registry() |
| 55 | + |
| 56 | +usePrometheus({ |
| 57 | + registry, |
| 58 | + parse: createHistogram({ |
| 59 | + registry: registry // make sure to add your custom registry, if you are not using the default one |
| 60 | + histogram: new Histogram({ |
| 61 | + name: 'my_custom_name', |
| 62 | + help: 'HELP ME', |
| 63 | + labelNames: ['opText'] as const, |
| 64 | + }), |
| 65 | + fillLabelsFn: params => { |
| 66 | + // if you wish to fill your `labels` with metadata, you can use the params in order to get access to things like DocumentNode, operationName, operationType, `error` (for error metrics) and `info` (for resolvers metrics) |
| 67 | + return { |
| 68 | + opText: print(params.document) |
| 69 | + } |
| 70 | + } |
| 71 | + }) |
| 72 | +}) |
| 73 | +``` |
0 commit comments