Skip to content

Commit 22a7bd7

Browse files
committed
feat: monitor MongoDB client
1 parent 92fe9f2 commit 22a7bd7

File tree

3 files changed

+486
-38
lines changed

3 files changed

+486
-38
lines changed

infra/mongo.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,54 @@
1+
// @ts-check
12
const { MongoClient, ObjectId } = require('mongodb');
3+
const { mongodb, logs } = require('@codefresh-io/cf-telemetry');
24
const { getDbNameFromUri } = require('./helper');
35

46
class Mongo {
57
constructor() {
68
this.db = undefined;
79
this.ObjectId = ObjectId;
10+
this.logger = new logs.Logger('codefresh:infra:mongo');
811
}
912

1013
/**
1114
* starts the connection to mongo
15+
* @returns {Promise<void>}
1216
*/
1317
async init(config) {
14-
const clientSettings = { ...config.mongo.options };
15-
const logger = require('cf-logs').Logger('codefresh:infra:mongo'); // eslint-disable-line
16-
this.logger = logger;
17-
18-
const { uri } = config.mongo;
19-
const dbName = config.mongo.dbName || getDbNameFromUri(uri);
20-
const client = new MongoClient(uri, clientSettings);
21-
logger.info(`Mongo db name ${dbName}`);
22-
2318
try {
24-
await client.connect();
25-
logger.info('Mongo driver connected');
19+
const clientSettings = { ...config.mongo.options };
20+
const { uri } = config.mongo;
21+
const dbName = config.mongo.dbName || getDbNameFromUri(uri);
22+
this.logger.info(`Mongo db name ${dbName}`);
23+
const client = new MongoClient(uri, clientSettings);
24+
mongodb.monitorMongoDBClient(client);
25+
26+
this.client = await client.connect();
27+
this.logger.info('Mongo driver connected');
28+
this.db = this.client.db(dbName);
29+
this.logger.info('Mongo db initialized');
2630
} catch (error) {
27-
logger.error('Error connecting to MongoDB:', error);
31+
this.logger.error(error, 'Error connecting to MongoDB');
2832
throw error;
2933
}
30-
31-
this.client = client;
32-
this.db = this.client.db(dbName);
33-
logger.info('Mongo db initialized');
3434
}
3535

3636

3737
/**
3838
* stops the connection to mongo
39+
* @returns {Promise<void>}
3940
*/
4041
async stop() {
41-
if (!this.db) {
42-
return;
43-
}
42+
if (!this.client) return;
4443
await this.client.close();
4544
}
4645

46+
/**
47+
* @param {string} collectionName
48+
* @returns {import('mongodb').Collection}
49+
*/
4750
collection(collectionName) {
51+
// @ts-ignore
4852
return this.db.collection(collectionName);
4953
}
5054
}

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codefresh-io/service-base",
3-
"version": "7.0.0",
3+
"version": "7.1.0",
44
"main": "index.js",
55
"description": "",
66
"bin": {
@@ -35,6 +35,7 @@
3535
"@codefresh-io/authenticated-entity": "^2.17.1",
3636
"@codefresh-io/cf-monitor": "^12.0.0",
3737
"@codefresh-io/cf-openapi": "^0.7.20",
38+
"@codefresh-io/cf-telemetry": "^1.0.2",
3839
"@codefresh-io/eventbus": "^2.0.0",
3940
"@codefresh-io/http-infra": "^1.8.15",
4041
"@codefresh-io/internal-service-config": "^1.0.3",
@@ -52,7 +53,7 @@
5253
"js-yaml": "^3.13.1",
5354
"lodash": "4.17.21",
5455
"method-override": "^3.0.0",
55-
"mongodb": "6.3.0",
56+
"mongodb": "^6.12.0",
5657
"morgan": "^1.9.1",
5758
"node-uuid": "^1.4.8",
5859
"proxyquire": "^1.8.0",
@@ -71,5 +72,6 @@
7172
"jest": "^29.7.0",
7273
"mocha": "^8.2.1",
7374
"mongodb-memory-server": "^9.1.6"
74-
}
75+
},
76+
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
7577
}

0 commit comments

Comments
 (0)