Skip to content

Commit 5a4f97a

Browse files
committed
add metrics for mongodb queries
1 parent c9f9bb6 commit 5a4f97a

5 files changed

Lines changed: 40 additions & 3 deletions

File tree

OpenFlow/src/DatabaseConnection.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Crypt } from "./Crypt";
77
import { Config } from "./Config";
88
import { TokenUser, Base, WellknownIds, Rights, NoderedUtil, mapFunc, finalizeFunc, reduceFunc, Ace, UpdateOneMessage, UpdateManyMessage, InsertOrUpdateOneMessage, Role, Rolemember, User } from "@openiap/openflow-api";
99
import { DBHelper } from "./DBHelper";
10+
import * as client from "prom-client";
1011
// tslint:disable-next-line: typedef
1112
const safeObjectID = (s: string | number | ObjectID) => ObjectID.isValid(s) ? new ObjectID(s) : null;
1213
const isoDatePattern = new RegExp(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/);
@@ -46,6 +47,29 @@ export class DatabaseConnection {
4647
this._dbname = dbname;
4748
this.mongodburl = mongodburl;
4849
}
50+
51+
public static mongodb_query = new client.Histogram({
52+
name: 'openflow_mongodb_query_seconds',
53+
help: 'Duration for mongodb queries microseconds',
54+
labelNames: ['collection'],
55+
buckets: [0.1, 0.3, 0.5, 0.7, 1, 3, 5, 7, 10]
56+
})
57+
public static mongodb_query_count = new client.Counter({
58+
name: 'openflow_mongodb_query_count',
59+
help: 'Total number of mongodb queues',
60+
labelNames: ["collection"]
61+
})
62+
public static mongodb_aggregate = new client.Histogram({
63+
name: 'openflow_mongodb_aggregate_seconds',
64+
help: 'Duration for mongodb queries microseconds',
65+
labelNames: ['collection'],
66+
buckets: [0.1, 0.3, 0.5, 0.7, 1, 3, 5, 7, 10]
67+
})
68+
public static mongodb_aggregate_count = new client.Counter({
69+
name: 'openflow_mongodb_aggregate_count',
70+
help: 'Total number of mongodb queues',
71+
labelNames: ["collection"]
72+
})
4973
static toArray(iterator): Promise<any[]> {
5074
return new Promise((resolve, reject) => {
5175
iterator.toArray((err, res) => {
@@ -375,6 +399,10 @@ export class DatabaseConnection {
375399
// }
376400
let arr: T[] = [];
377401

402+
403+
DatabaseConnection.mongodb_query_count.inc();
404+
DatabaseConnection.mongodb_query_count.labels(collectionname).inc();
405+
const end = DatabaseConnection.mongodb_query.startTimer();
378406
let _pipe = this.db.collection(collectionname).find(_query);
379407
if (projection != null) {
380408
_pipe = _pipe.project(projection);
@@ -384,6 +412,7 @@ export class DatabaseConnection {
384412
_pipe = _pipe.hint(myhint);
385413
}
386414
arr = await _pipe.toArray();
415+
end({ collection: collectionname });
387416
// if (projection != null) {
388417
// arr = await this.db.collection(collectionname).find(_query).project(projection).sort(mysort as any).limit(top).skip(skip).toArray();
389418
// } else {
@@ -493,7 +522,11 @@ export class DatabaseConnection {
493522
const options: CollectionAggregationOptions = {};
494523
options.hint = myhint;
495524
try {
525+
DatabaseConnection.mongodb_aggregate_count.inc();
526+
DatabaseConnection.mongodb_aggregate_count.labels(collectionname).inc();
527+
const end = DatabaseConnection.mongodb_aggregate.startTimer();
496528
const items: T[] = await this.db.collection(collectionname).aggregate(aggregates, options).toArray();
529+
end({ collection: collectionname });
497530
DatabaseConnection.traversejsondecode(items);
498531
const user: TokenUser = Crypt.verityToken(jwt);
499532
if (Config.log_aggregates) this._logger.debug("[" + user.username + "][" + collectionname + "] aggregate gave " + items.length + " results ");

OpenFlow/src/WebSocketServer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ export class WebSocketServer {
9494
if (!NoderedUtil.IsNullUndefinded(register)) register.registerMetric(WebSocketServer.websocket_rate_limit);
9595
if (!NoderedUtil.IsNullUndefinded(register)) register.registerMetric(WebSocketServer.websocket_messages);
9696
if (!NoderedUtil.IsNullUndefinded(register)) register.registerMetric(WebSocketServer.message_queue_count);
97+
if (!NoderedUtil.IsNullUndefinded(register)) register.registerMetric(DatabaseConnection.mongodb_query);
98+
if (!NoderedUtil.IsNullUndefinded(register)) register.registerMetric(DatabaseConnection.mongodb_query_count);
99+
if (!NoderedUtil.IsNullUndefinded(register)) register.registerMetric(DatabaseConnection.mongodb_aggregate);
100+
if (!NoderedUtil.IsNullUndefinded(register)) register.registerMetric(DatabaseConnection.mongodb_aggregate_count);
97101

98102

99103
setInterval(this.pingClients, 10000);

OpenFlowNodeRED/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openiap/nodered",
3-
"version": "1.1.163",
3+
"version": "1.1.164",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.163
1+
1.1.164

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openiap/openflow",
3-
"version": "1.1.163",
3+
"version": "1.1.164",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)