Skip to content

Commit

Permalink
Update the workflow file
Browse files Browse the repository at this point in the history
  • Loading branch information
chtushar committed May 24, 2024
1 parent cf0f24a commit 0a7a89a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 12 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions playground/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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({
Expand Down
9 changes: 8 additions & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
11 changes: 3 additions & 8 deletions src/OpenAPM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -457,7 +452,7 @@ export class OpenAPM extends LevitateEvents {
);
}

if (moduleName === 'postgres') {
if (moduleName === 'pg') {
const pg = require('pg');
instrumentPG(pg);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/mysql2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
70 changes: 70 additions & 0 deletions tests/pg.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 0a7a89a

Please sign in to comment.