newrelic 7.5.0 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NEWS.md +67 -0
- package/THIRD_PARTY_NOTICES.md +35 -82
- package/bin/test-naming-rules.js +0 -1
- package/bin/tracetractor +0 -1
- package/index.js +6 -5
- package/lib/agent.js +51 -31
- package/lib/collector/api.js +2 -3
- package/lib/config/index.js +7 -15
- package/lib/feature_flags.js +2 -4
- package/lib/instrumentation/fastify/spec-builders.js +5 -0
- package/lib/instrumentation/fastify.js +5 -0
- package/lib/instrumentation/mongodb/common.js +186 -0
- package/lib/instrumentation/mongodb/constants.js +97 -0
- package/lib/instrumentation/mongodb/v2-mongo.js +108 -0
- package/lib/instrumentation/mongodb/v3-mongo.js +61 -0
- package/lib/instrumentation/mongodb/v4-mongo.js +96 -0
- package/lib/instrumentation/mongodb.js +17 -310
- package/lib/instrumentation/pg.js +35 -16
- package/lib/spans/create-span-event-aggregator.js +23 -70
- package/package.json +26 -19
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict'
|
|
7
|
+
const {
|
|
8
|
+
CURSOR_OPS,
|
|
9
|
+
COLLECTION_OPS,
|
|
10
|
+
DB_OPS
|
|
11
|
+
} = require('./constants')
|
|
12
|
+
const { URL } = require('url')
|
|
13
|
+
|
|
14
|
+
const common = module.exports
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Instruments all methods from constants.CURSOR_OPS on a given
|
|
18
|
+
* cursor class
|
|
19
|
+
*
|
|
20
|
+
* @param {Shim} shim
|
|
21
|
+
* @param {Cursor} Cursor
|
|
22
|
+
*/
|
|
23
|
+
common.instrumentCursor = function instrumentCursor(shim, Cursor) {
|
|
24
|
+
if (Cursor && Cursor.prototype) {
|
|
25
|
+
const proto = Cursor.prototype
|
|
26
|
+
for (let i = 0; i < CURSOR_OPS.length; i++) {
|
|
27
|
+
shim.recordQuery(proto, CURSOR_OPS[i], common.makeQueryDescFunc(shim, CURSOR_OPS[i]))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
shim.recordQuery(proto, 'each', common.makeQueryDescFunc(shim, 'each'))
|
|
31
|
+
shim.recordOperation(proto, 'pipe')
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Instruments all methods from constants.COLLECTION_OPS on
|
|
37
|
+
* the Collection class
|
|
38
|
+
*
|
|
39
|
+
* @param {Shim} shim
|
|
40
|
+
* @param {Collection} Collection
|
|
41
|
+
*/
|
|
42
|
+
common.instrumentCollection = function instrumentCollection(shim, Collection) {
|
|
43
|
+
if (Collection && Collection.prototype) {
|
|
44
|
+
const proto = Collection.prototype
|
|
45
|
+
for (let i = 0; i < COLLECTION_OPS.length; i++) {
|
|
46
|
+
shim.recordQuery(
|
|
47
|
+
proto,
|
|
48
|
+
COLLECTION_OPS[i],
|
|
49
|
+
common.makeQueryDescFunc(shim, COLLECTION_OPS[i])
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Instruments all methods from constants.DB_OPS on
|
|
57
|
+
* the Db class.
|
|
58
|
+
*
|
|
59
|
+
* @param {Object} params
|
|
60
|
+
* @param {Shim} params.shim
|
|
61
|
+
* @param {Db} params.Db
|
|
62
|
+
*/
|
|
63
|
+
common.instrumentDb = function instrumentDb(shim, Db) {
|
|
64
|
+
if (Db && Db.prototype) {
|
|
65
|
+
const proto = Db.prototype
|
|
66
|
+
shim.recordOperation(proto, DB_OPS, {callback: shim.LAST, opaque: true })
|
|
67
|
+
// link to client.connect(removed in v4.0)
|
|
68
|
+
shim.recordOperation(Db, 'connect', {callback: shim.LAST})
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Sets up the desc for all instrumented query methods
|
|
74
|
+
* @param {Shim} shim
|
|
75
|
+
* @param {string} methodName
|
|
76
|
+
*/
|
|
77
|
+
common.makeQueryDescFunc = function makeQueryDescFunc(shim, methodName) {
|
|
78
|
+
if (methodName === 'each') {
|
|
79
|
+
return function eachDescFunc() {
|
|
80
|
+
const parameters = getInstanceAttributeParameters(shim, this)
|
|
81
|
+
return {query: methodName, parameters, rowCallback: shim.LAST, opaque: true }
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return function queryDescFunc() {
|
|
86
|
+
// segment name does not actually use query string
|
|
87
|
+
// method name is set as query so the query parser has access to the op name
|
|
88
|
+
const parameters = getInstanceAttributeParameters(shim, this)
|
|
89
|
+
return {query: methodName, parameters, promise: true, callback: shim.LAST, opaque: true}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Sets up a listener for `started` on instrumenter(mongo APM). This applies to
|
|
95
|
+
* mongo <4. The listener adds the following attributes to the active segment:
|
|
96
|
+
* host, port_path_or_id, and database_name
|
|
97
|
+
*
|
|
98
|
+
* @param {Shim} shim
|
|
99
|
+
* @param {Instrumentation} instrumenter instance of mongo APM class
|
|
100
|
+
*/
|
|
101
|
+
common.captureAttributesOnStarted = function captureAttributesOnStarted(shim, instrumenter) {
|
|
102
|
+
instrumenter.on('started', function onMongoEventStarted(evnt) {
|
|
103
|
+
// This assumes that this `started` event is fired _after_ our wrapper
|
|
104
|
+
// starts and creates the segment. We perform a check of the segment name
|
|
105
|
+
// out of an excess of caution.
|
|
106
|
+
const connId = evnt.connectionId
|
|
107
|
+
if (connId) {
|
|
108
|
+
// Mongo sticks the path to the domain socket in the "host" slot, but we
|
|
109
|
+
// want it in the "port", so if we have a domain socket we need to change
|
|
110
|
+
// the order of our parameters.
|
|
111
|
+
if (typeof connId === 'string') {
|
|
112
|
+
const parts = connId.split(':')
|
|
113
|
+
if (parts.length && parts[0][0] === '/') {
|
|
114
|
+
shim.captureInstanceAttributes('localhost', parts[0], evnt.databaseName)
|
|
115
|
+
} else {
|
|
116
|
+
shim.captureInstanceAttributes(parts[0], parts[1], evnt.databaseName)
|
|
117
|
+
}
|
|
118
|
+
} else if (connId.domainSocket) {
|
|
119
|
+
shim.captureInstanceAttributes('localhost', connId.host, evnt.databaseName)
|
|
120
|
+
} else {
|
|
121
|
+
shim.captureInstanceAttributes(connId.host, connId.port, evnt.databaseName)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Get the database_name, host, port_path_or_id
|
|
129
|
+
* for the query segment. v4 refactored where the toplogy is stored.
|
|
130
|
+
* You can now get the details via the client obj that's deeply nested
|
|
131
|
+
* See: https://github.com/mongodb/node-mongodb-native/pull/2594/files#diff-1d214e57ddda9095d296e5700ebce701333bfefcf417e234c584d14091b2f50dR168
|
|
132
|
+
*/
|
|
133
|
+
function getInstanceAttributeParameters(shim, obj) {
|
|
134
|
+
if (obj.s && obj.s.db && obj.s.topology) {
|
|
135
|
+
shim.logger.trace(
|
|
136
|
+
'Adding datastore instance attributes from obj.s.db + obj.s.topology'
|
|
137
|
+
)
|
|
138
|
+
const databaseName = obj.s.db.databaseName || null
|
|
139
|
+
const topology = obj.s.topology
|
|
140
|
+
if (topology.s && topology.s.options) {
|
|
141
|
+
return doCapture(topology.s.options, databaseName)
|
|
142
|
+
}
|
|
143
|
+
} else if (obj.s && obj.s.db && obj.s.db.s && obj.s.db.s.client &&
|
|
144
|
+
obj.s.db.s.client.s && obj.s.db.s.client.s.url) {
|
|
145
|
+
let { hostname: host, port: port_path_or_id } = new URL(obj.s.db.s.client.s.url)
|
|
146
|
+
if (host.endsWith('.sock')) {
|
|
147
|
+
// socket path is uri encoded
|
|
148
|
+
// decode it to properly name attrs
|
|
149
|
+
port_path_or_id = decodeURIComponent(host)
|
|
150
|
+
host = 'localhost'
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
host,
|
|
154
|
+
port_path_or_id,
|
|
155
|
+
database_name: obj.s.db.databaseName
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
shim.logger.trace('Could not find datastore instance attributes.')
|
|
160
|
+
return {
|
|
161
|
+
host: null,
|
|
162
|
+
port_path_or_id: null,
|
|
163
|
+
database_name: null
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function doCapture(conf, database) {
|
|
167
|
+
let host = conf.host
|
|
168
|
+
let port = conf.port
|
|
169
|
+
|
|
170
|
+
// If using a domain socket, mongo stores the path as the host name, but we
|
|
171
|
+
// pass it through the port value.
|
|
172
|
+
if (
|
|
173
|
+
(conf.socketOptions && conf.socketOptions.domainSocket) ||
|
|
174
|
+
/\.sock$/.test(host)
|
|
175
|
+
) {
|
|
176
|
+
port = host
|
|
177
|
+
host = 'localhost'
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
host: host,
|
|
182
|
+
port_path_or_id: port,
|
|
183
|
+
database_name: database
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict'
|
|
7
|
+
|
|
8
|
+
const DB_OPS = [
|
|
9
|
+
'addUser',
|
|
10
|
+
'authenticate',
|
|
11
|
+
'collection',
|
|
12
|
+
'collectionNames',
|
|
13
|
+
'collections',
|
|
14
|
+
'command',
|
|
15
|
+
'createCollection',
|
|
16
|
+
'createIndex',
|
|
17
|
+
'cursorInfo',
|
|
18
|
+
'dereference',
|
|
19
|
+
'dropCollection',
|
|
20
|
+
'dropDatabase',
|
|
21
|
+
'dropIndex',
|
|
22
|
+
'ensureIndex',
|
|
23
|
+
'eval',
|
|
24
|
+
'executeDbAdminCommand',
|
|
25
|
+
'indexInformation',
|
|
26
|
+
'logout',
|
|
27
|
+
'open',
|
|
28
|
+
'reIndex',
|
|
29
|
+
'removeUser',
|
|
30
|
+
'renameCollection',
|
|
31
|
+
'stats',
|
|
32
|
+
'_executeInsertCommand',
|
|
33
|
+
'_executeQueryCommand'
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
const COLLECTION_OPS = [
|
|
37
|
+
'aggregate',
|
|
38
|
+
'bulkWrite',
|
|
39
|
+
'count',
|
|
40
|
+
'createIndex',
|
|
41
|
+
'deleteMany',
|
|
42
|
+
'deleteOne',
|
|
43
|
+
'distinct',
|
|
44
|
+
'drop',
|
|
45
|
+
'dropAllIndexes',
|
|
46
|
+
'dropIndex',
|
|
47
|
+
'ensureIndex',
|
|
48
|
+
'findAndModify',
|
|
49
|
+
'findAndRemove',
|
|
50
|
+
'findOne',
|
|
51
|
+
'findOneAndDelete',
|
|
52
|
+
'findOneAndReplace',
|
|
53
|
+
'findOneAndUpdate',
|
|
54
|
+
'geoHaystackSearch',
|
|
55
|
+
'geoNear',
|
|
56
|
+
'group',
|
|
57
|
+
'indexes',
|
|
58
|
+
'indexExists',
|
|
59
|
+
'indexInformation',
|
|
60
|
+
'insert',
|
|
61
|
+
'insertMany',
|
|
62
|
+
'insertOne',
|
|
63
|
+
'isCapped',
|
|
64
|
+
'mapReduce',
|
|
65
|
+
'options',
|
|
66
|
+
'parallelCollectionScan',
|
|
67
|
+
'reIndex',
|
|
68
|
+
'remove',
|
|
69
|
+
'rename',
|
|
70
|
+
'replaceOne',
|
|
71
|
+
'save',
|
|
72
|
+
'stats',
|
|
73
|
+
'update',
|
|
74
|
+
'updateMany',
|
|
75
|
+
'updateOne'
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
const GRID_OPS = [
|
|
79
|
+
'put',
|
|
80
|
+
'get',
|
|
81
|
+
'delete'
|
|
82
|
+
]
|
|
83
|
+
|
|
84
|
+
const CURSOR_OPS = [
|
|
85
|
+
'nextObject',
|
|
86
|
+
'next',
|
|
87
|
+
'toArray',
|
|
88
|
+
'count',
|
|
89
|
+
'explain'
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
module.exports = {
|
|
93
|
+
COLLECTION_OPS,
|
|
94
|
+
CURSOR_OPS,
|
|
95
|
+
DB_OPS,
|
|
96
|
+
GRID_OPS
|
|
97
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict'
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
captureAttributesOnStarted,
|
|
10
|
+
makeQueryDescFunc
|
|
11
|
+
} = require('./common')
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* parser used to grab the collection and operation
|
|
15
|
+
* from a running query
|
|
16
|
+
*
|
|
17
|
+
* @param {Object} operation
|
|
18
|
+
*/
|
|
19
|
+
function queryParser(operation) {
|
|
20
|
+
let collection = this.collectionName || 'unknown'
|
|
21
|
+
if (this.ns) {
|
|
22
|
+
collection = this.ns.split(/\./)[1] || collection
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return {operation, collection}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Registers relevant instrumentation for mongo <= 3.0.6
|
|
29
|
+
* and >= 2. This relies on the built-in "APM" hook points
|
|
30
|
+
* to instrument their provided objects as well as sets
|
|
31
|
+
* up a listener for when commands start to properly
|
|
32
|
+
* add necessary attributes to segments
|
|
33
|
+
*
|
|
34
|
+
* @param {Shim} shim
|
|
35
|
+
* @param {Object} mongodb resolved package
|
|
36
|
+
*/
|
|
37
|
+
module.exports = function instrument(shim, mongodb) {
|
|
38
|
+
shim.setParser(queryParser)
|
|
39
|
+
|
|
40
|
+
const recordDesc = {
|
|
41
|
+
'Gridstore': {isQuery: false, makeDescFunc: function makeGridDesc(opName) {
|
|
42
|
+
return {name:'GridFS-' + opName, callback: shim.LAST}
|
|
43
|
+
}},
|
|
44
|
+
'OrderedBulkOperation': {isQuery: true, makeDescFunc: makeQueryDescFunc},
|
|
45
|
+
'UnorderedBulkOperation': {isQuery: true, makeDescFunc: makeQueryDescFunc},
|
|
46
|
+
'CommandCursor': {isQuery: true, makeDescFunc: makeQueryDescFunc},
|
|
47
|
+
'AggregationCursor': {isQuery: true, makeDescFunc: makeQueryDescFunc},
|
|
48
|
+
'Cursor': {isQuery: true, makeDescFunc: makeQueryDescFunc},
|
|
49
|
+
'Collection': {isQuery: true, makeDescFunc: makeQueryDescFunc},
|
|
50
|
+
'Db': {isQuery: false, makeDescFunc: function makeDbDesc() {
|
|
51
|
+
return {callback: shim.LAST}
|
|
52
|
+
}}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// instrument using the apm api
|
|
56
|
+
const instrumenter = mongodb.instrument(Object.create(null), instrumentModules)
|
|
57
|
+
captureAttributesOnStarted(shim, instrumenter)
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Every module groups instrumentations by their
|
|
61
|
+
* promise, callback, return permutations
|
|
62
|
+
* Iterate over permutations and properly
|
|
63
|
+
* wrap depending on the `recordDesc` above
|
|
64
|
+
* See: https://github.com/mongodb/node-mongodb-native/blob/v3.0.5/lib/collection.js#L384
|
|
65
|
+
*/
|
|
66
|
+
function instrumentModules(_, modules) {
|
|
67
|
+
modules.forEach((module) => {
|
|
68
|
+
const { obj, instrumentations, name } = module
|
|
69
|
+
instrumentations.forEach((meta) => {
|
|
70
|
+
applyInstrumentation(name, obj, meta)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Iterate over methods on object and lookup in `recordDesc` to decide
|
|
77
|
+
* if it needs to be wrapped as an operation or query
|
|
78
|
+
*
|
|
79
|
+
* @param {string} objectName name of class getting instrumented
|
|
80
|
+
* @param {Object} object reference to the class getting instrumented
|
|
81
|
+
* @param {Define} meta describes the methods and if they are callbacks
|
|
82
|
+
* promises, and return values
|
|
83
|
+
*/
|
|
84
|
+
function applyInstrumentation(objectName, object, meta) {
|
|
85
|
+
const { methods, options } = meta
|
|
86
|
+
if (options.callback) {
|
|
87
|
+
methods.forEach((method) => {
|
|
88
|
+
const { isQuery, makeDescFunc } = recordDesc[objectName]
|
|
89
|
+
const proto = object.prototype
|
|
90
|
+
if (isQuery) {
|
|
91
|
+
shim.recordQuery(proto, method, makeDescFunc(shim, method))
|
|
92
|
+
} else if (isQuery === false) { // could be unset
|
|
93
|
+
shim.recordOperation(proto, method, makeDescFunc(shim, method))
|
|
94
|
+
} else {
|
|
95
|
+
shim.logger.trace('No wrapping method found for %s', objectName)
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// the cursor object implements Readable stream and internally calls nextObject on
|
|
101
|
+
// each read, in which case we do not want to record each nextObject() call
|
|
102
|
+
if (/Cursor$/.test(objectName)) {
|
|
103
|
+
shim.recordOperation(object.prototype, 'pipe')
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict'
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
captureAttributesOnStarted,
|
|
10
|
+
instrumentCollection,
|
|
11
|
+
instrumentCursor,
|
|
12
|
+
instrumentDb
|
|
13
|
+
} = require('./common')
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* parser used to grab the collection and operation
|
|
17
|
+
* on every mongo operation
|
|
18
|
+
*
|
|
19
|
+
* @param {Object} operation
|
|
20
|
+
*/
|
|
21
|
+
function queryParser(operation) {
|
|
22
|
+
let collection = this.collectionName || 'unknown'
|
|
23
|
+
// in v3.3.0 aggregate commands added the collection
|
|
24
|
+
// to target
|
|
25
|
+
if (this.operation && this.operation.target) {
|
|
26
|
+
collection = this.operation.target
|
|
27
|
+
} else if (this.ns) {
|
|
28
|
+
collection = this.ns.split(/\./)[1] || collection
|
|
29
|
+
}
|
|
30
|
+
return {operation, collection}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Registers relevant instrumentation for mongo >= 3.0.6
|
|
35
|
+
* In 3.0.6 they refactored their "APM" module which removed
|
|
36
|
+
* a lot of niceities around instrumentation classes.
|
|
37
|
+
* see: https://github.com/mongodb/node-mongodb-native/pull/1675/files
|
|
38
|
+
* This reverts back to instrumenting pre-canned methods on classes
|
|
39
|
+
* as well as sets up a listener for when commands start to properly
|
|
40
|
+
* add necessary attributes to segments
|
|
41
|
+
*
|
|
42
|
+
* @param {Shim} shim
|
|
43
|
+
* @param {Object} mongodb resolved package
|
|
44
|
+
*/
|
|
45
|
+
module.exports = function instrument(shim, mongodb) {
|
|
46
|
+
shim.setParser(queryParser)
|
|
47
|
+
const instrumenter = mongodb.instrument(Object.create(null), () => {})
|
|
48
|
+
captureAttributesOnStarted(shim, instrumenter)
|
|
49
|
+
instrumentCursor(shim, mongodb.Cursor)
|
|
50
|
+
instrumentCursor(shim, shim.require('./lib/aggregation_cursor'))
|
|
51
|
+
instrumentCursor(shim, shim.require('./lib/command_cursor'))
|
|
52
|
+
instrumentCollection(shim, mongodb.Collection)
|
|
53
|
+
instrumentDb(shim, mongodb.Db)
|
|
54
|
+
|
|
55
|
+
// calling instrument sets up listeners for a few events
|
|
56
|
+
// we should restore this on unload to avoid leaking
|
|
57
|
+
// event emitters
|
|
58
|
+
shim.agent.once('unload', function uninstrumentMongo() {
|
|
59
|
+
instrumenter.uninstrument()
|
|
60
|
+
})
|
|
61
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2021 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict'
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
instrumentCollection,
|
|
10
|
+
instrumentCursor,
|
|
11
|
+
instrumentDb
|
|
12
|
+
} = require('./common')
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* parser used to grab the collection and operation
|
|
16
|
+
* from a running query
|
|
17
|
+
*
|
|
18
|
+
* @param {Object} operation
|
|
19
|
+
*/
|
|
20
|
+
function queryParser(operation) {
|
|
21
|
+
let collection = this.collectionName || 'unknown'
|
|
22
|
+
|
|
23
|
+
// cursor methods have collection on namespace.collection
|
|
24
|
+
if (this.namespace && this.namespace.collection) {
|
|
25
|
+
collection = this.namespace.collection
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {operation, collection}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `commandStarted` handler used to
|
|
33
|
+
* update host, port and database_name
|
|
34
|
+
* on segment attributes
|
|
35
|
+
*
|
|
36
|
+
* @param {Shim} shim
|
|
37
|
+
* @param {CommandStartedEvent} evnt
|
|
38
|
+
*/
|
|
39
|
+
function cmdStartedHandler(shim, evnt) {
|
|
40
|
+
if (evnt.connectionId) {
|
|
41
|
+
let [host,port] = evnt.address.split(':')
|
|
42
|
+
|
|
43
|
+
// connection via socket get port from client.url
|
|
44
|
+
// which looks like "mongodb://%2Ftmp%2Fmongodb-27017.sock"
|
|
45
|
+
if (['undefined'].includes(host, port)) {
|
|
46
|
+
host = 'localhost'
|
|
47
|
+
// socket path is uri encoded
|
|
48
|
+
// decode it to properly name attrs
|
|
49
|
+
const socketPath = this.s.url.split(':')
|
|
50
|
+
if (socketPath.length && socketPath[1].startsWith('//')) {
|
|
51
|
+
port = decodeURIComponent(socketPath[1].substr(2))
|
|
52
|
+
}
|
|
53
|
+
} else if (host === '127.0.0.1') {
|
|
54
|
+
host = 'localhost'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
shim.captureInstanceAttributes(host, port, evnt.databaseName)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* function executed when client.connect is called
|
|
63
|
+
* enable APM(monitorCommands) and add the
|
|
64
|
+
* `commandStarted` listener
|
|
65
|
+
*
|
|
66
|
+
* @param {Shim} shim
|
|
67
|
+
*/
|
|
68
|
+
function wrapConnect(shim) {
|
|
69
|
+
this.monitorCommands = true
|
|
70
|
+
this.on('commandStarted', cmdStartedHandler.bind(this, shim))
|
|
71
|
+
return { callback: shim.LAST }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Wraps connect to record as operation but also to add a listener
|
|
76
|
+
* for `commandStarted`. This will be emitted before every command starts
|
|
77
|
+
* so we can properly update the segment attributes with a more accurate
|
|
78
|
+
* host/port/database name
|
|
79
|
+
*
|
|
80
|
+
* @param {Shim} shim
|
|
81
|
+
* @param {MongoClient} MongoClient reference
|
|
82
|
+
*/
|
|
83
|
+
function instrumentMongoClient(shim, MongoClient) {
|
|
84
|
+
shim.recordOperation(MongoClient.prototype, 'connect', wrapConnect)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
module.exports = function instrument(shim, mongodb) {
|
|
89
|
+
shim.setParser(queryParser)
|
|
90
|
+
instrumentMongoClient(shim, mongodb.MongoClient)
|
|
91
|
+
instrumentCursor(shim, mongodb.AbstractCursor)
|
|
92
|
+
instrumentCursor(shim, mongodb.FindCursor)
|
|
93
|
+
instrumentCursor(shim, mongodb.AggregationCursor)
|
|
94
|
+
instrumentCollection(shim, mongodb.Collection)
|
|
95
|
+
instrumentDb(shim, mongodb.Db)
|
|
96
|
+
}
|