diff --git a/.eslintrc.js b/.eslintrc.js index 6adaf38..f78cb5b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -6,6 +6,7 @@ module.exports = { rules: { 'func-call-spacing': 'off', 'space-before-function-paren': 'off', - '@typescript-eslint/no-var-requires': 'off' + '@typescript-eslint/no-var-requires': 'off', + '@typescript-eslint/ban-ts-comment': 'off' } }; diff --git a/src/OpenAPM.ts b/src/OpenAPM.ts index eb19a26..22b87fb 100644 --- a/src/OpenAPM.ts +++ b/src/OpenAPM.ts @@ -17,9 +17,10 @@ import { getHostIpAddress, getPackageJson, getSanitizedPath } from './utils'; import { instrumentExpress } from './clients/express'; import { instrumentMySQL } from './clients/mysql2'; import { instrumentNestFactory } from './clients/nestjs'; -import { LevitateConfig, LevitateEvents } from './levitate/events'; import { instrumentNextjs } from './clients/nextjs'; +import { LevitateConfig, LevitateEvents } from './levitate/events'; + export type ExtractFromParams = { from: 'params'; key: string; @@ -85,7 +86,7 @@ const moduleNames = { const packageJson = getPackageJson(); export class OpenAPM extends LevitateEvents { - private simpleCache: Record = {}; + readonly simpleCache: Record = {}; private path: string; private metricsServerPort: number; private enabled: boolean; @@ -189,8 +190,6 @@ export class OpenAPM extends LevitateEvents { console.log('Shutting down metrics server gracefully.'); } this.metricsServer?.close((err) => { - promClient.register.clear(); - if (err) { reject(err); return; @@ -199,6 +198,9 @@ export class OpenAPM extends LevitateEvents { resolve(undefined); console.log('Metrics server shut down gracefully.'); }); + + promClient.register.clear(); + resolve(undefined); }); }; @@ -348,6 +350,8 @@ export class OpenAPM extends LevitateEvents { const prisma = new PrismaClient(); const prismaMetrics = prisma ? await prisma.$metrics.prometheus() : ''; metrics += prisma ? prismaMetrics : ''; + + await prisma.$disconnect(); } catch (error) { this.simpleCache['prisma:installed'] = false; } @@ -392,7 +396,6 @@ export class OpenAPM extends LevitateEvents { } } catch (error) { if (Object.keys(moduleNames).includes(moduleName)) { - console.log(error); throw new Error( `OpenAPM couldn't import the ${moduleNames[moduleName]} package, please install it.` ); diff --git a/tests/mysql2.test.ts b/tests/mysql2.test.ts index 0e0b169..5933fbd 100644 --- a/tests/mysql2.test.ts +++ b/tests/mysql2.test.ts @@ -1,9 +1,9 @@ -import { describe, beforeAll, expect, test, vi, afterAll } from 'vitest'; -import mysql2, { Connection, Pool, PoolCluster, PoolNamespace } from 'mysql2'; +import { describe, beforeAll, expect, test, afterAll } from 'vitest'; +import mysql2, { Connection, Pool, PoolCluster } from 'mysql2'; import { instrumentMySQL, symbols } from '../src/clients/mysql2'; import prom, { Histogram } from 'prom-client'; -const connectionUri = `mysql://root@localhost:3306/test_db`; +const connectionUri = 'mysql://root@localhost:3306/test_db'; const sendTestRequest = async (conn: Connection | Pool, query: string) => { return new Promise((resolve) => { diff --git a/tests/prisma.test.ts b/tests/prisma.test.ts new file mode 100644 index 0000000..377a83e --- /dev/null +++ b/tests/prisma.test.ts @@ -0,0 +1,44 @@ +import express, { Express } from 'express'; +import { test, expect, describe, beforeAll, afterAll, vi } from 'vitest'; + +import OpenAPM from '../src/OpenAPM'; +import { addRoutes, makeRequest } from './utils'; + +describe('Prisma', () => { + let openapm: OpenAPM; + let app: Express; + + beforeAll(async () => { + openapm = new OpenAPM({ + enableMetricsServer: false + }); + openapm.instrument('express'); + + app = express(); + + app.get('/metrics', async (req, res) => { + res.status(200).send(await openapm.getMetrics()); + }); + + addRoutes(app); + app.listen(3002); + }); + + afterAll(async () => { + await openapm.shutdown(); + }); + + test('prisma:installed - false', async () => { + vi.doMock('@prisma/client', async () => { + throw new Error('Cannot find module @prisma/client'); + }); + await makeRequest(app, '/api/10'); + await makeRequest(app, '/metrics'); + + expect(openapm.simpleCache['prisma:installed']).toBe(false); + }); + + test('simpleCache', async () => { + expect(openapm.simpleCache['prisma:installed']).toBe(false); + }); +});