@envelop/prometheus
Version:
This plugin tracks the complete execution flow, and reports metrics using Prometheus tracing (based on `prom-client`).
93 lines (92 loc) • 3.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractDeprecatedFields = exports.getHistogramFromConfig = exports.createCounter = exports.createSummary = exports.createHistogram = exports.createInternalContext = exports.shouldTraceFieldResolver = void 0;
const graphql_1 = require("graphql");
const prom_client_1 = require("prom-client");
function shouldTraceFieldResolver(info, whitelist) {
if (!whitelist) {
return true;
}
const parentType = info.parentType.name;
const fieldName = info.fieldName;
const coordinate = `${parentType}.${fieldName}`;
return whitelist.includes(coordinate) || whitelist.includes(`${parentType}.*`);
}
exports.shouldTraceFieldResolver = shouldTraceFieldResolver;
function getOperation(document) {
return document.definitions[0];
}
function createInternalContext(parseResult) {
if (parseResult === null) {
return null;
}
if (parseResult instanceof Error) {
return null;
}
const operation = getOperation(parseResult);
return {
document: parseResult,
operationName: operation.name?.value || 'Anonymous',
operationType: operation.operation,
};
}
exports.createInternalContext = createInternalContext;
function createHistogram(options) {
return options;
}
exports.createHistogram = createHistogram;
function createSummary(options) {
return options;
}
exports.createSummary = createSummary;
function createCounter(options) {
return options;
}
exports.createCounter = createCounter;
function getHistogramFromConfig(config, phase, name, help) {
return typeof config[phase] === 'object'
? config[phase]
: config[phase] === true
? createHistogram({
histogram: new prom_client_1.Histogram({
name,
help,
labelNames: ['operationType', 'operationName'],
registers: [config.registry || prom_client_1.register],
}),
fillLabelsFn: params => ({
operationName: params.operationName,
operationType: params.operationType,
}),
})
: undefined;
}
exports.getHistogramFromConfig = getHistogramFromConfig;
function extractDeprecatedFields(node, typeInfo) {
const found = [];
(0, graphql_1.visit)(node, (0, graphql_1.visitWithTypeInfo)(typeInfo, {
Argument: () => {
const argument = typeInfo.getArgument();
const field = typeInfo.getFieldDef();
if (field && argument && (argument.deprecationReason != null || argument.isDeprecated)) {
found.push({
fieldName: argument.name,
// the GraphQLArgument type doesn't contain context regarding the mutation the argument was passed to
// however, when visiting an argument, typeInfo.getFieldDef returns the mutation
typeName: field.name, // this is the mutation name
});
}
},
Field: () => {
const field = typeInfo.getFieldDef();
if (field && (field.deprecationReason != null || field.isDeprecated)) {
found.push({
fieldName: field.name,
typeName: typeInfo.getParentType().name || '',
});
}
},
}));
return found;
}
exports.extractDeprecatedFields = extractDeprecatedFields;