{"version":3,"file":"index.cjs","names":["createHttpMetrics","createGcMetrics","createRequestRecorder","createGcObserver","defaultNormalizers","Prometheus","timing","pkg.name"],"sources":["../package.json","../src/plugin/plugin.ts"],"sourcesContent":["","import type { TPromsterTiming, TRequestRecorder } from '@promster/metrics';\nimport {\n  createGcMetrics,\n  createGcObserver,\n  createHttpMetrics,\n  createRequestRecorder,\n  defaultNormalizers,\n  Prometheus,\n  skipMetricsInEnvironment,\n  timing,\n} from '@promster/metrics';\nimport type {\n  TDefaultedPromsterOptions,\n  TGcMetrics,\n  THttpMetrics,\n  TLabelValues,\n  TOptionalPromsterOptions,\n} from '@promster/types';\nimport type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';\nimport fastifyPlugin from 'fastify-plugin';\nimport merge from 'merge-options';\n\nimport pkg from '../../package.json';\n\nlet recordRequest: TRequestRecorder;\nlet upMetric: TGcMetrics['up'];\n\nconst extractPath = (req: FastifyRequest): string =>\n  // @ts-expect-error\n  req.raw.originalUrl || req.raw.url;\nconst getRequestRecorder = () => recordRequest;\nconst signalIsUp = () => {\n  if (!upMetric) {\n    return;\n  }\n\n  for (const upMetricType of upMetric) {\n    upMetricType.set(1);\n  }\n};\n\nconst signalIsNotUp = () => {\n  if (!upMetric) {\n    return;\n  }\n\n  for (const upMetricType of upMetric) {\n    upMetricType.set(0);\n  }\n};\n\ntype TSkipFunction = <TRequest = FastifyRequest, TResponse = FastifyReply>(\n  _req: TRequest,\n  _res: TResponse,\n  _labels: TLabelValues,\n) => boolean;\n\nexport type TPromsterOptions = TOptionalPromsterOptions & {\n  skip?: TSkipFunction;\n};\nconst createPlugin = async (\n  fastify: FastifyInstance,\n  options: TPromsterOptions,\n) => {\n  const allDefaultedOptions: TDefaultedPromsterOptions & {\n    skip?: TSkipFunction;\n  } = merge(\n    createHttpMetrics.defaultOptions,\n    createGcMetrics.defaultOptions,\n    createRequestRecorder.defaultOptions,\n    // @ts-expect-error\n    createGcObserver.defaultOptions,\n    defaultNormalizers,\n    options,\n  );\n\n  const shouldSkipMetricsByEnvironment =\n    skipMetricsInEnvironment(allDefaultedOptions);\n\n  const httpMetrics: THttpMetrics = createHttpMetrics(allDefaultedOptions);\n  const gcMetrics: TGcMetrics = createGcMetrics(allDefaultedOptions);\n\n  const observeGc = createGcObserver(gcMetrics, allDefaultedOptions);\n\n  recordRequest = createRequestRecorder(httpMetrics, allDefaultedOptions);\n  upMetric = gcMetrics?.up;\n\n  if (!shouldSkipMetricsByEnvironment) {\n    observeGc();\n  }\n\n  fastify.decorate('Prometheus', Prometheus);\n  fastify.decorate('recordRequest', recordRequest);\n  fastify.decorateRequest<TPromsterTiming | null>('__promsterTiming__', null);\n\n  fastify.addHook('onRequest', async (request, _) => {\n    // @ts-expect-error\n    request.__promsterTiming__ = timing.start();\n  });\n\n  fastify.addHook('onResponse', async (request, reply) => {\n    const labels = Object.assign(\n      {},\n      {\n        method: allDefaultedOptions.normalizeMethod(request.raw.method ?? '', {\n          req: request,\n          res: reply,\n        }),\n        status_code: allDefaultedOptions.normalizeStatusCode(reply.statusCode, {\n          req: request,\n          res: reply,\n        }),\n        path: allDefaultedOptions.normalizePath(extractPath(request), {\n          req: request,\n          res: reply,\n        }),\n      },\n      allDefaultedOptions.getLabelValues?.(request, reply),\n    );\n\n    const requestContentLength = Number(request.headers['content-length'] ?? 0);\n    const responseContentLength = Number(\n      reply.getHeader('content-length') ?? 0,\n    );\n\n    const shouldSkipByRequest = allDefaultedOptions.skip?.(\n      request,\n      reply,\n      labels,\n    );\n\n    if (!shouldSkipByRequest && !shouldSkipMetricsByEnvironment) {\n      // @ts-expect-error\n      recordRequest(request.__promsterTiming__ as TPromsterTiming, {\n        labels,\n        requestContentLength,\n        responseContentLength,\n      });\n    }\n  });\n\n  signalIsUp();\n};\n\nconst plugin = fastifyPlugin(createPlugin, {\n  fastify: '>= 5.0.0',\n  name: pkg.name,\n});\n\nexport { plugin, getRequestRecorder, signalIsUp, signalIsNotUp, extractPath };\n"],"mappings":";;;;;;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