-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin/prometheus] Fix duplicated metrics (#3251)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
d318fd9
commit a8ddac5
Showing
7 changed files
with
333 additions
and
42 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.changeset/@graphql-yoga_plugin-prometheus-3251-dependencies.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@graphql-yoga/plugin-prometheus': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@envelop/prometheus@^10.0.0` | ||
↗︎](https://www.npmjs.com/package/@envelop/prometheus/v/10.0.0) (from `^9.4.0`, in | ||
`dependencies`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--- | ||
'@graphql-yoga/plugin-prometheus': major | ||
--- | ||
|
||
Adds a cache for metrics definition (Summary, Histogram and Counter). | ||
|
||
Fixes an issue preventing this plugin to be initialized multiple times, leading to metrics | ||
duplication error (https://github.com/ardatan/graphql-mesh/issues/6545). | ||
|
||
## Behavior Breaking Change: | ||
|
||
Due to Prometheus client API limitations, a metric is only defined once for a given registry. This | ||
means that if the configuration of the metrics, it will be silently ignored on plugin | ||
re-initialization. | ||
|
||
This is to avoid potential loss of metrics data produced between the plugin re-initialization and | ||
the last pull by the prometheus agent. | ||
|
||
If you need to be sure metrics configuration is up to date after a plugin re-initialization, you can | ||
either: | ||
|
||
- restart the whole node process instead of just recreating a graphql server at runtime | ||
- clear the registry using `registry.clear()` before plugin re-initialization: | ||
```ts | ||
function usePrometheusWithReset() { | ||
registry.clear() | ||
return usePrometheus({ ... }) | ||
} | ||
``` | ||
- use a new registry for each plugin instance: | ||
```ts | ||
function usePrometheusWithRegistry() { | ||
const registry = new Registry() | ||
return usePrometheus({ | ||
registry, | ||
... | ||
}) | ||
} | ||
``` | ||
|
||
Keep in mind that this implies potential data loss in pull mode. | ||
|
||
## API Breaking Change: | ||
|
||
To ensure metrics from being registered multiple times on the same registry, the signature of | ||
`createHistogram`, `createSummary` and `createCounter` have been changed to now include the registry | ||
as a mandatory parameter. | ||
|
||
If you were customizing metrics parameters, you will need to update the metric definitions | ||
|
||
```diff | ||
usePrometheus({ | ||
execute: createHistogram({ | ||
+ registry: registry | ||
histogram: new Histogram({ | ||
name: 'my_custom_name', | ||
help: 'HELP ME', | ||
labelNames: ['opText'] as const, | ||
- registers: [registry], | ||
}), | ||
fillLabelsFn: () => {} | ||
}), | ||
requestCount: createCounter({ | ||
+ registry: registry | ||
histogram: new Histogram({ | ||
name: 'my_custom_name', | ||
help: 'HELP ME', | ||
labelNames: ['opText'] as const, | ||
- registers: [registry], | ||
}), | ||
fillLabelsFn: () => {} | ||
}), | ||
requestSummary: createSummary({ | ||
+ registry: registry | ||
histogram: new Histogram({ | ||
name: 'my_custom_name', | ||
help: 'HELP ME', | ||
labelNames: ['opText'] as const, | ||
- registers: [registry], | ||
}), | ||
fillLabelsFn: () => {} | ||
}), | ||
}) | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.