diff --git a/src/MariaDB.ts b/src/MariaDB.ts index af29806..7aedabd 100644 --- a/src/MariaDB.ts +++ b/src/MariaDB.ts @@ -96,7 +96,7 @@ class MariaDB #pool: PoolCluster | null; #nodes: Node[]; #canQueryDonor: boolean; - #queryHistogram?: {startTimer: (labels: object) => () => void}; + static #queryHistogram?: {startTimer: (labels: object) => () => void}; #server: IServer; constructor (server: IServer, options: MariaOptions) @@ -241,16 +241,16 @@ class MariaDB { this.#logger.warn('Metrics recording was enabled but missing prom-client dependency'); } - else if (this.#config.recordMetrics && Prometheus) + else if (this.#config.recordMetrics && Prometheus && !MariaDB.#queryHistogram) { this.#logger.info('Setting up metric recording'); - this.#queryHistogram = new Prometheus.Histogram({ + MariaDB.#queryHistogram = new Prometheus.Histogram({ name: 'sql_queries', - help: 'Tracks query duration and frequency', + help: 'Tracks query duration in seconds and frequency', buckets: Prometheus?.exponentialBuckets(0.005, 2, 10), labelNames: [ 'type' ] as const }); - this.#server.registerMetric(this.#queryHistogram!); + this.#server.registerMetric(MariaDB.#queryHistogram!); } this.#_ready = true; @@ -407,7 +407,7 @@ class MariaDB { const result = await new Promise((resolve, reject) => { - const endTimer = this.#queryHistogram?.startTimer({ type: [ 'SELECT', 'UPDATE', 'INSERT', 'DELETE' ].find(entry => query.toUpperCase().includes(entry)) ?? 'OTHER' }); + const endTimer = MariaDB.#queryHistogram?.startTimer({ type: [ 'SELECT', 'UPDATE', 'INSERT', 'DELETE' ].find(entry => query.toUpperCase().includes(entry)) ?? 'OTHER' }); const q = connection.query({ timeout, sql: query }, values, (err, results, fields) => { if (err) diff --git a/src/MongoDB.ts b/src/MongoDB.ts index 623e9da..5f0742f 100644 --- a/src/MongoDB.ts +++ b/src/MongoDB.ts @@ -55,7 +55,7 @@ class MongoDB #db: Db | null; #_client: MongoClient; - #queryHistogram?: { startTimer: (labels: object) => () => void }; + static #queryHistogram?: { startTimer: (labels: object) => () => void }; #server: IServer; constructor (server: IServer, config: MongoOptions) @@ -145,16 +145,16 @@ class MongoDB { this.#logger.warn('Metrics recording was enabled but missing prom-client dependency'); } - else if (this.#config.recordMetrics && Prometheus) + else if (this.#config.recordMetrics && Prometheus && !MongoDB.#queryHistogram) { this.#logger.info('Setting up metric recording'); - this.#queryHistogram = new Prometheus.Histogram({ + MongoDB.#queryHistogram = new Prometheus.Histogram({ name: 'mongo_queries', - help: 'Tracks query duration and frequency', + help: 'Tracks query duration in seconds and frequency', buckets: Prometheus?.exponentialBuckets(0.005, 2, 10), labelNames: [ 'type' ] as const }); - this.#server.registerMetric(this.#queryHistogram!); + this.#server.registerMetric(MongoDB.#queryHistogram!); } this.#logger.status('MongoDB ready'); @@ -198,7 +198,7 @@ class MongoDB this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`); - const endTimer = this.#queryHistogram?.startTimer({ type: 'find' }); + const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'find' }); const cursor = this.#db.collection(db).find(query as Filter, options); const data = await cursor.toArray(); if (endTimer) @@ -225,7 +225,7 @@ class MongoDB query._id = new ObjectId(query._id); this.#logger.debug(`Incoming findOne query for ${db} with parameters ${inspect(query)}`); - const endTimer = this.#queryHistogram?.startTimer({ type: 'findOne' }); + const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'findOne' }); const result = await this.#db.collection(db).findOne(query as Filter, options); if (endTimer) endTimer(); @@ -253,7 +253,7 @@ class MongoDB filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming update query for '${db}' with parameters\n${inspect(filter)}\nand data\n${inspect(data)}`); - const endTimer = this.#queryHistogram?.startTimer({ type: 'updateMany' }); + const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'updateMany' }); const result = await this.#db.collection(db).updateMany(filter as Filter, { $set: data }, { upsert }); if (endTimer) endTimer(); @@ -279,7 +279,7 @@ class MongoDB filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming updateOne query for ${db} with parameters ${inspect(filter)}`); - const endTimer = this.#queryHistogram?.startTimer({ type: 'updateOne' }); + const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'updateOne' }); const result = await this.#db.collection(db).updateOne(filter as Filter, { $set: data }, { upsert }); if (endTimer) endTimer(); @@ -305,7 +305,7 @@ class MongoDB data._id = new ObjectId(data._id); this.#logger.debug(`Incoming insertOne query for ${db} with parameters ${inspect(data)}`); - const endTimer = this.#queryHistogram?.startTimer({ type: 'insertOne' }); + const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'insertOne' }); const result = await this.#db.collection(db).insertOne(data); if (endTimer) endTimer(); @@ -322,7 +322,7 @@ class MongoDB filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`); - const endTimer = this.#queryHistogram?.startTimer({ type: 'deleteOne' }); + const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'deleteOne' }); const result = await this.#db.collection(db).deleteOne(filter); if (endTimer) endTimer(); @@ -340,7 +340,7 @@ class MongoDB if (typeof filter._id === 'string') filter._id = new ObjectId(filter._id); - const endTimer = this.#queryHistogram?.startTimer({ type: 'findOneAndDelete' }); + const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'findOneAndDelete' }); const result = await this.#db.collection(db).findOneAndDelete(filter as Filter, { includeResultMetadata: meta }); if (endTimer) endTimer(); @@ -367,7 +367,7 @@ class MongoDB filter._id = new ObjectId(filter._id); this.#logger.debug(`Incoming push query for ${db}, with upsert ${upsert} and with parameters ${inspect(filter)} and data ${inspect(data)}`); - const endTimer = this.#queryHistogram?.startTimer({ type: 'push' }); + const endTimer = MongoDB.#queryHistogram?.startTimer({ type: 'push' }); const result = await this.#db.collection(db).updateOne(filter, { $push: data }, { upsert }); if (endTimer) endTimer();