Skip to content

Commit

Permalink
[plugin/prometheus] Fix duplicated metrics (#3251)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
EmrysMyrddin and github-actions[bot] authored May 8, 2024
1 parent d318fd9 commit a8ddac5
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 42 deletions.
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`)
85 changes: 85 additions & 0 deletions .changeset/big-students-grin.md
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: () => {}
}),
})
```

2 changes: 1 addition & 1 deletion packages/plugins/prometheus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"prom-client": "^15.0.0"
},
"dependencies": {
"@envelop/prometheus": "^9.4.0"
"@envelop/prometheus": "^10.0.0"
},
"devDependencies": {
"prom-client": "15.0.0"
Expand Down
21 changes: 16 additions & 5 deletions packages/plugins/prometheus/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { getOperationAST } from 'graphql';
import { Plugin } from 'graphql-yoga';
import { register as defaultRegistry, Histogram } from 'prom-client';
import { register as defaultRegistry } from 'prom-client';
import {
CounterAndLabels,
createCounter,
createHistogram,
createSummary,
PrometheusTracingPluginConfig as EnvelopPrometheusTracingPluginConfig,
FillLabelsFnParams,
HistogramAndLabels,
SummaryAndLabels,
usePrometheus as useEnvelopPrometheus,
} from '@envelop/prometheus';

export { createCounter, createHistogram, createSummary, FillLabelsFnParams };
export {
CounterAndLabels,
FillLabelsFnParams,
HistogramAndLabels,
SummaryAndLabels,
createCounter,
createHistogram,
createSummary,
};

export interface PrometheusTracingPluginConfig extends EnvelopPrometheusTracingPluginConfig {
http?: boolean | string | ReturnType<typeof createHistogram>;
Expand Down Expand Up @@ -46,12 +57,12 @@ export function usePrometheus(options: PrometheusTracingPluginConfig): Plugin {
typeof options.http === 'object'
? options.http
: createHistogram({
histogram: new Histogram({
registry,
histogram: {
name: typeof options.http === 'string' ? options.http : 'graphql_yoga_http_duration',
help: 'Time spent on HTTP connection',
labelNames,
registers: [registry],
}),
},
fillLabelsFn(params, { request, response }) {
const labels: Record<string, string> = {
method: request.method,
Expand Down
11 changes: 11 additions & 0 deletions packages/plugins/prometheus/tests/prometheus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,15 @@ describe('Prometheus', () => {
expect(metrics).toContain('method="POST"');
expect(metrics).toContain('statusCode="200"');
});

it('should be able to register the same histogram for multiple different registries', async () => {
usePrometheus({
http: true,
registry,
});
usePrometheus({
http: true,
registry,
});
});
});
Loading

0 comments on commit a8ddac5

Please sign in to comment.