This repository was archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmongo.js
100 lines (87 loc) · 3.42 KB
/
mongo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var shared = require('./shared');
module.exports = function (service) {
var pending = 0;
var doCount = function (conn, from, funneler, serviceName, thisService) {
conn.collection(serviceName, function (err, coll) {
pending++;
coll.count(function (err, count) {
pending--;
funneler({
'funnel': 'mongo',
'nodeName': from.replace(/^mongodb:\/\//, ''),
'serviceName': serviceName,
'metricName': 'count',
'reading': count
});
if (pending == 0) {
conn.close();
}
});
});
};
var doQuery = function (conn, from, funneler, serviceName, thisService) {
conn.collection(thisService.collection, function (err, coll) {
pending++;
coll.count(thisService.query, function (err, count) {
pending--;
funneler({
'funnel': 'mongo',
'nodeName': from.replace(/^mongodb:\/\//, ''),
'serviceName': serviceName,
'metricName': 'query',
'reading': count
});
if (pending == 0) {
conn.close();
}
});
});
};
var doAggregate = function (conn, from, funneler, serviceName, thisService) {
conn.collection(thisService.collection, function (err, coll) {
pending++;
coll.aggregate(thisService.aggregate, function (err, result) {
pending--;
if (result) {
funneler({
'funnel': 'mongo',
'nodeName': from.replace(/^mongodb:\/\//, ''),
'serviceName': serviceName,
'metricName': 'aggregate',
'reading': thisService.processor(result),
});
}
if (pending == 0) {
conn.close();
}
});
});
};
return function(funneler) {
if (typeof service.from == 'string') {
service.from = [service.from];
}
var mongodb = require('mongodb');
service.from.forEach(function (from) {
mongodb.connect(from, function(err, conn) {
for (var serviceName in service.services) {
(function (thisService) {
var funnelerWrapper = function (data) {
if (thisService.metricName) {
data.explicitMetricName = thisService.metricName;
}
funneler(data);
};
if (thisService === shared.COUNT || thisService.count) {
doCount(conn, from, funnelerWrapper, serviceName, thisService);
} else if (thisService.query) {
doQuery(conn, from, funnelerWrapper, serviceName, thisService);
} else if (thisService.aggregate) {
doAggregate(conn, from, funnelerWrapper, serviceName, thisService);
}
})(service.services[serviceName]);
}
});
});
};
};