@graphql-yoga/plugin-prometheus 3.1.0 → 3.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/index.js CHANGED
@@ -11,15 +11,20 @@ function usePrometheus(options) {
11
11
  const endpoint = options.endpoint || '/metrics';
12
12
  const registry = options.registry || prom_client_1.register;
13
13
  let httpHistogram;
14
+ function labelExists(label) {
15
+ if (options.labels?.[label] == null) {
16
+ return true;
17
+ }
18
+ return options.labels[label];
19
+ }
14
20
  if (options.http) {
15
- const labelNames = [
16
- 'url',
17
- 'method',
18
- 'statusCode',
19
- 'statusText',
20
- 'operationName',
21
- 'operationType',
22
- ];
21
+ const labelNames = ['url', 'method', 'statusCode', 'statusText'];
22
+ if (labelExists('operationName')) {
23
+ labelNames.push('operationName');
24
+ }
25
+ if (labelExists('operationType')) {
26
+ labelNames.push('operationType');
27
+ }
23
28
  if (options.httpRequestHeaders) {
24
29
  labelNames.push('requestHeaders');
25
30
  }
@@ -44,9 +49,12 @@ function usePrometheus(options) {
44
49
  statusCode: response.status,
45
50
  statusText: response.statusText,
46
51
  };
47
- if (params?.operationType) {
52
+ if (labelExists('operationType') && params.operationType) {
48
53
  labels.operationType = params.operationType;
49
54
  }
55
+ if (labelExists('operationName')) {
56
+ labels.operationName = params.operationName || 'Anonymous';
57
+ }
50
58
  if (options.httpRequestHeaders) {
51
59
  labels.requestHeaders = JSON.stringify(Object.fromEntries(request.headers.entries()));
52
60
  }
@@ -63,17 +71,18 @@ function usePrometheus(options) {
63
71
  onPluginInit({ addPlugin }) {
64
72
  addPlugin((0, prometheus_1.usePrometheus)({ ...options, registry }));
65
73
  },
66
- async onRequest({ request, url, fetchAPI, endResponse }) {
74
+ onRequest({ request, url, fetchAPI, endResponse }) {
67
75
  startByRequest.set(request, Date.now());
68
- if (url.pathname === endpoint) {
69
- const metrics = await registry.metrics();
70
- const response = new fetchAPI.Response(metrics, {
71
- headers: {
72
- 'Content-Type': registry.contentType,
73
- },
76
+ if (endpoint && url.pathname === endpoint) {
77
+ return registry.metrics().then(metrics => {
78
+ endResponse(new fetchAPI.Response(metrics, {
79
+ headers: {
80
+ 'Content-Type': registry.contentType,
81
+ },
82
+ }));
74
83
  });
75
- endResponse(response);
76
84
  }
85
+ return undefined;
77
86
  },
78
87
  onExecute({ args }) {
79
88
  const operationAST = (0, graphql_1.getOperationAST)(args.document, args.operationName);
package/esm/index.js CHANGED
@@ -6,15 +6,20 @@ export function usePrometheus(options) {
6
6
  const endpoint = options.endpoint || '/metrics';
7
7
  const registry = options.registry || defaultRegistry;
8
8
  let httpHistogram;
9
+ function labelExists(label) {
10
+ if (options.labels?.[label] == null) {
11
+ return true;
12
+ }
13
+ return options.labels[label];
14
+ }
9
15
  if (options.http) {
10
- const labelNames = [
11
- 'url',
12
- 'method',
13
- 'statusCode',
14
- 'statusText',
15
- 'operationName',
16
- 'operationType',
17
- ];
16
+ const labelNames = ['url', 'method', 'statusCode', 'statusText'];
17
+ if (labelExists('operationName')) {
18
+ labelNames.push('operationName');
19
+ }
20
+ if (labelExists('operationType')) {
21
+ labelNames.push('operationType');
22
+ }
18
23
  if (options.httpRequestHeaders) {
19
24
  labelNames.push('requestHeaders');
20
25
  }
@@ -39,9 +44,12 @@ export function usePrometheus(options) {
39
44
  statusCode: response.status,
40
45
  statusText: response.statusText,
41
46
  };
42
- if (params?.operationType) {
47
+ if (labelExists('operationType') && params.operationType) {
43
48
  labels.operationType = params.operationType;
44
49
  }
50
+ if (labelExists('operationName')) {
51
+ labels.operationName = params.operationName || 'Anonymous';
52
+ }
45
53
  if (options.httpRequestHeaders) {
46
54
  labels.requestHeaders = JSON.stringify(Object.fromEntries(request.headers.entries()));
47
55
  }
@@ -58,17 +66,18 @@ export function usePrometheus(options) {
58
66
  onPluginInit({ addPlugin }) {
59
67
  addPlugin(useEnvelopPrometheus({ ...options, registry }));
60
68
  },
61
- async onRequest({ request, url, fetchAPI, endResponse }) {
69
+ onRequest({ request, url, fetchAPI, endResponse }) {
62
70
  startByRequest.set(request, Date.now());
63
- if (url.pathname === endpoint) {
64
- const metrics = await registry.metrics();
65
- const response = new fetchAPI.Response(metrics, {
66
- headers: {
67
- 'Content-Type': registry.contentType,
68
- },
71
+ if (endpoint && url.pathname === endpoint) {
72
+ return registry.metrics().then(metrics => {
73
+ endResponse(new fetchAPI.Response(metrics, {
74
+ headers: {
75
+ 'Content-Type': registry.contentType,
76
+ },
77
+ }));
69
78
  });
70
- endResponse(response);
71
79
  }
80
+ return undefined;
72
81
  },
73
82
  onExecute({ args }) {
74
83
  const operationAST = getOperationAST(args.document, args.operationName);
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@graphql-yoga/plugin-prometheus",
3
- "version": "3.1.0",
3
+ "version": "3.1.2",
4
4
  "description": "Prometheus plugin for GraphQL Yoga.",
5
5
  "peerDependencies": {
6
6
  "graphql": "^15.2.0 || ^16.0.0",
7
- "graphql-yoga": "^5.1.0",
7
+ "graphql-yoga": "^5.1.1",
8
8
  "prom-client": "^15.0.0"
9
9
  },
10
10
  "dependencies": {
11
- "@envelop/prometheus": "9.0.0"
11
+ "@envelop/prometheus": "^9.2.0"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
@@ -9,6 +9,6 @@ export interface PrometheusTracingPluginConfig extends EnvelopPrometheusTracingP
9
9
  * The endpoint to serve metrics exposed by this plugin.
10
10
  * Defaults to "/metrics".
11
11
  */
12
- endpoint?: string;
12
+ endpoint?: string | boolean;
13
13
  }
14
14
  export declare function usePrometheus(options: PrometheusTracingPluginConfig): Plugin;
@@ -9,6 +9,6 @@ export interface PrometheusTracingPluginConfig extends EnvelopPrometheusTracingP
9
9
  * The endpoint to serve metrics exposed by this plugin.
10
10
  * Defaults to "/metrics".
11
11
  */
12
- endpoint?: string;
12
+ endpoint?: string | boolean;
13
13
  }
14
14
  export declare function usePrometheus(options: PrometheusTracingPluginConfig): Plugin;