@envelop/prometheus
Version:
This plugin tracks the complete execution flow, and reports metrics using Prometheus tracing (based on `prom-client`).
396 lines (395 loc) • 20 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePrometheus = exports.execStartTimeMap = exports.fillLabelsFnParamsMap = exports.createSummary = exports.createHistogram = exports.createCounter = void 0;
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
const graphql_1 = require("graphql");
const prom_client_1 = require("prom-client");
const core_1 = require("@envelop/core");
const on_resolve_1 = require("@envelop/on-resolve");
const utils_js_1 = require("./utils.js");
Object.defineProperty(exports, "createCounter", { enumerable: true, get: function () { return utils_js_1.createCounter; } });
Object.defineProperty(exports, "createHistogram", { enumerable: true, get: function () { return utils_js_1.createHistogram; } });
Object.defineProperty(exports, "createSummary", { enumerable: true, get: function () { return utils_js_1.createSummary; } });
exports.fillLabelsFnParamsMap = new WeakMap();
exports.execStartTimeMap = new WeakMap();
const usePrometheus = (config = {}) => {
let typeInfo = null;
const registry = (0, utils_js_1.instrumentRegistry)(config.registry || prom_client_1.register);
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');
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');
const contextBuildingHistogram = (0, utils_js_1.getHistogramFromConfig)(config, 'contextBuilding', typeof config.contextBuilding === 'string'
? config.contextBuilding
: 'graphql_envelop_phase_context', 'Time spent on building the GraphQL context');
const executeHistogram = (0, utils_js_1.getHistogramFromConfig)(config, 'execute', typeof config.execute === 'string' ? config.execute : 'graphql_envelop_phase_execute', 'Time spent on running the GraphQL "execute" function');
const subscribeHistogram = (0, utils_js_1.getHistogramFromConfig)(config, 'subscribe', typeof config.subscribe === 'string' ? config.subscribe : 'graphql_envelop_phase_subscribe', 'Time spent on running the GraphQL "subscribe" function');
const resolversHistogram = typeof config.resolvers === 'object'
? config.resolvers
: config.resolvers === true || typeof config.resolvers === 'string'
? (0, utils_js_1.createHistogram)({
registry,
histogram: {
name: typeof config.resolvers === 'string'
? config.resolvers
: 'graphql_envelop_execute_resolver',
help: 'Time spent on running the GraphQL resolvers',
labelNames: [
'operationType',
'operationName',
'fieldName',
'typeName',
'returnType',
].filter(label => (0, utils_js_1.labelExists)(config, label)),
},
fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
operationName: params.operationName,
operationType: params.operationType,
fieldName: params.info?.fieldName,
typeName: params.info?.parentType.name,
returnType: params.info?.returnType.toString(),
}),
})
: undefined;
const requestTotalHistogram = typeof config.requestTotalDuration === 'object'
? config.requestTotalDuration
: config.requestTotalDuration === true || typeof config.requestTotalDuration === 'string'
? (0, utils_js_1.createHistogram)({
registry,
histogram: {
name: typeof config.requestTotalDuration === 'string'
? config.requestTotalDuration
: 'graphql_envelop_request_duration',
help: 'Time spent on running the GraphQL operation from parse to execute',
labelNames: ['operationType', 'operationName'].filter(label => (0, utils_js_1.labelExists)(config, label)),
},
fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
operationName: params.operationName,
operationType: params.operationType,
}),
})
: undefined;
const requestSummary = typeof config.requestSummary === 'object'
? config.requestSummary
: config.requestSummary === true || typeof config.requestSummary === 'string'
? (0, utils_js_1.createSummary)({
registry,
summary: {
name: typeof config.requestSummary === 'string'
? config.requestSummary
: 'graphql_envelop_request_time_summary',
help: 'Summary to measure the time to complete GraphQL operations',
labelNames: ['operationType', 'operationName'].filter(label => (0, utils_js_1.labelExists)(config, label)),
},
fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
operationName: params.operationName,
operationType: params.operationType,
}),
})
: undefined;
const errorsCounter = typeof config.errors === 'object'
? config.errors
: config.errors === true || typeof config.errors === 'string'
? (0, utils_js_1.createCounter)({
registry,
counter: {
name: typeof config.errors === 'string' ? config.errors : 'graphql_envelop_error_result',
help: 'Counts the amount of errors reported from all phases',
labelNames: ['operationType', 'operationName', 'path', 'phase'].filter(label => (0, utils_js_1.labelExists)(config, label)),
},
fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
operationName: params.operationName,
operationType: params.operationType,
path: params.error?.path?.join('.'),
phase: params.errorPhase,
}),
})
: undefined;
const reqCounter = typeof config.requestCount === 'object'
? config.requestCount
: config.requestCount === true || typeof config.requestCount === 'string'
? (0, utils_js_1.createCounter)({
registry,
counter: {
name: typeof config.requestCount === 'string'
? config.requestCount
: 'graphql_envelop_request',
help: 'Counts the amount of GraphQL requests executed through Envelop',
labelNames: ['operationType', 'operationName'].filter(label => (0, utils_js_1.labelExists)(config, label)),
},
fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
operationName: params.operationName,
operationType: params.operationType,
}),
})
: undefined;
const deprecationCounter = typeof config.deprecatedFields === 'object'
? config.deprecatedFields
: config.deprecatedFields === true || typeof config.deprecatedFields === 'string'
? (0, utils_js_1.createCounter)({
registry,
counter: {
name: typeof config.deprecatedFields === 'string'
? config.deprecatedFields
: 'graphql_envelop_deprecated_field',
help: 'Counts the amount of deprecated fields used in selection sets',
labelNames: ['operationType', 'operationName', 'fieldName', 'typeName'].filter(label => (0, utils_js_1.labelExists)(config, label)),
},
fillLabelsFn: params => (0, utils_js_1.filterFillParamsFnParams)(config, {
operationName: params.operationName,
operationType: params.operationType,
fieldName: params.deprecationInfo?.fieldName,
typeName: params.deprecationInfo?.typeName,
}),
})
: undefined;
const schemaChangeCounter = typeof config.schemaChangeCount === 'object'
? config.schemaChangeCount
: config.schemaChangeCount === true || typeof config.schemaChangeCount === 'string'
? (0, utils_js_1.createCounter)({
registry,
counter: {
name: typeof config.schemaChangeCount === 'string'
? config.schemaChangeCount
: 'graphql_envelop_schema_change',
help: 'Counts the amount of schema changes',
},
fillLabelsFn: () => ({}),
})
: undefined;
const onParse = ({ context, params }) => {
if (config.skipIntrospection && (0, core_1.isIntrospectionOperationString)(params.source)) {
return;
}
const startTime = Date.now();
return params => {
const totalTime = (Date.now() - startTime) / 1000;
let fillLabelsFnParams = exports.fillLabelsFnParamsMap.get(params.result);
if (!fillLabelsFnParams) {
fillLabelsFnParams = (0, utils_js_1.createFillLabelFnParams)(params.result, context, params => (0, utils_js_1.filterFillParamsFnParams)(config, params));
exports.fillLabelsFnParamsMap.set(context, fillLabelsFnParams);
}
if (fillLabelsFnParams) {
parseHistogram?.histogram.observe(parseHistogram.fillLabelsFn(fillLabelsFnParams, context), totalTime);
if (deprecationCounter && typeInfo) {
const deprecatedFields = (0, utils_js_1.extractDeprecatedFields)(fillLabelsFnParams.document, typeInfo);
if (deprecatedFields.length > 0) {
for (const depField of deprecatedFields) {
deprecationCounter.counter
.labels(deprecationCounter.fillLabelsFn({
...fillLabelsFnParams,
deprecationInfo: depField,
}, context))
.inc();
}
}
}
}
else {
// means that we got a parse error, report it
errorsCounter?.counter
.labels({
phase: 'parse',
})
.inc();
}
};
};
const onValidate = validateHistogram
? ({ context }) => {
const fillLabelsFnParams = exports.fillLabelsFnParamsMap.get(context);
if (!fillLabelsFnParams) {
return undefined;
}
const startTime = Date.now();
return ({ valid }) => {
const totalTime = (Date.now() - startTime) / 1000;
const labels = validateHistogram.fillLabelsFn(fillLabelsFnParams, context);
validateHistogram.histogram.observe(labels, totalTime);
if (!valid) {
errorsCounter?.counter
.labels({
...labels,
phase: 'validate',
})
.inc();
}
};
}
: undefined;
const onContextBuilding = contextBuildingHistogram
? ({ context }) => {
const fillLabelsFnParams = exports.fillLabelsFnParamsMap.get(context);
if (!fillLabelsFnParams) {
return undefined;
}
const startTime = Date.now();
return () => {
const totalTime = (Date.now() - startTime) / 1000;
contextBuildingHistogram.histogram.observe(contextBuildingHistogram.fillLabelsFn(fillLabelsFnParams, context), totalTime);
};
}
: undefined;
const onExecute = executeHistogram
? ({ args }) => {
const fillLabelsFnParams = exports.fillLabelsFnParamsMap.get(args.contextValue);
if (!fillLabelsFnParams) {
return undefined;
}
const startTime = Date.now();
reqCounter?.counter
.labels(reqCounter.fillLabelsFn(fillLabelsFnParams, args.contextValue))
.inc();
function handleResult(result) {
if (errorsCounter && result.errors && result.errors.length > 0) {
for (const error of result.errors) {
errorsCounter.counter
.labels(errorsCounter.fillLabelsFn({
...fillLabelsFnParams,
errorPhase: 'execute',
error,
}, args.contextValue))
.inc();
}
}
}
const result = {
onExecuteDone: ({ result }) => {
const execStartTime = exports.execStartTimeMap.get(args.contextValue);
const handleEnd = () => {
const totalTime = (Date.now() - startTime) / 1000;
executeHistogram.histogram.observe(executeHistogram.fillLabelsFn(fillLabelsFnParams, args.contextValue), totalTime);
requestTotalHistogram?.histogram.observe(requestTotalHistogram.fillLabelsFn(fillLabelsFnParams, args.contextValue), totalTime);
if (requestSummary && execStartTime) {
const summaryTime = (Date.now() - execStartTime) / 1000;
requestSummary.summary.observe(requestSummary.fillLabelsFn(fillLabelsFnParams, args.contextValue), summaryTime);
}
};
if (!(0, core_1.isAsyncIterable)(result)) {
handleResult(result);
handleEnd();
return undefined;
}
else {
return {
onNext({ result }) {
handleResult(result);
},
onEnd() {
handleEnd();
},
};
}
},
};
return result;
}
: undefined;
const onSubscribe = subscribeHistogram
? ({ args }) => {
const fillLabelsFnParams = exports.fillLabelsFnParamsMap.get(args.contextValue);
if (!fillLabelsFnParams) {
return undefined;
}
const startTime = Date.now();
reqCounter?.counter
.labels(reqCounter.fillLabelsFn(fillLabelsFnParams, args.contextValue))
.inc();
function handleResult(result) {
if (errorsCounter && result.errors && result.errors.length > 0) {
for (const error of result.errors) {
errorsCounter.counter
.labels(errorsCounter.fillLabelsFn({
...fillLabelsFnParams,
errorPhase: 'execute',
error,
}, args.contextValue))
.inc();
}
}
}
const result = {
onSubscribeResult: ({ result }) => {
const execStartTime = exports.execStartTimeMap.get(args.contextValue);
const handleEnd = () => {
const totalTime = (Date.now() - startTime) / 1000;
subscribeHistogram.histogram.observe(subscribeHistogram.fillLabelsFn(fillLabelsFnParams, args.contextValue), totalTime);
requestTotalHistogram?.histogram.observe(requestTotalHistogram.fillLabelsFn(fillLabelsFnParams, args.contextValue), totalTime);
if (requestSummary && execStartTime) {
const summaryTime = (Date.now() - execStartTime) / 1000;
requestSummary.summary.observe(requestSummary.fillLabelsFn(fillLabelsFnParams, args.contextValue), summaryTime);
}
};
if (!(0, core_1.isAsyncIterable)(result)) {
handleResult(result);
handleEnd();
return undefined;
}
else {
return {
onNext({ result }) {
handleResult(result);
},
onEnd() {
handleEnd();
},
};
}
},
};
return result;
}
: undefined;
const countedSchemas = new WeakSet();
return {
onEnveloped({ context }) {
if (!exports.execStartTimeMap.has(context)) {
exports.execStartTimeMap.set(context, Date.now());
}
},
onPluginInit({ addPlugin, registerContextErrorHandler }) {
if (resolversHistogram) {
addPlugin((0, on_resolve_1.useOnResolve)(({ info, context }) => {
const shouldTrace = (0, utils_js_1.shouldTraceFieldResolver)(info, config.resolversWhitelist);
if (!shouldTrace) {
return undefined;
}
const startTime = Date.now();
return () => {
const totalTime = (Date.now() - startTime) / 1000;
const fillLabelsFnParams = exports.fillLabelsFnParamsMap.get(context);
const paramsCtx = {
...fillLabelsFnParams,
info,
};
resolversHistogram.histogram.observe(resolversHistogram.fillLabelsFn(paramsCtx, context), totalTime);
};
}));
}
registerContextErrorHandler(({ context }) => {
const fillLabelsFnParams = exports.fillLabelsFnParamsMap.get(context);
let extraLabels;
if (fillLabelsFnParams) {
extraLabels = contextBuildingHistogram?.fillLabelsFn(fillLabelsFnParams, context);
}
errorsCounter?.counter
.labels({
...extraLabels,
phase: 'context',
})
.inc();
});
},
onSchemaChange({ schema }) {
typeInfo = new graphql_1.TypeInfo(schema);
if (schemaChangeCounter && !countedSchemas.has(schema)) {
schemaChangeCounter.counter.inc();
countedSchemas.add(schema);
}
},
onParse,
onValidate,
onContextBuilding,
onExecute,
onSubscribe,
};
};
exports.usePrometheus = usePrometheus;