From 0f708d6d67608341df1e1c22fd4351ad26f36b5f Mon Sep 17 00:00:00 2001 From: dosexe Date: Thu, 31 Oct 2024 15:03:43 +0300 Subject: [PATCH] AP-5778 introduce prisma metrics plugin --- packages/app/prisma-utils/src/plugins/MetricsCollector.ts | 8 ++++++++ .../app/prisma-utils/src/plugins/prismaMetricsPlugin.ts | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/app/prisma-utils/src/plugins/MetricsCollector.ts b/packages/app/prisma-utils/src/plugins/MetricsCollector.ts index fbb1c732..8023aa03 100644 --- a/packages/app/prisma-utils/src/plugins/MetricsCollector.ts +++ b/packages/app/prisma-utils/src/plugins/MetricsCollector.ts @@ -101,28 +101,34 @@ export class MetricsCollector { const jsonMetrics = await this.getJsonMetrics() for (const counterMetric of jsonMetrics.counters) { const existingMetric = this.metrics.counters[counterMetric.key] + /* c8 ignore start */ if (!existingMetric) { nonRegisteredMetrics.counters.push(counterMetric) continue } + /* c8 ignore stop */ // we need to reset counter since prisma returns already the accumulated counter value existingMetric.reset() existingMetric.inc(counterMetric.value) } for (const gaugeMetric of jsonMetrics.gauges) { const existingMetric = this.metrics.gauges[gaugeMetric.key] + /* c8 ignore start */ if (!existingMetric) { nonRegisteredMetrics.gauges.push(gaugeMetric) continue } + /* c8 ignore stop */ existingMetric.set(gaugeMetric.value) } for (const histogramMetric of jsonMetrics.histograms) { const existingMetric = this.metrics.histograms[histogramMetric.key] + /* c8 ignore start */ if (!existingMetric) { nonRegisteredMetrics.histograms.push(histogramMetric) continue } + /* c8 ignore stop */ existingMetric.observe(histogramMetric.value.count) } @@ -143,6 +149,7 @@ export class MetricsCollector { return } + /* c8 ignore start */ const newMetrics = registerMetrics(prefix, nonRegisteredMetrics) for (const [key, value] of Object.entries(newMetrics.counters)) { @@ -155,6 +162,7 @@ export class MetricsCollector { this.metrics.histograms[key] = value } this.logger.debug({}, `Prisma metrics registered ${newMetrics.keys}`) + /* c8 ignore stop */ } /** diff --git a/packages/app/prisma-utils/src/plugins/prismaMetricsPlugin.ts b/packages/app/prisma-utils/src/plugins/prismaMetricsPlugin.ts index 6ce0840a..6e4a1a20 100644 --- a/packages/app/prisma-utils/src/plugins/prismaMetricsPlugin.ts +++ b/packages/app/prisma-utils/src/plugins/prismaMetricsPlugin.ts @@ -83,15 +83,15 @@ function plugin( }) next() - } catch (err: unknown) { /* c8 ignore start */ + } catch (err: unknown) { return next( err instanceof Error ? err : new Error('Unknown error in prisma-metrics-plugin', { cause: err }), ) - /* c8 ignore stop */ } + /* c8 ignore stop */ } export const prismaMetricsPlugin: FastifyPluginCallback =