Compare commits

..

3 Commits

Author SHA1 Message Date
69a7016e97 v1.7.2 2023-11-11 17:52:08 +02:00
8a4d8c1d1b better help strings 2023-11-11 17:51:40 +02:00
8997d5e60d v1.7.1 2023-11-11 17:29:53 +02:00
3 changed files with 20 additions and 20 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@navy.gif/wrappers", "name": "@navy.gif/wrappers",
"version": "1.7.0", "version": "1.7.2",
"description": "Various wrapper classes I use in my projects", "description": "Various wrapper classes I use in my projects",
"repository": "https://git.corgi.wtf/Navy.gif/wrappers.git", "repository": "https://git.corgi.wtf/Navy.gif/wrappers.git",
"author": "Navy.gif", "author": "Navy.gif",

View File

@ -96,7 +96,7 @@ class MariaDB
#pool: PoolCluster | null; #pool: PoolCluster | null;
#nodes: Node[]; #nodes: Node[];
#canQueryDonor: boolean; #canQueryDonor: boolean;
#queryHistogram?: {startTimer: (labels: object) => () => void}; static #queryHistogram?: {startTimer: (labels: object) => () => void};
#server: IServer; #server: IServer;
constructor (server: IServer, options: MariaOptions) constructor (server: IServer, options: MariaOptions)
@ -241,16 +241,16 @@ class MariaDB
{ {
this.#logger.warn('Metrics recording was enabled but missing prom-client dependency'); 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.#logger.info('Setting up metric recording');
this.#queryHistogram = new Prometheus.Histogram({ MariaDB.#queryHistogram = new Prometheus.Histogram({
name: 'sql_queries', name: 'sql_queries',
help: 'Tracks query duration and frequency', help: 'Tracks query duration in seconds and frequency',
buckets: Prometheus?.exponentialBuckets(0.005, 2, 10), buckets: Prometheus?.exponentialBuckets(0.005, 2, 10),
labelNames: [ 'type' ] as const labelNames: [ 'type' ] as const
}); });
this.#server.registerMetric(this.#queryHistogram!); this.#server.registerMetric(MariaDB.#queryHistogram!);
} }
this.#_ready = true; this.#_ready = true;
@ -407,7 +407,7 @@ class MariaDB
{ {
const result = await new Promise<T[] | FieldInfo[] | [OkPacket]| undefined>((resolve, reject) => const result = await new Promise<T[] | FieldInfo[] | [OkPacket]| undefined>((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) => const q = connection.query({ timeout, sql: query }, values, (err, results, fields) =>
{ {
if (err) if (err)

View File

@ -55,7 +55,7 @@ class MongoDB
#db: Db | null; #db: Db | null;
#_client: MongoClient; #_client: MongoClient;
#queryHistogram?: { startTimer: (labels: object) => () => void }; static #queryHistogram?: { startTimer: (labels: object) => () => void };
#server: IServer; #server: IServer;
constructor (server: IServer, config: MongoOptions) constructor (server: IServer, config: MongoOptions)
@ -145,16 +145,16 @@ class MongoDB
{ {
this.#logger.warn('Metrics recording was enabled but missing prom-client dependency'); 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.#logger.info('Setting up metric recording');
this.#queryHistogram = new Prometheus.Histogram({ MongoDB.#queryHistogram = new Prometheus.Histogram({
name: 'mongo_queries', name: 'mongo_queries',
help: 'Tracks query duration and frequency', help: 'Tracks query duration in seconds and frequency',
buckets: Prometheus?.exponentialBuckets(0.005, 2, 10), buckets: Prometheus?.exponentialBuckets(0.005, 2, 10),
labelNames: [ 'type' ] as const labelNames: [ 'type' ] as const
}); });
this.#server.registerMetric(this.#queryHistogram!); this.#server.registerMetric(MongoDB.#queryHistogram!);
} }
this.#logger.status('MongoDB ready'); this.#logger.status('MongoDB ready');
@ -198,7 +198,7 @@ class MongoDB
this.#logger.debug(`Incoming find query for ${db} with parameters ${inspect(query)}`); 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<T>(db).find(query as Filter<T>, options); const cursor = this.#db.collection<T>(db).find(query as Filter<T>, options);
const data = await cursor.toArray(); const data = await cursor.toArray();
if (endTimer) if (endTimer)
@ -225,7 +225,7 @@ class MongoDB
query._id = new ObjectId(query._id); query._id = new ObjectId(query._id);
this.#logger.debug(`Incoming findOne query for ${db} with parameters ${inspect(query)}`); 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<T>(db).findOne(query as Filter<T>, options); const result = await this.#db.collection<T>(db).findOne(query as Filter<T>, options);
if (endTimer) if (endTimer)
endTimer(); endTimer();
@ -253,7 +253,7 @@ class MongoDB
filter._id = new ObjectId(filter._id); filter._id = new ObjectId(filter._id);
this.#logger.debug(`Incoming update query for '${db}' with parameters\n${inspect(filter)}\nand data\n${inspect(data)}`); 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<T>(db).updateMany(filter as Filter<T>, { $set: data }, { upsert }); const result = await this.#db.collection<T>(db).updateMany(filter as Filter<T>, { $set: data }, { upsert });
if (endTimer) if (endTimer)
endTimer(); endTimer();
@ -279,7 +279,7 @@ class MongoDB
filter._id = new ObjectId(filter._id); filter._id = new ObjectId(filter._id);
this.#logger.debug(`Incoming updateOne query for ${db} with parameters ${inspect(filter)}`); 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<Document>, { $set: data }, { upsert }); const result = await this.#db.collection(db).updateOne(filter as Filter<Document>, { $set: data }, { upsert });
if (endTimer) if (endTimer)
endTimer(); endTimer();
@ -305,7 +305,7 @@ class MongoDB
data._id = new ObjectId(data._id); data._id = new ObjectId(data._id);
this.#logger.debug(`Incoming insertOne query for ${db} with parameters ${inspect(data)}`); 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); const result = await this.#db.collection(db).insertOne(data);
if (endTimer) if (endTimer)
endTimer(); endTimer();
@ -322,7 +322,7 @@ class MongoDB
filter._id = new ObjectId(filter._id); filter._id = new ObjectId(filter._id);
this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`); 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); const result = await this.#db.collection(db).deleteOne(filter);
if (endTimer) if (endTimer)
endTimer(); endTimer();
@ -340,7 +340,7 @@ class MongoDB
if (typeof filter._id === 'string') if (typeof filter._id === 'string')
filter._id = new ObjectId(filter._id); 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<T>(db).findOneAndDelete(filter as Filter<T>, { includeResultMetadata: meta }); const result = await this.#db.collection<T>(db).findOneAndDelete(filter as Filter<T>, { includeResultMetadata: meta });
if (endTimer) if (endTimer)
endTimer(); endTimer();
@ -367,7 +367,7 @@ class MongoDB
filter._id = new ObjectId(filter._id); 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)}`); 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 }); const result = await this.#db.collection(db).updateOne(filter, { $push: data }, { upsert });
if (endTimer) if (endTimer)
endTimer(); endTimer();