Skip to content

Commit

Permalink
AP-5778 introduce prisma metrics plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosexe committed Oct 31, 2024
1 parent 5673655 commit 0f708d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/app/prisma-utils/src/plugins/MetricsCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)) {
Expand All @@ -155,6 +162,7 @@ export class MetricsCollector {
this.metrics.histograms[key] = value
}
this.logger.debug({}, `Prisma metrics registered ${newMetrics.keys}`)
/* c8 ignore stop */
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/app/prisma-utils/src/plugins/prismaMetricsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrismaMetricsPluginOptions> =
Expand Down

0 comments on commit 0f708d6

Please sign in to comment.