UNPKG

@promster/fastify

Version:

Fastify server integrations of promster

175 lines (167 loc) 6.03 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var metrics = require('@promster/metrics'); var fastifyPlugin = require('fastify-plugin'); var merge = require('merge-options'); var types = require('@promster/types'); function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; } var fastifyPlugin__default = /*#__PURE__*/_interopDefault(fastifyPlugin); var merge__default = /*#__PURE__*/_interopDefault(merge); var pkg = { name: "@promster/fastify", version: "15.3.1", description: "Fastify server integrations of promster", main: "dist/promster-fastify.cjs.js", typings: "dist/promster-fastify.cjs.d.ts", types: "dist/promster-fastify.cjs.d.ts", files: [ "readme.md", "package.json", "LICENSE", "dist/**", "modules/**" ], publishConfig: { access: "public" }, engines: { node: ">=20", npm: ">=8" }, repository: { type: "git", url: "https://github.com/tdeekens/promster.git", directory: "packages/fastify" }, author: "Tobias Deekens <nerd@tdeekens.name>", license: "MIT", bugs: { url: "https://github.com/tdeekens/promster/issues" }, homepage: "https://github.com/tdeekens/promster#readme", keywords: [ "metrics", "continousdelivery", "prometheus" ], dependencies: { "@promster/metrics": "workspace:*", "@promster/server": "workspace:*", "@promster/types": "workspace:*", "fastify-plugin": "^4.5.1", "merge-options": "3.0.4", "parse-prometheus-text-format": "1.1.1" }, devDependencies: { fastify: "4.29.0" } }; let recordRequest; let upMetric; const extractPath = req => // @ts-expect-error req.raw.originalUrl || req.raw.url; const getRequestRecorder = () => recordRequest; const signalIsUp = () => { if (!upMetric) { return; } for (const upMetricType of upMetric) { upMetricType.set(1); } }; const signalIsNotUp = () => { if (!upMetric) { return; } for (const upMetricType of upMetric) { upMetricType.set(0); } }; const createPlugin = async (fastify, options) => { const allDefaultedOptions = merge__default["default"](metrics.createHttpMetrics.defaultOptions, metrics.createGcMetrics.defaultOptions, metrics.createRequestRecorder.defaultOptions, // @ts-expect-error metrics.createGcObserver.defaultOptions, metrics.defaultNormalizers, options); const shouldSkipMetricsByEnvironment = metrics.skipMetricsInEnvironment(allDefaultedOptions); const httpMetrics = metrics.createHttpMetrics(allDefaultedOptions); const gcMetrics = metrics.createGcMetrics(allDefaultedOptions); const observeGc = metrics.createGcObserver(gcMetrics, allDefaultedOptions); recordRequest = metrics.createRequestRecorder(httpMetrics, allDefaultedOptions); upMetric = gcMetrics === null || gcMetrics === void 0 ? void 0 : gcMetrics.up; if (!shouldSkipMetricsByEnvironment) { observeGc(); } fastify.decorate('Prometheus', metrics.Prometheus); fastify.decorate('recordRequest', recordRequest); fastify.decorateRequest('__promsterTiming__', null); fastify.addHook('onRequest', async (request, _) => { // @ts-expect-error request.__promsterTiming__ = metrics.timing.start(); }); fastify.addHook('onResponse', async (request, reply) => { var _request$raw$method, _allDefaultedOptions$, _request$headers$cont, _reply$getHeader, _allDefaultedOptions$2; const labels = Object.assign({}, { method: allDefaultedOptions.normalizeMethod((_request$raw$method = request.raw.method) !== null && _request$raw$method !== void 0 ? _request$raw$method : '', { req: request, res: reply }), status_code: allDefaultedOptions.normalizeStatusCode(reply.statusCode, { req: request, res: reply }), path: allDefaultedOptions.normalizePath(extractPath(request), { req: request, res: reply }) }, (_allDefaultedOptions$ = allDefaultedOptions.getLabelValues) === null || _allDefaultedOptions$ === void 0 ? void 0 : _allDefaultedOptions$.call(allDefaultedOptions, request, reply)); const requestContentLength = Number((_request$headers$cont = request.headers['content-length']) !== null && _request$headers$cont !== void 0 ? _request$headers$cont : 0); const responseContentLength = Number((_reply$getHeader = reply.getHeader('content-length')) !== null && _reply$getHeader !== void 0 ? _reply$getHeader : 0); const shouldSkipByRequest = (_allDefaultedOptions$2 = allDefaultedOptions.skip) === null || _allDefaultedOptions$2 === void 0 ? void 0 : _allDefaultedOptions$2.call(allDefaultedOptions, request, reply, labels); if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) { // @ts-expect-error recordRequest(request.__promsterTiming__, { labels, requestContentLength, responseContentLength }); } }); }; const plugin = fastifyPlugin__default["default"](createPlugin, { fastify: '>= 1.6.0', name: pkg.name }); Object.defineProperty(exports, 'Prometheus', { enumerable: true, get: function () { return metrics.Prometheus; } }); Object.defineProperty(exports, 'defaultNormalizers', { enumerable: true, get: function () { return metrics.defaultNormalizers; } }); Object.defineProperty(exports, 'defaultRegister', { enumerable: true, get: function () { return metrics.defaultRegister; } }); Object.defineProperty(exports, 'getContentType', { enumerable: true, get: function () { return metrics.getContentType; } }); Object.defineProperty(exports, 'getSummary', { enumerable: true, get: function () { return metrics.getSummary; } }); Object.defineProperty(exports, 'timing', { enumerable: true, get: function () { return metrics.timing; } }); exports.getRequestRecorder = getRequestRecorder; exports.plugin = plugin; exports.signalIsNotUp = signalIsNotUp; exports.signalIsUp = signalIsUp; Object.keys(types).forEach(function (k) { if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, { enumerable: true, get: function () { return types[k]; } }); });