-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathmongodb-core.js
152 lines (127 loc) · 5.23 KB
/
mongodb-core.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
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict'
var semver = require('semver')
const { getDBDestination } = require('../context')
var shimmer = require('../shimmer')
var SERVER_FNS = ['insert', 'update', 'remove', 'auth']
var CURSOR_FNS_FIRST = ['next', '_getmore']
const firstSpan = Symbol('first-span')
module.exports = function (mongodb, agent, { version, enabled }) {
if (!enabled) return mongodb
if (!semver.satisfies(version, '>=1.2.19 <4')) {
agent.logger.debug('mongodb-core version %s not supported - aborting...', version)
return mongodb
}
const ins = agent._instrumentation
if (mongodb.Server) {
agent.logger.debug('shimming mongodb-core.Server.prototype.command')
shimmer.wrap(mongodb.Server.prototype, 'command', wrapCommand)
agent.logger.debug('shimming mongodb-core.Server.prototype functions: %j', SERVER_FNS)
shimmer.massWrap(mongodb.Server.prototype, SERVER_FNS, wrapQuery)
}
if (mongodb.Cursor) {
agent.logger.debug('shimming mongodb-core.Cursor.prototype functions: %j', CURSOR_FNS_FIRST)
shimmer.massWrap(mongodb.Cursor.prototype, CURSOR_FNS_FIRST, wrapCursor)
}
return mongodb
function wrapCommand (orig) {
return function wrappedFunction (ns, cmd) {
var trans = agent._instrumentation.currTransaction()
var id = trans && trans.id
var span
agent.logger.debug('intercepted call to mongodb-core.Server.prototype.command %o', { id, ns })
if (trans && arguments.length > 0) {
var index = arguments.length - 1
var cb = arguments[index]
if (typeof cb === 'function') {
var type
if (cmd.findAndModify) type = 'findAndModify'
else if (cmd.createIndexes) type = 'createIndexes'
else if (cmd.ismaster) type = 'ismaster'
else if (cmd.count) type = 'count'
else type = 'command'
span = ins.createSpan(ns + '.' + type, 'db', 'mongodb', 'query', { exitSpan: true })
if (span) {
span.setDbContext({ type: 'mongodb', instance: ns })
arguments[index] = ins.bindFunctionToRunContext(ins.currRunContext(), wrappedCallback)
}
}
}
return orig.apply(this, arguments)
function wrappedCallback (_err, commandResult) {
agent.logger.debug('intercepted mongodb-core.Server.prototype.command callback %o', { id })
if (commandResult && commandResult.connection) {
span._setDestinationContext(getDBDestination(
commandResult.connection.host, commandResult.connection.port))
}
span.end()
return cb.apply(this, arguments)
}
}
}
function wrapQuery (orig, name) {
return function wrappedFunction (ns) {
var trans = agent._instrumentation.currTransaction()
var id = trans && trans.id
var span
agent.logger.debug('intercepted call to mongodb-core.Server.prototype.%s %o', name, { id, ns })
if (trans && arguments.length > 0) {
var index = arguments.length - 1
var cb = arguments[index]
if (typeof cb === 'function') {
span = ins.createSpan(ns + '.' + name, 'db', 'mongodb', 'query', { exitSpan: true })
if (span) {
span.setDbContext({ type: 'mongodb', instance: ns })
arguments[index] = ins.bindFunctionToRunContext(ins.currRunContext(), wrappedCallback)
}
}
}
return orig.apply(this, arguments)
function wrappedCallback (_err, commandResult) {
agent.logger.debug('intercepted mongodb-core.Server.prototype.%s callback %o', name, { id })
if (commandResult && commandResult.connection) {
span._setDestinationContext(getDBDestination(
commandResult.connection.host, commandResult.connection.port))
}
span.end()
return cb.apply(this, arguments)
}
}
}
function wrapCursor (orig, name) {
return function wrappedFunction () {
var trans = agent._instrumentation.currTransaction()
var id = trans && trans.id
var span
agent.logger.debug('intercepted call to mongodb-core.Cursor.prototype.%s %o', name, { id })
if (trans && arguments.length > 0) {
var cb = arguments[0]
if (typeof cb === 'function') {
if (name !== 'next' || !this[firstSpan]) {
var spanName = `${this.ns}.${this.cmd.find ? 'find' : name}`
span = ins.createSpan(spanName, 'db', 'mongodb', 'query', { exitSpan: true })
}
if (span) {
span.setDbContext({ type: 'mongodb', instance: this.ns })
// Limitation: Currently not getting destination address/port for
// cursor calls.
arguments[0] = ins.bindFunctionToRunContext(ins.currRunContext(), wrappedCallback)
if (name === 'next') {
this[firstSpan] = true
}
}
}
}
return orig.apply(this, arguments)
function wrappedCallback () {
agent.logger.debug('intercepted mongodb-core.Cursor.prototype.%s callback %o', name, { id })
span.end()
return cb.apply(this, arguments)
}
}
}
}