Skip to content

Commit 532c935

Browse files
fix prometheus duplicated metrics (#6911)
* fix prometheus duplicated metrics * changeset * chore(dependencies): updated changesets for modified dependencies * remove local linked dependency * fix type import * chore(dependencies): updated changesets for modified dependencies * better options * update changeset * update documentation * better changeset * export metrics customization functions and types * use stable version of the yoga plugin * update lockfile * chore(dependencies): updated changesets for modified dependencies --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 8ee8d40 commit 532c935

File tree

10 files changed

+422
-166
lines changed

10 files changed

+422
-166
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-mesh/plugin-prometheus": patch
3+
---
4+
dependencies updates:
5+
- Updated dependency [`@graphql-yoga/plugin-prometheus@^5.0.0` ↗︎](https://www.npmjs.com/package/@graphql-yoga/plugin-prometheus/v/5.0.0) (from `^4.1.0`, in `dependencies`)

.changeset/beige-pillows-move.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
```

.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/.changeset/*.md
21
/website/src/generated-markdown/
32
/website/src/pages/docs/api/
43
.next/

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"editor.tabSize": 2,
77
"editor.insertSpaces": true,
8-
"editor.rulers": [80],
8+
"editor.rulers": [100],
99
"editor.wordWrapColumn": 110,
1010
"prettier.semi": true,
1111
"files.trimTrailingWhitespace": true,

packages/plugins/prometheus/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"dependencies": {
4343
"@graphql-mesh/serve-runtime": "^0.3.4",
44-
"@graphql-yoga/plugin-prometheus": "^4.1.0"
44+
"@graphql-yoga/plugin-prometheus": "^5.0.0"
4545
},
4646
"devDependencies": {
4747
"prom-client": "15.1.2"

packages/plugins/prometheus/src/createHistogramForEnvelop.ts

-37
This file was deleted.

0 commit comments

Comments
 (0)