UNPKG

@graphql-yoga/plugin-prometheus

Version:
97 lines (96 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSummary = exports.createHistogram = exports.createCounter = void 0; exports.usePrometheus = usePrometheus; const graphql_1 = require("graphql"); const prom_client_1 = require("prom-client"); const prometheus_1 = require("@envelop/prometheus"); Object.defineProperty(exports, "createCounter", { enumerable: true, get: function () { return prometheus_1.createCounter; } }); Object.defineProperty(exports, "createHistogram", { enumerable: true, get: function () { return prometheus_1.createHistogram; } }); Object.defineProperty(exports, "createSummary", { enumerable: true, get: function () { return prometheus_1.createSummary; } }); function usePrometheus(options) { const endpoint = options.endpoint || '/metrics'; const registry = options.registry || prom_client_1.register; let httpHistogram; function labelExists(label) { if (options.labels?.[label] == null) { return true; } return options.labels[label]; } if (options.http) { const labelNames = ['method', 'statusCode']; if (labelExists('operationName')) { labelNames.push('operationName'); } if (labelExists('operationType')) { labelNames.push('operationType'); } httpHistogram = typeof options.http === 'object' ? options.http : (0, prometheus_1.createHistogram)({ registry, histogram: { name: typeof options.http === 'string' ? options.http : 'graphql_yoga_http_duration', help: 'Time spent on HTTP connection', labelNames, }, fillLabelsFn(params, { request, response }) { const labels = { method: request.method, statusCode: response.status, }; if (labelExists('operationType') && params.operationType) { labels.operationType = params.operationType; } if (labelExists('operationName')) { labels.operationName = params.operationName || 'Anonymous'; } return labels; }, }); } const startByRequest = new WeakMap(); const paramsByRequest = new WeakMap(); return { onPluginInit({ addPlugin }) { addPlugin((0, prometheus_1.usePrometheus)({ ...options, registry })); }, onRequest({ request, url, fetchAPI, endResponse }) { startByRequest.set(request, Date.now()); if (endpoint && url.pathname === endpoint) { return registry.metrics().then(metrics => { endResponse(new fetchAPI.Response(metrics, { headers: { 'Content-Type': registry.contentType, }, })); }); } return undefined; }, onParse() { return ({ result: document, context: { params, request } }) => { const operationAST = (0, graphql_1.getOperationAST)(document, params.operationName); paramsByRequest.set(request, { document, operationName: operationAST?.name?.value, operationType: operationAST?.operation, }); }; }, onResponse({ request, response, serverContext }) { const start = startByRequest.get(request); if (start) { const duration = Date.now() - start; const params = paramsByRequest.get(request); httpHistogram?.histogram.observe(httpHistogram.fillLabelsFn(params || {}, { ...serverContext, request, response, }), duration); } }, }; }