Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
chtushar committed Nov 8, 2023
1 parent 4667fc0 commit a39468b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
1 change: 0 additions & 1 deletion playground/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ openapm.instrument('express');
openapm.instrument('mysql');

const app = express();
// app.use(openapm.REDMiddleware);

const pool = mysql2.createPool(
`mysql://express-app:[email protected]/express` // If this throws an error, Change the db url to the one you're running on your machine locally or the testing instance you might have hosted.
Expand Down
20 changes: 8 additions & 12 deletions src/OpenAPM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,12 @@ export interface OpenAPMOptions {
excludeDefaultLabels?: Array<DefaultLabels>;
}

export enum SupportedModules {
Express = 'express',
MySQL = 'mysql',
NestJS = 'nestjs'
}
export type SupportedModules = 'express' | 'mysql' | 'nestjs';

const moduleNames = {
[SupportedModules.Express]: 'express',
[SupportedModules.MySQL]: 'mysql2',
[SupportedModules.NestJS]: '@nestjs/core'
express: 'express',
mysql: 'mysql2',
nestjs: '@nestjs/core'
};

const packageJson = getPackageJson();
Expand Down Expand Up @@ -301,20 +297,20 @@ export class OpenAPM extends EventEmitter {

public instrument(moduleName: SupportedModules) {
try {
if (moduleName === SupportedModules.Express) {
if (moduleName === 'express') {
const express = require('express');
instrumentExpress(express, this._REDMiddleware);
}
if (moduleName === SupportedModules.MySQL) {
if (moduleName === 'mysql') {
const mysql2 = require('mysql2');
instrumentMySQL(mysql2);
}
if (moduleName === SupportedModules.NestJS) {
if (moduleName === 'nestjs') {
const { NestFactory } = require('@nestjs/core');
instrumentNestFactory(NestFactory, this._REDMiddleware);
}
} catch (error) {
if (Object.values(SupportedModules).includes(moduleName)) {
if (Object.keys(moduleNames).includes(moduleName)) {
console.log(error);
throw new Error(
`OpenAPM couldn't import the ${moduleNames[moduleName]} package, please install it.`
Expand Down
5 changes: 3 additions & 2 deletions tests/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ describe('REDMiddleware', () => {
let parsedData: Array<Record<string, any>> = [];

beforeAll(async () => {
app = express();
openapm = new OpenAPM();
app.use(openapm.REDMiddleware);
openapm.instrument('express');

app = express();

addRoutes(app);
app.listen(3002);
Expand Down

0 comments on commit a39468b

Please sign in to comment.