@envelop/prometheus 9.4.0 → 10.0.0-alpha-20240429133416-6ed19f38

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/README.md CHANGED
@@ -106,14 +106,14 @@ const getEnveloped = envelop({
106
106
  usePrometheus({
107
107
  // all optional, and by default, all set to false, please opt-in to the metrics you wish to get
108
108
  parse: createHistogram({
109
+ registry: registry // make sure to add your custom registry, if you are not using the default one
109
110
  histogram: new Histogram({
110
111
  name: 'my_custom_name',
111
112
  help: 'HELP ME',
112
113
  labelNames: ['opText'] as const,
113
- registers: [registry] // make sure to add your custom registry, if you are not using the default one
114
114
  }),
115
115
  fillLabelsFn: params => {
116
- // if you wish to fill your `lables` 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)
116
+ // 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)
117
117
  return {
118
118
  opText: print(params.document)
119
119
  }
package/cjs/index.js CHANGED
@@ -14,6 +14,7 @@ exports.fillLabelsFnParamsMap = new WeakMap();
14
14
  exports.execStartTimeMap = new WeakMap();
15
15
  const usePrometheus = (config = {}) => {
16
16
  let typeInfo = null;
17
+ const registry = (0, utils_js_1.instrumentRegistry)(config.registry || prom_client_1.register);
17
18
  const parseHistogram = (0, utils_js_1.getHistogramFromConfig)(config, 'parse', typeof config.parse === 'string' ? config.parse : 'graphql_envelop_phase_parse', 'Time spent on running GraphQL "parse" function');
18
19
  const validateHistogram = (0, utils_js_1.getHistogramFromConfig)(config, 'validate', typeof config.validate === 'string' ? config.validate : 'graphql_envelop_phase_validate', 'Time spent on running GraphQL "validate" function');
19
20
  const contextBuildingHistogram = (0, utils_js_1.getHistogramFromConfig)(config, 'contextBuilding', typeof config.contextBuilding === 'string'
@@ -25,7 +26,8 @@ const usePrometheus = (config = {}) => {
25
26
  ? config.resolvers
26
27
  : config.resolvers === true || typeof config.resolvers === 'string'
27
28
  ? (0, utils_js_1.createHistogram)({
28
- histogram: new prom_client_1.Histogram({
29
+ registry,
30
+ histogram: {
29
31
  name: typeof config.resolvers === 'string'
30
32
  ? config.resolvers
31
33
  : 'graphql_envelop_execute_resolver',
@@ -37,8 +39,7 @@ const usePrometheus = (config = {}) => {
37
39
  'typeName',
38
40
  'returnType',
39
41
  ].filter(label => (0, utils_js_1.labelExists)(config, label)),
40
- registers: [config.registry || prom_client_1.register],
41
- }),
42
+ },
42
43
  fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
43
44
  operationName: params.operationName,
44
45
  operationType: params.operationType,
@@ -52,14 +53,14 @@ const usePrometheus = (config = {}) => {
52
53
  ? config.requestTotalDuration
53
54
  : config.requestTotalDuration === true || typeof config.requestTotalDuration === 'string'
54
55
  ? (0, utils_js_1.createHistogram)({
55
- histogram: new prom_client_1.Histogram({
56
+ registry,
57
+ histogram: {
56
58
  name: typeof config.requestTotalDuration === 'string'
57
59
  ? config.requestTotalDuration
58
60
  : 'graphql_envelop_request_duration',
59
61
  help: 'Time spent on running the GraphQL operation from parse to execute',
60
62
  labelNames: ['operationType', 'operationName'].filter(label => (0, utils_js_1.labelExists)(config, label)),
61
- registers: [config.registry || prom_client_1.register],
62
- }),
63
+ },
63
64
  fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
64
65
  operationName: params.operationName,
65
66
  operationType: params.operationType,
@@ -70,14 +71,14 @@ const usePrometheus = (config = {}) => {
70
71
  ? config.requestSummary
71
72
  : config.requestSummary === true || typeof config.requestSummary === 'string'
72
73
  ? (0, utils_js_1.createSummary)({
73
- summary: new prom_client_1.Summary({
74
+ registry,
75
+ summary: {
74
76
  name: typeof config.requestSummary === 'string'
75
77
  ? config.requestSummary
76
78
  : 'graphql_envelop_request_time_summary',
77
79
  help: 'Summary to measure the time to complete GraphQL operations',
78
80
  labelNames: ['operationType', 'operationName'].filter(label => (0, utils_js_1.labelExists)(config, label)),
79
- registers: [config.registry || prom_client_1.register],
80
- }),
81
+ },
81
82
  fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
82
83
  operationName: params.operationName,
83
84
  operationType: params.operationType,
@@ -88,12 +89,12 @@ const usePrometheus = (config = {}) => {
88
89
  ? config.errors
89
90
  : config.errors === true || typeof config.errors === 'string'
90
91
  ? (0, utils_js_1.createCounter)({
91
- counter: new prom_client_1.Counter({
92
+ registry,
93
+ counter: {
92
94
  name: typeof config.errors === 'string' ? config.errors : 'graphql_envelop_error_result',
93
95
  help: 'Counts the amount of errors reported from all phases',
94
96
  labelNames: ['operationType', 'operationName', 'path', 'phase'].filter(label => (0, utils_js_1.labelExists)(config, label)),
95
- registers: [config.registry || prom_client_1.register],
96
- }),
97
+ },
97
98
  fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
98
99
  operationName: params.operationName,
99
100
  operationType: params.operationType,
@@ -106,14 +107,14 @@ const usePrometheus = (config = {}) => {
106
107
  ? config.requestCount
107
108
  : config.requestCount === true || typeof config.requestCount === 'string'
108
109
  ? (0, utils_js_1.createCounter)({
109
- counter: new prom_client_1.Counter({
110
+ registry,
111
+ counter: {
110
112
  name: typeof config.requestCount === 'string'
111
113
  ? config.requestCount
112
114
  : 'graphql_envelop_request',
113
115
  help: 'Counts the amount of GraphQL requests executed through Envelop',
114
116
  labelNames: ['operationType', 'operationName'].filter(label => (0, utils_js_1.labelExists)(config, label)),
115
- registers: [config.registry || prom_client_1.register],
116
- }),
117
+ },
117
118
  fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
118
119
  operationName: params.operationName,
119
120
  operationType: params.operationType,
@@ -124,14 +125,14 @@ const usePrometheus = (config = {}) => {
124
125
  ? config.deprecatedFields
125
126
  : config.deprecatedFields === true || typeof config.deprecatedFields === 'string'
126
127
  ? (0, utils_js_1.createCounter)({
127
- counter: new prom_client_1.Counter({
128
+ registry,
129
+ counter: {
128
130
  name: typeof config.deprecatedFields === 'string'
129
131
  ? config.deprecatedFields
130
132
  : 'graphql_envelop_deprecated_field',
131
133
  help: 'Counts the amount of deprecated fields used in selection sets',
132
134
  labelNames: ['operationType', 'operationName', 'fieldName', 'typeName'].filter(label => (0, utils_js_1.labelExists)(config, label)),
133
- registers: [config.registry || prom_client_1.register],
134
- }),
135
+ },
135
136
  fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
136
137
  operationName: params.operationName,
137
138
  operationType: params.operationType,
@@ -144,13 +145,13 @@ const usePrometheus = (config = {}) => {
144
145
  ? config.schemaChangeCount
145
146
  : config.schemaChangeCount === true || typeof config.schemaChangeCount === 'string'
146
147
  ? (0, utils_js_1.createCounter)({
147
- counter: new prom_client_1.Counter({
148
+ registry,
149
+ counter: {
148
150
  name: typeof config.schemaChangeCount === 'string'
149
151
  ? config.schemaChangeCount
150
152
  : 'graphql_envelop_schema_change',
151
153
  help: 'Counts the amount of schema changes',
152
- registers: [config.registry || prom_client_1.register],
153
- }),
154
+ },
154
155
  fillLabelsFn: () => ({}),
155
156
  })
156
157
  : undefined;
package/cjs/metrics.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";
package/cjs/utils.js CHANGED
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.filterFillParamsFnParams = exports.labelExists = exports.extractDeprecatedFields = exports.getHistogramFromConfig = exports.createCounter = exports.createSummary = exports.createHistogram = exports.createFillLabelFnParams = exports.shouldTraceFieldResolver = void 0;
3
+ exports.instrumentRegistry = exports.filterFillParamsFnParams = exports.labelExists = exports.extractDeprecatedFields = exports.getHistogramFromConfig = exports.createCounter = exports.registerCounter = exports.createSummary = exports.registerSummary = exports.createHistogram = exports.registerHistogram = exports.createFillLabelFnParams = exports.shouldTraceFieldResolver = void 0;
4
4
  const graphql_1 = require("graphql");
5
5
  const prom_client_1 = require("prom-client");
6
+ const histograms = new WeakMap();
7
+ const summaries = new WeakMap();
8
+ const counters = new WeakMap();
6
9
  function shouldTraceFieldResolver(info, whitelist) {
7
10
  if (!whitelist) {
8
11
  return true;
@@ -31,16 +34,62 @@ function createFillLabelFnParams(parseResult, context, filterParams) {
31
34
  });
32
35
  }
33
36
  exports.createFillLabelFnParams = createFillLabelFnParams;
37
+ function registerHistogram(registry, conf) {
38
+ if (!histograms.has(registry)) {
39
+ histograms.set(registry, new Map());
40
+ }
41
+ const registryHistograms = histograms.get(registry);
42
+ if (!registryHistograms.has(conf.name)) {
43
+ conf.registers = [registry];
44
+ registryHistograms.set(conf.name, new prom_client_1.Histogram(conf));
45
+ }
46
+ return registryHistograms.get(conf.name);
47
+ }
48
+ exports.registerHistogram = registerHistogram;
34
49
  function createHistogram(options) {
35
- return options;
50
+ return {
51
+ histogram: registerHistogram(options.registry, options.histogram),
52
+ // histogram: new Histogram(options.histogram),
53
+ fillLabelsFn: options.fillLabelsFn,
54
+ };
36
55
  }
37
56
  exports.createHistogram = createHistogram;
57
+ function registerSummary(registry, conf) {
58
+ if (!summaries.has(registry)) {
59
+ summaries.set(registry, new Map());
60
+ }
61
+ const registrySummaries = summaries.get(registry);
62
+ if (!registrySummaries.has(conf.name)) {
63
+ conf.registers = [registry];
64
+ registrySummaries.set(conf.name, new prom_client_1.Summary(conf));
65
+ }
66
+ return registrySummaries.get(conf.name);
67
+ }
68
+ exports.registerSummary = registerSummary;
38
69
  function createSummary(options) {
39
- return options;
70
+ return {
71
+ summary: registerSummary(options.registry, options.summary),
72
+ fillLabelsFn: options.fillLabelsFn,
73
+ };
40
74
  }
41
75
  exports.createSummary = createSummary;
76
+ function registerCounter(registry, conf) {
77
+ if (!counters.has(registry)) {
78
+ counters.set(registry, new Map());
79
+ }
80
+ const registryCounters = counters.get(registry);
81
+ if (!registryCounters.has(conf.name)) {
82
+ conf.registers = [registry];
83
+ registryCounters.set(conf.name, new prom_client_1.Counter(conf));
84
+ }
85
+ return registryCounters.get(conf.name);
86
+ }
87
+ exports.registerCounter = registerCounter;
42
88
  function createCounter(options) {
43
- return options;
89
+ return {
90
+ counter: registerCounter(options.registry, options.counter),
91
+ fillLabelsFn: options.fillLabelsFn,
92
+ };
44
93
  }
45
94
  exports.createCounter = createCounter;
46
95
  function getHistogramFromConfig(config, phase, name, help) {
@@ -48,12 +97,12 @@ function getHistogramFromConfig(config, phase, name, help) {
48
97
  ? config[phase]
49
98
  : config[phase] === true
50
99
  ? createHistogram({
51
- histogram: new prom_client_1.Histogram({
100
+ registry: config.registry || prom_client_1.register,
101
+ histogram: {
52
102
  name,
53
103
  help,
54
104
  labelNames: ['operationType', 'operationName'].filter(label => labelExists(config, label)),
55
- registers: [config.registry || prom_client_1.register],
56
- }),
105
+ },
57
106
  fillLabelsFn: params => filterFillParamsFnParams(config, {
58
107
  operationName: params.operationName,
59
108
  operationType: params.operationType,
@@ -104,3 +153,17 @@ function filterFillParamsFnParams(config, params) {
104
153
  return Object.fromEntries(Object.entries(params).filter(([key]) => labelExists(config, key)));
105
154
  }
106
155
  exports.filterFillParamsFnParams = filterFillParamsFnParams;
156
+ const clearRegistry = new WeakMap();
157
+ function instrumentRegistry(registry) {
158
+ if (!clearRegistry.has(registry)) {
159
+ clearRegistry.set(registry, registry.clear.bind(registry));
160
+ }
161
+ registry.clear = () => {
162
+ histograms.delete(registry);
163
+ summaries.delete(registry);
164
+ counters.delete(registry);
165
+ clearRegistry.get(registry)();
166
+ };
167
+ return registry;
168
+ }
169
+ exports.instrumentRegistry = instrumentRegistry;
package/esm/index.js CHANGED
@@ -1,14 +1,15 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
2
2
  import { TypeInfo } from 'graphql';
3
- import { Counter, register as defaultRegistry, Histogram, Summary } from 'prom-client';
3
+ import { register as defaultRegistry } from 'prom-client';
4
4
  import { isAsyncIterable, isIntrospectionOperationString, } from '@envelop/core';
5
5
  import { useOnResolve } from '@envelop/on-resolve';
6
- import { createCounter, createFillLabelFnParams, createHistogram, createSummary, extractDeprecatedFields, filterFillParamsFnParams, getHistogramFromConfig, labelExists, shouldTraceFieldResolver, } from './utils.js';
6
+ import { createCounter, createFillLabelFnParams, createHistogram, createSummary, extractDeprecatedFields, filterFillParamsFnParams, getHistogramFromConfig, instrumentRegistry, labelExists, shouldTraceFieldResolver, } from './utils.js';
7
7
  export { createCounter, createHistogram, createSummary, };
8
8
  export const fillLabelsFnParamsMap = new WeakMap();
9
9
  export const execStartTimeMap = new WeakMap();
10
10
  export const usePrometheus = (config = {}) => {
11
11
  let typeInfo = null;
12
+ const registry = instrumentRegistry(config.registry || defaultRegistry);
12
13
  const parseHistogram = getHistogramFromConfig(config, 'parse', typeof config.parse === 'string' ? config.parse : 'graphql_envelop_phase_parse', 'Time spent on running GraphQL "parse" function');
13
14
  const validateHistogram = getHistogramFromConfig(config, 'validate', typeof config.validate === 'string' ? config.validate : 'graphql_envelop_phase_validate', 'Time spent on running GraphQL "validate" function');
14
15
  const contextBuildingHistogram = getHistogramFromConfig(config, 'contextBuilding', typeof config.contextBuilding === 'string'
@@ -20,7 +21,8 @@ export const usePrometheus = (config = {}) => {
20
21
  ? config.resolvers
21
22
  : config.resolvers === true || typeof config.resolvers === 'string'
22
23
  ? createHistogram({
23
- histogram: new Histogram({
24
+ registry,
25
+ histogram: {
24
26
  name: typeof config.resolvers === 'string'
25
27
  ? config.resolvers
26
28
  : 'graphql_envelop_execute_resolver',
@@ -32,8 +34,7 @@ export const usePrometheus = (config = {}) => {
32
34
  'typeName',
33
35
  'returnType',
34
36
  ].filter(label => labelExists(config, label)),
35
- registers: [config.registry || defaultRegistry],
36
- }),
37
+ },
37
38
  fillLabelsFn: params => filterFillParamsFnParams(config, {
38
39
  operationName: params.operationName,
39
40
  operationType: params.operationType,
@@ -47,14 +48,14 @@ export const usePrometheus = (config = {}) => {
47
48
  ? config.requestTotalDuration
48
49
  : config.requestTotalDuration === true || typeof config.requestTotalDuration === 'string'
49
50
  ? createHistogram({
50
- histogram: new Histogram({
51
+ registry,
52
+ histogram: {
51
53
  name: typeof config.requestTotalDuration === 'string'
52
54
  ? config.requestTotalDuration
53
55
  : 'graphql_envelop_request_duration',
54
56
  help: 'Time spent on running the GraphQL operation from parse to execute',
55
57
  labelNames: ['operationType', 'operationName'].filter(label => labelExists(config, label)),
56
- registers: [config.registry || defaultRegistry],
57
- }),
58
+ },
58
59
  fillLabelsFn: params => filterFillParamsFnParams(config, {
59
60
  operationName: params.operationName,
60
61
  operationType: params.operationType,
@@ -65,14 +66,14 @@ export const usePrometheus = (config = {}) => {
65
66
  ? config.requestSummary
66
67
  : config.requestSummary === true || typeof config.requestSummary === 'string'
67
68
  ? createSummary({
68
- summary: new Summary({
69
+ registry,
70
+ summary: {
69
71
  name: typeof config.requestSummary === 'string'
70
72
  ? config.requestSummary
71
73
  : 'graphql_envelop_request_time_summary',
72
74
  help: 'Summary to measure the time to complete GraphQL operations',
73
75
  labelNames: ['operationType', 'operationName'].filter(label => labelExists(config, label)),
74
- registers: [config.registry || defaultRegistry],
75
- }),
76
+ },
76
77
  fillLabelsFn: params => filterFillParamsFnParams(config, {
77
78
  operationName: params.operationName,
78
79
  operationType: params.operationType,
@@ -83,12 +84,12 @@ export const usePrometheus = (config = {}) => {
83
84
  ? config.errors
84
85
  : config.errors === true || typeof config.errors === 'string'
85
86
  ? createCounter({
86
- counter: new Counter({
87
+ registry,
88
+ counter: {
87
89
  name: typeof config.errors === 'string' ? config.errors : 'graphql_envelop_error_result',
88
90
  help: 'Counts the amount of errors reported from all phases',
89
91
  labelNames: ['operationType', 'operationName', 'path', 'phase'].filter(label => labelExists(config, label)),
90
- registers: [config.registry || defaultRegistry],
91
- }),
92
+ },
92
93
  fillLabelsFn: params => filterFillParamsFnParams(config, {
93
94
  operationName: params.operationName,
94
95
  operationType: params.operationType,
@@ -101,14 +102,14 @@ export const usePrometheus = (config = {}) => {
101
102
  ? config.requestCount
102
103
  : config.requestCount === true || typeof config.requestCount === 'string'
103
104
  ? createCounter({
104
- counter: new Counter({
105
+ registry,
106
+ counter: {
105
107
  name: typeof config.requestCount === 'string'
106
108
  ? config.requestCount
107
109
  : 'graphql_envelop_request',
108
110
  help: 'Counts the amount of GraphQL requests executed through Envelop',
109
111
  labelNames: ['operationType', 'operationName'].filter(label => labelExists(config, label)),
110
- registers: [config.registry || defaultRegistry],
111
- }),
112
+ },
112
113
  fillLabelsFn: params => filterFillParamsFnParams(config, {
113
114
  operationName: params.operationName,
114
115
  operationType: params.operationType,
@@ -119,14 +120,14 @@ export const usePrometheus = (config = {}) => {
119
120
  ? config.deprecatedFields
120
121
  : config.deprecatedFields === true || typeof config.deprecatedFields === 'string'
121
122
  ? createCounter({
122
- counter: new Counter({
123
+ registry,
124
+ counter: {
123
125
  name: typeof config.deprecatedFields === 'string'
124
126
  ? config.deprecatedFields
125
127
  : 'graphql_envelop_deprecated_field',
126
128
  help: 'Counts the amount of deprecated fields used in selection sets',
127
129
  labelNames: ['operationType', 'operationName', 'fieldName', 'typeName'].filter(label => labelExists(config, label)),
128
- registers: [config.registry || defaultRegistry],
129
- }),
130
+ },
130
131
  fillLabelsFn: params => filterFillParamsFnParams(config, {
131
132
  operationName: params.operationName,
132
133
  operationType: params.operationType,
@@ -139,13 +140,13 @@ export const usePrometheus = (config = {}) => {
139
140
  ? config.schemaChangeCount
140
141
  : config.schemaChangeCount === true || typeof config.schemaChangeCount === 'string'
141
142
  ? createCounter({
142
- counter: new Counter({
143
+ registry,
144
+ counter: {
143
145
  name: typeof config.schemaChangeCount === 'string'
144
146
  ? config.schemaChangeCount
145
147
  : 'graphql_envelop_schema_change',
146
148
  help: 'Counts the amount of schema changes',
147
- registers: [config.registry || defaultRegistry],
148
- }),
149
+ },
149
150
  fillLabelsFn: () => ({}),
150
151
  })
151
152
  : undefined;
package/esm/metrics.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";
package/esm/utils.js CHANGED
@@ -1,5 +1,8 @@
1
1
  import { visit, visitWithTypeInfo, } from 'graphql';
2
- import { register as defaultRegistry, Histogram } from 'prom-client';
2
+ import { Counter, register as defaultRegistry, Histogram, Summary, } from 'prom-client';
3
+ const histograms = new WeakMap();
4
+ const summaries = new WeakMap();
5
+ const counters = new WeakMap();
3
6
  export function shouldTraceFieldResolver(info, whitelist) {
4
7
  if (!whitelist) {
5
8
  return true;
@@ -26,26 +29,69 @@ export function createFillLabelFnParams(parseResult, context, filterParams) {
26
29
  operationType: operation.operation,
27
30
  });
28
31
  }
32
+ export function registerHistogram(registry, conf) {
33
+ if (!histograms.has(registry)) {
34
+ histograms.set(registry, new Map());
35
+ }
36
+ const registryHistograms = histograms.get(registry);
37
+ if (!registryHistograms.has(conf.name)) {
38
+ conf.registers = [registry];
39
+ registryHistograms.set(conf.name, new Histogram(conf));
40
+ }
41
+ return registryHistograms.get(conf.name);
42
+ }
29
43
  export function createHistogram(options) {
30
- return options;
44
+ return {
45
+ histogram: registerHistogram(options.registry, options.histogram),
46
+ // histogram: new Histogram(options.histogram),
47
+ fillLabelsFn: options.fillLabelsFn,
48
+ };
49
+ }
50
+ export function registerSummary(registry, conf) {
51
+ if (!summaries.has(registry)) {
52
+ summaries.set(registry, new Map());
53
+ }
54
+ const registrySummaries = summaries.get(registry);
55
+ if (!registrySummaries.has(conf.name)) {
56
+ conf.registers = [registry];
57
+ registrySummaries.set(conf.name, new Summary(conf));
58
+ }
59
+ return registrySummaries.get(conf.name);
31
60
  }
32
61
  export function createSummary(options) {
33
- return options;
62
+ return {
63
+ summary: registerSummary(options.registry, options.summary),
64
+ fillLabelsFn: options.fillLabelsFn,
65
+ };
66
+ }
67
+ export function registerCounter(registry, conf) {
68
+ if (!counters.has(registry)) {
69
+ counters.set(registry, new Map());
70
+ }
71
+ const registryCounters = counters.get(registry);
72
+ if (!registryCounters.has(conf.name)) {
73
+ conf.registers = [registry];
74
+ registryCounters.set(conf.name, new Counter(conf));
75
+ }
76
+ return registryCounters.get(conf.name);
34
77
  }
35
78
  export function createCounter(options) {
36
- return options;
79
+ return {
80
+ counter: registerCounter(options.registry, options.counter),
81
+ fillLabelsFn: options.fillLabelsFn,
82
+ };
37
83
  }
38
84
  export function getHistogramFromConfig(config, phase, name, help) {
39
85
  return typeof config[phase] === 'object'
40
86
  ? config[phase]
41
87
  : config[phase] === true
42
88
  ? createHistogram({
43
- histogram: new Histogram({
89
+ registry: config.registry || defaultRegistry,
90
+ histogram: {
44
91
  name,
45
92
  help,
46
93
  labelNames: ['operationType', 'operationName'].filter(label => labelExists(config, label)),
47
- registers: [config.registry || defaultRegistry],
48
- }),
94
+ },
49
95
  fillLabelsFn: params => filterFillParamsFnParams(config, {
50
96
  operationName: params.operationName,
51
97
  operationType: params.operationType,
@@ -92,3 +138,16 @@ export function labelExists(config, label) {
92
138
  export function filterFillParamsFnParams(config, params) {
93
139
  return Object.fromEntries(Object.entries(params).filter(([key]) => labelExists(config, key)));
94
140
  }
141
+ const clearRegistry = new WeakMap();
142
+ export function instrumentRegistry(registry) {
143
+ if (!clearRegistry.has(registry)) {
144
+ clearRegistry.set(registry, registry.clear.bind(registry));
145
+ }
146
+ registry.clear = () => {
147
+ histograms.delete(registry);
148
+ summaries.delete(registry);
149
+ counters.delete(registry);
150
+ clearRegistry.get(registry)();
151
+ };
152
+ return registry;
153
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/prometheus",
3
- "version": "9.4.0",
3
+ "version": "10.0.0-alpha-20240429133416-6ed19f38",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@envelop/core": "^5.0.0",
@@ -1,7 +1,7 @@
1
1
  import { Plugin } from '@envelop/core';
2
2
  import { PrometheusTracingPluginConfig } from './config.cjs';
3
3
  import { createCounter, createHistogram, createSummary, FillLabelsFnParams } from './utils.cjs';
4
- export { PrometheusTracingPluginConfig, createCounter, createHistogram, createSummary, FillLabelsFnParams, };
4
+ export { FillLabelsFnParams, PrometheusTracingPluginConfig, createCounter, createHistogram, createSummary, };
5
5
  export declare const fillLabelsFnParamsMap: WeakMap<any, FillLabelsFnParams | null>;
6
6
  export declare const execStartTimeMap: WeakMap<any, number>;
7
7
  export declare const usePrometheus: (config?: PrometheusTracingPluginConfig) => Plugin;
@@ -1,7 +1,7 @@
1
1
  import { Plugin } from '@envelop/core';
2
2
  import { PrometheusTracingPluginConfig } from './config.js';
3
3
  import { createCounter, createHistogram, createSummary, FillLabelsFnParams } from './utils.js';
4
- export { PrometheusTracingPluginConfig, createCounter, createHistogram, createSummary, FillLabelsFnParams, };
4
+ export { FillLabelsFnParams, PrometheusTracingPluginConfig, createCounter, createHistogram, createSummary, };
5
5
  export declare const fillLabelsFnParamsMap: WeakMap<any, FillLabelsFnParams | null>;
6
6
  export declare const execStartTimeMap: WeakMap<any, number>;
7
7
  export declare const usePrometheus: (config?: PrometheusTracingPluginConfig) => Plugin;
File without changes
File without changes
@@ -1,5 +1,5 @@
1
1
  import { ASTNode, DocumentNode, GraphQLError, GraphQLResolveInfo, OperationDefinitionNode, TypeInfo } from 'graphql';
2
- import { Counter, Histogram, Summary } from 'prom-client';
2
+ import { Counter, Histogram, Summary, type CounterConfiguration, type HistogramConfiguration, type Registry, type SummaryConfiguration } from 'prom-client';
3
3
  import { AfterParseEventPayload } from '@envelop/core';
4
4
  import { PrometheusTracingPluginConfig } from './config.cjs';
5
5
  export type DeprecatedFieldInfo = {
@@ -17,22 +17,41 @@ export type FillLabelsFnParams = {
17
17
  };
18
18
  export declare function shouldTraceFieldResolver(info: GraphQLResolveInfo, whitelist: string[] | undefined): boolean;
19
19
  export declare function createFillLabelFnParams(parseResult: AfterParseEventPayload<any>['result'], context: any, filterParams: (params: FillLabelsFnParams) => FillLabelsFnParams | null): FillLabelsFnParams | null;
20
- export type FillLabelsFn<LabelNames extends string> = (params: FillLabelsFnParams, rawContext: any) => Record<LabelNames, string>;
21
- export declare function createHistogram<LabelNames extends string>(options: {
20
+ export type FillLabelsFn<LabelNames extends string, Params extends Record<string, any>> = (params: Params, rawContext: any) => Record<LabelNames, string | number>;
21
+ export type HistogramAndLabels<LabelNames extends string, Params extends Record<string, any>> = {
22
22
  histogram: Histogram<LabelNames>;
23
- fillLabelsFn: FillLabelsFn<LabelNames>;
24
- }): typeof options;
25
- export declare function createSummary<LabelNames extends string>(options: {
23
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
24
+ };
25
+ export declare function registerHistogram<LabelNames extends string>(registry: Registry, conf: Omit<HistogramConfiguration<LabelNames>, 'registers'>): Histogram<LabelNames>;
26
+ export declare function createHistogram<LabelNames extends string, Params extends Record<string, any> = FillLabelsFnParams>(options: {
27
+ registry: Registry;
28
+ histogram: Omit<HistogramConfiguration<LabelNames>, 'registers'>;
29
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
30
+ }): HistogramAndLabels<LabelNames, Params>;
31
+ export type SummaryAndLabels<LabelNames extends string, Params extends Record<string, any>> = {
26
32
  summary: Summary<LabelNames>;
27
- fillLabelsFn: FillLabelsFn<LabelNames>;
28
- }): typeof options;
29
- export declare function createCounter<LabelNames extends string>(options: {
33
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
34
+ };
35
+ export declare function registerSummary<LabelNames extends string>(registry: Registry, conf: Omit<SummaryConfiguration<LabelNames>, 'registers'>): Summary<LabelNames>;
36
+ export declare function createSummary<LabelNames extends string, Params extends Record<string, any> = FillLabelsFnParams>(options: {
37
+ registry: Registry;
38
+ summary: Omit<SummaryConfiguration<LabelNames>, 'registers'>;
39
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
40
+ }): SummaryAndLabels<LabelNames, Params>;
41
+ export type CounterAndLabels<LabelNames extends string, Params extends Record<string, any>> = {
30
42
  counter: Counter<LabelNames>;
31
- fillLabelsFn: FillLabelsFn<LabelNames>;
32
- }): typeof options;
43
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
44
+ };
45
+ export declare function registerCounter<LabelNames extends string>(registry: Registry, conf: Omit<CounterConfiguration<LabelNames>, 'registers'>): Counter<LabelNames>;
46
+ export declare function createCounter<LabelNames extends string, Params extends Record<string, any> = FillLabelsFnParams>(options: {
47
+ registry: Registry;
48
+ counter: Omit<CounterConfiguration<LabelNames>, 'registers'>;
49
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
50
+ }): CounterAndLabels<LabelNames, Params>;
33
51
  export declare function getHistogramFromConfig(config: PrometheusTracingPluginConfig, phase: keyof PrometheusTracingPluginConfig, name: string, help: string): ReturnType<typeof createHistogram> | undefined;
34
52
  export declare function extractDeprecatedFields(node: ASTNode, typeInfo: TypeInfo): DeprecatedFieldInfo[];
35
53
  export declare function labelExists(config: PrometheusTracingPluginConfig, label: string): any;
36
54
  export declare function filterFillParamsFnParams(config: PrometheusTracingPluginConfig, params: Record<string, any>): {
37
55
  [k: string]: any;
38
56
  };
57
+ export declare function instrumentRegistry(registry: Registry): Registry<"text/plain; version=0.0.4; charset=utf-8">;
@@ -1,5 +1,5 @@
1
1
  import { ASTNode, DocumentNode, GraphQLError, GraphQLResolveInfo, OperationDefinitionNode, TypeInfo } from 'graphql';
2
- import { Counter, Histogram, Summary } from 'prom-client';
2
+ import { Counter, Histogram, Summary, type CounterConfiguration, type HistogramConfiguration, type Registry, type SummaryConfiguration } from 'prom-client';
3
3
  import { AfterParseEventPayload } from '@envelop/core';
4
4
  import { PrometheusTracingPluginConfig } from './config.js';
5
5
  export type DeprecatedFieldInfo = {
@@ -17,22 +17,41 @@ export type FillLabelsFnParams = {
17
17
  };
18
18
  export declare function shouldTraceFieldResolver(info: GraphQLResolveInfo, whitelist: string[] | undefined): boolean;
19
19
  export declare function createFillLabelFnParams(parseResult: AfterParseEventPayload<any>['result'], context: any, filterParams: (params: FillLabelsFnParams) => FillLabelsFnParams | null): FillLabelsFnParams | null;
20
- export type FillLabelsFn<LabelNames extends string> = (params: FillLabelsFnParams, rawContext: any) => Record<LabelNames, string>;
21
- export declare function createHistogram<LabelNames extends string>(options: {
20
+ export type FillLabelsFn<LabelNames extends string, Params extends Record<string, any>> = (params: Params, rawContext: any) => Record<LabelNames, string | number>;
21
+ export type HistogramAndLabels<LabelNames extends string, Params extends Record<string, any>> = {
22
22
  histogram: Histogram<LabelNames>;
23
- fillLabelsFn: FillLabelsFn<LabelNames>;
24
- }): typeof options;
25
- export declare function createSummary<LabelNames extends string>(options: {
23
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
24
+ };
25
+ export declare function registerHistogram<LabelNames extends string>(registry: Registry, conf: Omit<HistogramConfiguration<LabelNames>, 'registers'>): Histogram<LabelNames>;
26
+ export declare function createHistogram<LabelNames extends string, Params extends Record<string, any> = FillLabelsFnParams>(options: {
27
+ registry: Registry;
28
+ histogram: Omit<HistogramConfiguration<LabelNames>, 'registers'>;
29
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
30
+ }): HistogramAndLabels<LabelNames, Params>;
31
+ export type SummaryAndLabels<LabelNames extends string, Params extends Record<string, any>> = {
26
32
  summary: Summary<LabelNames>;
27
- fillLabelsFn: FillLabelsFn<LabelNames>;
28
- }): typeof options;
29
- export declare function createCounter<LabelNames extends string>(options: {
33
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
34
+ };
35
+ export declare function registerSummary<LabelNames extends string>(registry: Registry, conf: Omit<SummaryConfiguration<LabelNames>, 'registers'>): Summary<LabelNames>;
36
+ export declare function createSummary<LabelNames extends string, Params extends Record<string, any> = FillLabelsFnParams>(options: {
37
+ registry: Registry;
38
+ summary: Omit<SummaryConfiguration<LabelNames>, 'registers'>;
39
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
40
+ }): SummaryAndLabels<LabelNames, Params>;
41
+ export type CounterAndLabels<LabelNames extends string, Params extends Record<string, any>> = {
30
42
  counter: Counter<LabelNames>;
31
- fillLabelsFn: FillLabelsFn<LabelNames>;
32
- }): typeof options;
43
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
44
+ };
45
+ export declare function registerCounter<LabelNames extends string>(registry: Registry, conf: Omit<CounterConfiguration<LabelNames>, 'registers'>): Counter<LabelNames>;
46
+ export declare function createCounter<LabelNames extends string, Params extends Record<string, any> = FillLabelsFnParams>(options: {
47
+ registry: Registry;
48
+ counter: Omit<CounterConfiguration<LabelNames>, 'registers'>;
49
+ fillLabelsFn: FillLabelsFn<LabelNames, Params>;
50
+ }): CounterAndLabels<LabelNames, Params>;
33
51
  export declare function getHistogramFromConfig(config: PrometheusTracingPluginConfig, phase: keyof PrometheusTracingPluginConfig, name: string, help: string): ReturnType<typeof createHistogram> | undefined;
34
52
  export declare function extractDeprecatedFields(node: ASTNode, typeInfo: TypeInfo): DeprecatedFieldInfo[];
35
53
  export declare function labelExists(config: PrometheusTracingPluginConfig, label: string): any;
36
54
  export declare function filterFillParamsFnParams(config: PrometheusTracingPluginConfig, params: Record<string, any>): {
37
55
  [k: string]: any;
38
56
  };
57
+ export declare function instrumentRegistry(registry: Registry): Registry<"text/plain; version=0.0.4; charset=utf-8">;