@envelop/prometheus 6.4.0-alpha-e9434aa.0 → 6.4.0-alpha-05dbec7.0
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/esm/config.js +1 -2
- package/esm/index.js +34 -40
- package/esm/utils.js +12 -22
- package/package.json +2 -2
package/esm/config.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/esm/index.js
CHANGED
|
@@ -1,31 +1,26 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.usePrometheus = exports.createSummary = exports.createHistogram = exports.createCounter = void 0;
|
|
4
1
|
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
Object.defineProperty(exports, "createCounter", { enumerable: true, get: function () { return utils_js_1.createCounter; } });
|
|
11
|
-
Object.defineProperty(exports, "createSummary", { enumerable: true, get: function () { return utils_js_1.createSummary; } });
|
|
2
|
+
import { isIntrospectionOperationString, isAsyncIterable, } from '@envelop/core';
|
|
3
|
+
import { TypeInfo } from 'graphql';
|
|
4
|
+
import { Summary, Counter, Histogram, register as defaultRegistry } from 'prom-client';
|
|
5
|
+
import { getHistogramFromConfig, createHistogram, createCounter, shouldTraceFieldResolver, createInternalContext, extractDeprecatedFields, createSummary, } from './utils.js';
|
|
6
|
+
export { createCounter, createHistogram, createSummary };
|
|
12
7
|
const promPluginContext = Symbol('promPluginContext');
|
|
13
8
|
const promPluginExecutionStartTimeSymbol = Symbol('promPluginExecutionStartTimeSymbol');
|
|
14
|
-
const usePrometheus = (config = {}) => {
|
|
9
|
+
export const usePrometheus = (config = {}) => {
|
|
15
10
|
let typeInfo = null;
|
|
16
|
-
const parseHistogram =
|
|
17
|
-
const validateHistogram =
|
|
18
|
-
const contextBuildingHistogram =
|
|
19
|
-
const executeHistogram =
|
|
11
|
+
const parseHistogram = getHistogramFromConfig(config, 'parse', 'graphql_envelop_phase_parse', 'Time spent on running GraphQL "parse" function');
|
|
12
|
+
const validateHistogram = getHistogramFromConfig(config, 'validate', 'graphql_envelop_phase_validate', 'Time spent on running GraphQL "validate" function');
|
|
13
|
+
const contextBuildingHistogram = getHistogramFromConfig(config, 'contextBuilding', 'graphql_envelop_phase_context', 'Time spent on building the GraphQL context');
|
|
14
|
+
const executeHistogram = getHistogramFromConfig(config, 'execute', 'graphql_envelop_phase_execute', 'Time spent on running the GraphQL "execute" function');
|
|
20
15
|
const resolversHistogram = typeof config.resolvers === 'object'
|
|
21
16
|
? config.resolvers
|
|
22
17
|
: config.resolvers === true
|
|
23
|
-
?
|
|
24
|
-
histogram: new
|
|
18
|
+
? createHistogram({
|
|
19
|
+
histogram: new Histogram({
|
|
25
20
|
name: 'graphql_envelop_execute_resolver',
|
|
26
21
|
help: 'Time spent on running the GraphQL resolvers',
|
|
27
22
|
labelNames: ['operationType', 'operationName', 'fieldName', 'typeName', 'returnType'],
|
|
28
|
-
registers: [config.registry ||
|
|
23
|
+
registers: [config.registry || defaultRegistry],
|
|
29
24
|
}),
|
|
30
25
|
fillLabelsFn: params => {
|
|
31
26
|
var _a, _b, _c;
|
|
@@ -42,12 +37,12 @@ const usePrometheus = (config = {}) => {
|
|
|
42
37
|
const requestTotalHistogram = typeof config.requestTotalDuration === 'object'
|
|
43
38
|
? config.requestTotalDuration
|
|
44
39
|
: config.requestTotalDuration === true
|
|
45
|
-
?
|
|
46
|
-
histogram: new
|
|
40
|
+
? createHistogram({
|
|
41
|
+
histogram: new Histogram({
|
|
47
42
|
name: 'graphql_envelop_request_duration',
|
|
48
43
|
help: 'Time spent on running the GraphQL operation from parse to execute',
|
|
49
44
|
labelNames: ['operationType', 'operationName'],
|
|
50
|
-
registers: [config.registry ||
|
|
45
|
+
registers: [config.registry || defaultRegistry],
|
|
51
46
|
}),
|
|
52
47
|
fillLabelsFn: params => ({
|
|
53
48
|
operationName: params.operationName,
|
|
@@ -58,12 +53,12 @@ const usePrometheus = (config = {}) => {
|
|
|
58
53
|
const requestSummary = typeof config.requestSummary === 'object'
|
|
59
54
|
? config.requestSummary
|
|
60
55
|
: config.requestSummary === true
|
|
61
|
-
?
|
|
62
|
-
summary: new
|
|
56
|
+
? createSummary({
|
|
57
|
+
summary: new Summary({
|
|
63
58
|
name: 'graphql_envelop_request_time_summary',
|
|
64
59
|
help: 'Summary to measure the time to complete GraphQL operations',
|
|
65
60
|
labelNames: ['operationType', 'operationName'],
|
|
66
|
-
registers: [config.registry ||
|
|
61
|
+
registers: [config.registry || defaultRegistry],
|
|
67
62
|
}),
|
|
68
63
|
fillLabelsFn: params => ({
|
|
69
64
|
operationName: params.operationName,
|
|
@@ -74,12 +69,12 @@ const usePrometheus = (config = {}) => {
|
|
|
74
69
|
const errorsCounter = typeof config.errors === 'object'
|
|
75
70
|
? config.errors
|
|
76
71
|
: config.errors === true
|
|
77
|
-
?
|
|
78
|
-
counter: new
|
|
72
|
+
? createCounter({
|
|
73
|
+
counter: new Counter({
|
|
79
74
|
name: 'graphql_envelop_error_result',
|
|
80
75
|
help: 'Counts the amount of errors reported from all phases',
|
|
81
76
|
labelNames: ['operationType', 'operationName', 'path', 'phase'],
|
|
82
|
-
registers: [config.registry ||
|
|
77
|
+
registers: [config.registry || defaultRegistry],
|
|
83
78
|
}),
|
|
84
79
|
fillLabelsFn: params => {
|
|
85
80
|
var _a, _b;
|
|
@@ -95,12 +90,12 @@ const usePrometheus = (config = {}) => {
|
|
|
95
90
|
const reqCounter = typeof config.requestCount === 'object'
|
|
96
91
|
? config.requestCount
|
|
97
92
|
: config.requestCount === true
|
|
98
|
-
?
|
|
99
|
-
counter: new
|
|
93
|
+
? createCounter({
|
|
94
|
+
counter: new Counter({
|
|
100
95
|
name: 'graphql_envelop_request',
|
|
101
96
|
help: 'Counts the amount of GraphQL requests executed through Envelop',
|
|
102
97
|
labelNames: ['operationType', 'operationName'],
|
|
103
|
-
registers: [config.registry ||
|
|
98
|
+
registers: [config.registry || defaultRegistry],
|
|
104
99
|
}),
|
|
105
100
|
fillLabelsFn: params => ({
|
|
106
101
|
operationName: params.operationName,
|
|
@@ -111,12 +106,12 @@ const usePrometheus = (config = {}) => {
|
|
|
111
106
|
const deprecationCounter = typeof config.deprecatedFields === 'object'
|
|
112
107
|
? config.deprecatedFields
|
|
113
108
|
: config.deprecatedFields === true
|
|
114
|
-
?
|
|
115
|
-
counter: new
|
|
109
|
+
? createCounter({
|
|
110
|
+
counter: new Counter({
|
|
116
111
|
name: 'graphql_envelop_deprecated_field',
|
|
117
112
|
help: 'Counts the amount of deprecated fields used in selection sets',
|
|
118
113
|
labelNames: ['operationType', 'operationName', 'fieldName', 'typeName'],
|
|
119
|
-
registers: [config.registry ||
|
|
114
|
+
registers: [config.registry || defaultRegistry],
|
|
120
115
|
}),
|
|
121
116
|
fillLabelsFn: params => {
|
|
122
117
|
var _a, _b;
|
|
@@ -130,20 +125,20 @@ const usePrometheus = (config = {}) => {
|
|
|
130
125
|
})
|
|
131
126
|
: undefined;
|
|
132
127
|
const onParse = ({ context, extendContext, params }) => {
|
|
133
|
-
if (config.skipIntrospection &&
|
|
128
|
+
if (config.skipIntrospection && isIntrospectionOperationString(params.source)) {
|
|
134
129
|
return;
|
|
135
130
|
}
|
|
136
131
|
const startTime = Date.now();
|
|
137
132
|
return params => {
|
|
138
133
|
const totalTime = (Date.now() - startTime) / 1000;
|
|
139
|
-
const internalContext =
|
|
134
|
+
const internalContext = createInternalContext(params.result);
|
|
140
135
|
if (internalContext) {
|
|
141
136
|
extendContext({
|
|
142
137
|
[promPluginContext]: internalContext,
|
|
143
138
|
});
|
|
144
139
|
parseHistogram === null || parseHistogram === void 0 ? void 0 : parseHistogram.histogram.observe(parseHistogram.fillLabelsFn(internalContext, context), totalTime);
|
|
145
140
|
if (deprecationCounter && typeInfo) {
|
|
146
|
-
const deprecatedFields =
|
|
141
|
+
const deprecatedFields = extractDeprecatedFields(internalContext.document, typeInfo);
|
|
147
142
|
if (deprecatedFields.length > 0) {
|
|
148
143
|
for (const depField of deprecatedFields) {
|
|
149
144
|
deprecationCounter.counter
|
|
@@ -211,7 +206,7 @@ const usePrometheus = (config = {}) => {
|
|
|
211
206
|
const summaryTime = (Date.now() - args.contextValue[promPluginExecutionStartTimeSymbol]) / 1000;
|
|
212
207
|
requestSummary.summary.observe(requestSummary.fillLabelsFn(args.contextValue[promPluginContext], args.contextValue), summaryTime);
|
|
213
208
|
}
|
|
214
|
-
if (errorsCounter && !
|
|
209
|
+
if (errorsCounter && !isAsyncIterable(result) && result.errors && result.errors.length > 0) {
|
|
215
210
|
for (const error of result.errors) {
|
|
216
211
|
errorsCounter.counter
|
|
217
212
|
.labels(errorsCounter.fillLabelsFn({
|
|
@@ -230,7 +225,7 @@ const usePrometheus = (config = {}) => {
|
|
|
230
225
|
return {
|
|
231
226
|
onResolverCalled: resolversHistogram
|
|
232
227
|
? ({ info, context }) => {
|
|
233
|
-
const shouldTrace =
|
|
228
|
+
const shouldTrace = shouldTraceFieldResolver(info, config.resolversWhitelist);
|
|
234
229
|
if (!shouldTrace) {
|
|
235
230
|
return undefined;
|
|
236
231
|
}
|
|
@@ -251,7 +246,7 @@ const usePrometheus = (config = {}) => {
|
|
|
251
246
|
});
|
|
252
247
|
},
|
|
253
248
|
onSchemaChange({ schema }) {
|
|
254
|
-
typeInfo = new
|
|
249
|
+
typeInfo = new TypeInfo(schema);
|
|
255
250
|
},
|
|
256
251
|
onParse,
|
|
257
252
|
onValidate,
|
|
@@ -259,4 +254,3 @@ const usePrometheus = (config = {}) => {
|
|
|
259
254
|
onExecute,
|
|
260
255
|
};
|
|
261
256
|
};
|
|
262
|
-
exports.usePrometheus = usePrometheus;
|
package/esm/utils.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const graphql_1 = require("graphql");
|
|
5
|
-
const prom_client_1 = require("prom-client");
|
|
6
|
-
function shouldTraceFieldResolver(info, whitelist) {
|
|
1
|
+
import { visit, visitWithTypeInfo, } from 'graphql';
|
|
2
|
+
import { Histogram, register as defaultRegistry } from 'prom-client';
|
|
3
|
+
export function shouldTraceFieldResolver(info, whitelist) {
|
|
7
4
|
if (!whitelist) {
|
|
8
5
|
return true;
|
|
9
6
|
}
|
|
@@ -12,11 +9,10 @@ function shouldTraceFieldResolver(info, whitelist) {
|
|
|
12
9
|
const coordinate = `${parentType}.${fieldName}`;
|
|
13
10
|
return whitelist.includes(coordinate) || whitelist.includes(`${parentType}.*`);
|
|
14
11
|
}
|
|
15
|
-
exports.shouldTraceFieldResolver = shouldTraceFieldResolver;
|
|
16
12
|
function getOperation(document) {
|
|
17
13
|
return document.definitions[0];
|
|
18
14
|
}
|
|
19
|
-
function createInternalContext(parseResult) {
|
|
15
|
+
export function createInternalContext(parseResult) {
|
|
20
16
|
var _a;
|
|
21
17
|
if (parseResult === null) {
|
|
22
18
|
return null;
|
|
@@ -31,29 +27,25 @@ function createInternalContext(parseResult) {
|
|
|
31
27
|
operationType: operation.operation,
|
|
32
28
|
};
|
|
33
29
|
}
|
|
34
|
-
|
|
35
|
-
function createHistogram(options) {
|
|
30
|
+
export function createHistogram(options) {
|
|
36
31
|
return options;
|
|
37
32
|
}
|
|
38
|
-
|
|
39
|
-
function createSummary(options) {
|
|
33
|
+
export function createSummary(options) {
|
|
40
34
|
return options;
|
|
41
35
|
}
|
|
42
|
-
|
|
43
|
-
function createCounter(options) {
|
|
36
|
+
export function createCounter(options) {
|
|
44
37
|
return options;
|
|
45
38
|
}
|
|
46
|
-
|
|
47
|
-
function getHistogramFromConfig(config, phase, name, help) {
|
|
39
|
+
export function getHistogramFromConfig(config, phase, name, help) {
|
|
48
40
|
return typeof config[phase] === 'object'
|
|
49
41
|
? config[phase]
|
|
50
42
|
: config[phase] === true
|
|
51
43
|
? createHistogram({
|
|
52
|
-
histogram: new
|
|
44
|
+
histogram: new Histogram({
|
|
53
45
|
name,
|
|
54
46
|
help,
|
|
55
47
|
labelNames: ['operationType', 'operationName'],
|
|
56
|
-
registers: [config.registry ||
|
|
48
|
+
registers: [config.registry || defaultRegistry],
|
|
57
49
|
}),
|
|
58
50
|
fillLabelsFn: params => ({
|
|
59
51
|
operationName: params.operationName,
|
|
@@ -62,10 +54,9 @@ function getHistogramFromConfig(config, phase, name, help) {
|
|
|
62
54
|
})
|
|
63
55
|
: undefined;
|
|
64
56
|
}
|
|
65
|
-
|
|
66
|
-
function extractDeprecatedFields(node, typeInfo) {
|
|
57
|
+
export function extractDeprecatedFields(node, typeInfo) {
|
|
67
58
|
const found = [];
|
|
68
|
-
|
|
59
|
+
visit(node, visitWithTypeInfo(typeInfo, {
|
|
69
60
|
Field: () => {
|
|
70
61
|
const field = typeInfo.getFieldDef();
|
|
71
62
|
if (field && (field.deprecationReason != null || field.isDeprecated)) {
|
|
@@ -78,4 +69,3 @@ function extractDeprecatedFields(node, typeInfo) {
|
|
|
78
69
|
}));
|
|
79
70
|
return found;
|
|
80
71
|
}
|
|
81
|
-
exports.extractDeprecatedFields = extractDeprecatedFields;
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/prometheus",
|
|
3
|
-
"version": "6.4.0-alpha-
|
|
3
|
+
"version": "6.4.0-alpha-05dbec7.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "2.4.0-alpha-
|
|
6
|
+
"@envelop/core": "2.4.0-alpha-05dbec7.0",
|
|
7
7
|
"prom-client": "^13 || ^14.0.0",
|
|
8
8
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
9
9
|
},
|