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 29, 2024
1 parent d929018 commit ee2a5dd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/app/prisma-utils/src/plugins/MetricsCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function getMetrics(
metrics.counters[metric.key] = new prometheus.Counter({
name: metric.key,
help: metric.description,
labelNames: ['prisma', 'connection-pool'] as const,
labelNames: ['prisma', 'connection_pool'] as const,
})
metrics.names.push(metric.key)
}
Expand All @@ -39,7 +39,7 @@ function getMetrics(
metrics.gauges[metric.key] = new prometheus.Gauge({
name: metric.key,
help: metric.description,
labelNames: ['prisma', 'connection-pool'] as const,
labelNames: ['prisma', 'connection_pool'] as const,
})
metrics.names.push(metric.key)
}
Expand All @@ -49,7 +49,7 @@ function getMetrics(
name: metric.key,
help: metric.description,
buckets: histogramBuckets,
labelNames: ['prisma', 'connection-pool'] as const,
labelNames: ['prisma', 'connection_pool'] as const,
})
metrics.names.push(metric.key)
}
Expand Down
17 changes: 12 additions & 5 deletions packages/app/prisma-utils/src/plugins/prismaMetricsPlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { FastifyInstance } from 'fastify'
import fastify from 'fastify'

import { metricsPlugin } from '@lokalise/fastify-extras'
import { PrismaClient } from '@prisma/client'
import { generateMonotonicUuid } from '@lokalise/id-utils'
import { type Item1, PrismaClient } from '@prisma/client'
import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { z } from 'zod'
import { DB_MODEL, cleanTables } from '../../test/DbCleaner'
Expand All @@ -18,6 +19,11 @@ type TestOptions = {
enableMetricsPlugin: boolean
}

const TEST_ITEM_1: Item1 = {
id: generateMonotonicUuid(),
value: 'one',
}

const DEFAULT_TEST_OPTIONS = { enableMetricsPlugin: true }

async function initAppWithPrismaMetrics(
Expand Down Expand Up @@ -70,9 +76,9 @@ describe('prismaMetricsPlugin', () => {

it('throws if fastify-metrics was not initialized', async () => {
await expect(() => {
return initAppWithPrismaMetrics({ prisma })
return initAppWithPrismaMetrics({ prisma }, { enableMetricsPlugin: false })
}).rejects.toThrowError(
'No Prometheus Client found, BullMQ metrics plugin requires `fastify-metrics` plugin to be registered',
'No Prometheus Client found, Prisma metrics plugin requires `fastify-metrics` plugin to be registered',
)
})

Expand All @@ -84,18 +90,19 @@ describe('prismaMetricsPlugin', () => {

const responseBefore = await getMetrics()
expect(responseBefore.result.body).not.toContain(
'bullmq_jobs_finished_duration_count{status="completed",queue="test_job"} 1',
'prisma_pool_connections_opened_total{status="completed",queue="test_job"} 1',
)

// prisma call
await prisma.item1.create({ data: TEST_ITEM_1 })

await setTimeout(100)

await app.prismaMetrics.collect()

const responseAfter = await getMetrics()
expect(responseAfter.result.body).toContain(
'bullmq_jobs_finished_duration_count{status="completed",queue="test_job"} 1',
'prisma_pool_connections_opened_total{status="completed",queue="test_job"} 1',
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function plugin(
fastify.metrics.client.register,
fastify.log,
)
// await collector.init()
const collectFn = async () => await collector.collect()
let scheduler: CollectionScheduler

Expand Down

0 comments on commit ee2a5dd

Please sign in to comment.