From 0a7a89a2833bad15af3a03d79243bb7a6229c8b3 Mon Sep 17 00:00:00 2001 From: Tushar Choudhari Date: Fri, 24 May 2024 15:28:25 +0530 Subject: [PATCH] Update the workflow file --- .github/workflows/build.yml | 10 ++++++ playground/app.js | 6 ++-- run-tests.sh | 9 ++++- src/OpenAPM.ts | 11 ++---- tests/mysql2.test.ts | 2 +- tests/pg.test.ts | 70 +++++++++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 tests/pg.test.ts diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 80d7061..45c0afd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,12 +17,22 @@ jobs: with: mysql-version: '8.0' + - uses: harmon758/postgresql-action@v1 + with: + postgresql version: '14' + - name: Test MySQL Connection run: mysql -h localhost -u root -e 'SELECT version()' + - name: Test Postgres Connection + run: psql -h localhost -U root -c 'SELECT version()' + - name: Create test DB run: mysql -h localhost -u root -e 'CREATE DATABASE test_db' + - name: Create test DB + run: psql -h localhost -U root -c 'CREATE DATABASE testdb' + - name: 🛎 Checkout uses: actions/checkout@v3 diff --git a/playground/app.js b/playground/app.js index cd2e8f5..b9fcf81 100644 --- a/playground/app.js +++ b/playground/app.js @@ -5,7 +5,7 @@ * */ require('dotenv').config(); var express = require('express'); -var { OpenAPM } = require('../dist/index.js'); +var { OpenAPM } = require('../dist/src/index.js'); var pg = require('pg'); // var mysql2 = require('mysql2'); @@ -52,7 +52,9 @@ app.get('/result', async (req, res) => { // await client.query(`INSERT INTO "users" (username) VALUES ('JohnDoe');`); let result; try { - result = await client.query('select * from users;'); + result = await client.query( + "select * from users where username='JohnDoe';" + ); } catch (error) {} res.status(200).json({ diff --git a/run-tests.sh b/run-tests.sh index b069832..13999bd 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -38,6 +38,11 @@ runMysqlTests() { npm run vitest -t ./tests/mysql2.test.ts } +# Run PG tests +runPgTests() { + npm run vitest -t ./tests/pg.test.ts +} + # Check if a variable is passed if [ "$1" = "express" ]; then npm run vitest -t ./tests/express.test.ts @@ -53,7 +58,9 @@ elif [ "$1" = "nestjs" ]; then elif [ "$1" = "prisma" ]; then runPrismaTests elif [ "$1" = "mysql2" ]; then - runAllTests + runMysqlTests +elif [ "$1" = "pg" ]; then + runPgTests else runAllTests fi \ No newline at end of file diff --git a/src/OpenAPM.ts b/src/OpenAPM.ts index 7099631..b64d256 100644 --- a/src/OpenAPM.ts +++ b/src/OpenAPM.ts @@ -80,19 +80,14 @@ export interface OpenAPMOptions { levitateConfig?: LevitateConfig; } -export type SupportedModules = - | 'express' - | 'mysql' - | 'nestjs' - | 'nextjs' - | 'postgres'; +export type SupportedModules = 'express' | 'mysql' | 'nestjs' | 'nextjs' | 'pg'; const moduleNames = { express: 'express', mysql: 'mysql2', nestjs: '@nestjs/core', nextjs: 'next', - postgres: 'pg' + pg: 'pg' }; const packageJson = getPackageJson(); @@ -457,7 +452,7 @@ export class OpenAPM extends LevitateEvents { ); } - if (moduleName === 'postgres') { + if (moduleName === 'pg') { const pg = require('pg'); instrumentPG(pg); } diff --git a/tests/mysql2.test.ts b/tests/mysql2.test.ts index 7ed0731..e1a0d0f 100644 --- a/tests/mysql2.test.ts +++ b/tests/mysql2.test.ts @@ -3,7 +3,7 @@ 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:password@localhost:3306/test_db'; const sendTestRequest = async (conn: Connection | Pool, query: string) => { return new Promise((resolve) => { diff --git a/tests/pg.test.ts b/tests/pg.test.ts new file mode 100644 index 0000000..e50b8c6 --- /dev/null +++ b/tests/pg.test.ts @@ -0,0 +1,70 @@ +import type { Connection } from 'pg'; +import { describe, beforeAll, afterAll, expect, test } from 'vitest'; +import { Client } from 'pg'; +import { OpenAPM, getMetricClient } from '../src/OpenAPM'; + +let connectionUri = 'postgres://tester:password@localhost:5432/testdb'; + +describe('pg', () => { + let client; + let openapm: OpenAPM; + + const getMetrics = async () => { + const client = getMetricClient(); + const parsedData = await client.register.getMetricsAsJSON(); + return parsedData; + }; + + beforeAll(async () => { + openapm = new OpenAPM(); + openapm.instrument('pg'); + + client = new Client(connectionUri); + await client.connect(); + }); + + afterAll(async () => { + await openapm.shutdown(); + await client.end(); + }); + + test('should connect to the database', async () => { + const res = await client.query('SELECT NOW()'); + expect(res.rows[0].now).toBeDefined(); + }); + + test('should capture metrics', async () => { + const res = await client.query('SELECT NOW()'); + expect(res.rows[0].now).toBeDefined(); + const metrics = await getMetrics(); + + const length = metrics.find( + (m) => m.name === 'db_requests_duration_milliseconds' + )?.values.length; + + expect(length).toBeDefined(); + expect(length).toBeGreaterThan(0); + }); + + test('masks the values in the query', async () => { + await client.query('SELECT * FROM users WHERE id = 1'); + await client.query("SELECT * FROM users WHERE username = 'JohnDoe'"); + + const metrics = await getMetrics(); + + const histogram = metrics.find( + (m) => m.name === 'db_requests_duration_milliseconds' + ); + const values = histogram?.values; + + const numberQuery = values?.find( + (v) => v.labels.query === 'SELECT * FROM users WHERE id = $1' + ); + const stringQuery = values?.find( + (v) => v.labels.query === 'SELECT * FROM users WHERE username = $1' + ); + + expect(numberQuery).toBeDefined(); + expect(stringQuery).toBeDefined(); + }); +});