This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
mongodb.js
200 lines (184 loc) · 5.36 KB
/
mongodb.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
module.exports = {
monitor(mongodbRequirePath, logger) {
let mongodb, mongodbCore
try {
// for the v1 driver the methods to wrap are in the mongodb
// module in lib/mongodb/db.js
mongodb = require(mongodbRequirePath)
} catch (error) {}
try {
// for the v2 driver the relevant methods are in the mongodb-core
// module in lib/topologies/{server,replset,mongos}.js
const v2Path = mongodbRequirePath.replace(/\/mongodb$/, '/mongodb-core')
mongodbCore = require(v2Path)
} catch (error1) {}
const Metrics = require('./index')
const monitorMethod = function(base, method, type) {
let _method
if (base == null) {
return
}
if ((_method = base[method]) == null) {
return
}
const arglen = _method.length
const mongoDriverV1Wrapper = function(dbCommand, options, callback) {
let query
if (typeof callback === 'undefined') {
callback = options
options = {}
}
const collection = dbCommand.collectionName
if (collection.match(/\$cmd$/)) {
// Ignore noisy command methods like authenticating, ismaster and ping
return _method.call(this, dbCommand, options, callback)
}
if (dbCommand.query != null) {
query = Object.keys(dbCommand.query)
.sort()
.join('_')
}
const timer = new Metrics.Timer('mongo', { collection, query })
const start = new Date()
return _method.call(this, dbCommand, options, function() {
timer.done()
logger.log(
{
query: dbCommand.query,
query_type: type,
collection,
'response-time': new Date() - start
},
'mongo request'
)
return callback.apply(this, arguments)
})
}
const mongoDriverV2Wrapper = function(ns, ops, options, callback) {
let query
if (typeof callback === 'undefined') {
callback = options
options = {}
}
if (ns.match(/\$cmd$/)) {
// Ignore noisy command methods like authenticating, ismaster and ping
return _method.call(this, ns, ops, options, callback)
}
let key = `mongo-requests.${ns}.${type}`
if (ops[0].q != null) {
// ops[0].q
query = Object.keys(ops[0].q)
.sort()
.join('_')
key += '.' + query
}
const timer = new Metrics.Timer(key)
const start = new Date()
return _method.call(this, ns, ops, options, function() {
timer.done()
logger.log(
{
query: ops[0].q,
query_type: type,
collection: ns,
'response-time': new Date() - start
},
'mongo request'
)
return callback.apply(this, arguments)
})
}
if (arglen === 3) {
return (base[method] = mongoDriverV1Wrapper)
} else if (arglen === 4) {
return (base[method] = mongoDriverV2Wrapper)
}
}
monitorMethod(
mongodb != null ? mongodb.Db.prototype : undefined,
'_executeQueryCommand',
'query'
)
monitorMethod(
mongodb != null ? mongodb.Db.prototype : undefined,
'_executeRemoveCommand',
'remove'
)
monitorMethod(
mongodb != null ? mongodb.Db.prototype : undefined,
'_executeInsertCommand',
'insert'
)
monitorMethod(
mongodb != null ? mongodb.Db.prototype : undefined,
'_executeUpdateCommand',
'update'
)
monitorMethod(
mongodbCore != null ? mongodbCore.Server.prototype : undefined,
'command',
'command'
)
monitorMethod(
mongodbCore != null ? mongodbCore.Server.prototype : undefined,
'remove',
'remove'
)
monitorMethod(
mongodbCore != null ? mongodbCore.Server.prototype : undefined,
'insert',
'insert'
)
monitorMethod(
mongodbCore != null ? mongodbCore.Server.prototype : undefined,
'update',
'update'
)
monitorMethod(
mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
'command',
'command'
)
monitorMethod(
mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
'remove',
'remove'
)
monitorMethod(
mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
'insert',
'insert'
)
monitorMethod(
mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined,
'update',
'update'
)
monitorMethod(
mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
'command',
'command'
)
monitorMethod(
mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
'remove',
'remove'
)
monitorMethod(
mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
'insert',
'insert'
)
return monitorMethod(
mongodbCore != null ? mongodbCore.Mongos.prototype : undefined,
'update',
'update'
)
}
}