This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
239 lines (195 loc) · 6.63 KB
/
index.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
'use strict'
var fs = require('fs')
var path = require('path')
var hook = require('require-in-the-middle')
var semver = require('semver')
var Transaction = require('./transaction')
var shimmer = require('./shimmer')
var MODULES = [
'apollo-server-core',
'bluebird',
'cassandra-driver',
'elasticsearch',
'express',
'express-graphql',
'express-queue',
'fastify',
'finalhandler',
'generic-pool',
'graphql',
'handlebars',
'hapi',
'http',
'https',
'http2',
'ioredis',
'knex',
'koa',
'koa-router',
'mimic-response',
'mongodb-core',
'mysql',
'mysql2',
'pg',
'redis',
'restify',
'tedious',
'ws'
]
module.exports = Instrumentation
function Instrumentation (agent) {
this._agent = agent
this._hook = null // this._hook is only exposed for testing purposes
this._started = false
this.currentTransaction = null
// Span for binding callbacks
this.bindingSpan = null
// Span which is actively bound
this.activeSpan = null
Object.defineProperty(this, 'currentSpan', {
get () {
return this.bindingSpan || this.activeSpan
}
})
}
Instrumentation.modules = Object.freeze(MODULES)
Instrumentation.prototype.start = function () {
if (!this._agent._conf.instrument) return
var self = this
this._started = true
if (this._agent._conf.asyncHooks && semver.gte(process.version, '8.2.0')) {
require('./async-hooks')(this)
} else {
require('./patch-async')(this)
}
var disabled = new Set(this._agent._conf.disableInstrumentations)
this._agent.logger.debug('adding hook to Node.js module loader')
this._hook = hook(MODULES, function (exports, name, basedir) {
var enabled = !disabled.has(name)
var pkg, version
if (basedir) {
pkg = path.join(basedir, 'package.json')
try {
version = JSON.parse(fs.readFileSync(pkg)).version
} catch (e) {
self._agent.logger.debug('could not shim %s module: %s', name, e.message)
return exports
}
} else {
version = process.versions.node
}
return self._patchModule(exports, name, version, enabled)
})
}
Instrumentation.prototype._patchModule = function (exports, name, version, enabled) {
this._agent.logger.debug('shimming %s@%s module', name, version)
return require('./modules/' + name)(exports, this._agent, version, enabled)
}
Instrumentation.prototype.addEndedTransaction = function (transaction) {
var agent = this._agent
if (this._started) {
var payload = agent._transactionFilters.process(transaction._encode())
if (!payload) return agent.logger.debug('transaction ignored by filter %o', { trans: transaction.id, trace: transaction.traceId })
agent.logger.debug('sending transaction %o', { trans: transaction.id, trace: transaction.traceId })
agent._transport.sendTransaction(payload)
} else {
agent.logger.debug('ignoring transaction %o', { trans: transaction.id, trace: transaction.traceId })
}
}
Instrumentation.prototype.addEndedSpan = function (span) {
var agent = this._agent
if (this._started) {
agent.logger.debug('encoding span %o', { span: span.id, parent: span.parentId, trace: span.traceId, name: span.name, type: span.type })
span._encode(function (err, payload) {
if (err) {
agent.logger.error('error encoding span %o', { span: span.id, parent: span.parentId, trace: span.traceId, name: span.name, type: span.type, error: err.message })
return
}
payload = agent._spanFilters.process(payload)
if (!payload) {
agent.logger.debug('span ignored by filter %o', { span: span.id, parent: span.parentId, trace: span.traceId, name: span.name, type: span.type })
return
}
agent.logger.debug('sending span %o', { span: span.id, parent: span.parentId, trace: span.traceId, name: span.name, type: span.type })
if (agent._transport) agent._transport.sendSpan(payload)
})
} else {
agent.logger.debug('ignoring span %o', { span: span.id, parent: span.parentId, trace: span.traceId, name: span.name, type: span.type })
}
}
Instrumentation.prototype.startTransaction = function (name, type, opts) {
// To be backwards compatible with the old API, we also accept a `traceparent` string
if (typeof opts === 'string') opts = { childOf: opts }
return new Transaction(this._agent, name, type, opts)
}
Instrumentation.prototype.endTransaction = function (result, endTime) {
if (!this.currentTransaction) {
this._agent.logger.debug('cannot end transaction - no active transaction found')
return
}
this.currentTransaction.end(result, endTime)
}
Instrumentation.prototype.setDefaultTransactionName = function (name) {
var trans = this.currentTransaction
if (!trans) {
this._agent.logger.debug('no active transaction found - cannot set default transaction name')
return
}
trans.setDefaultName(name)
}
Instrumentation.prototype.setTransactionName = function (name) {
var trans = this.currentTransaction
if (!trans) {
this._agent.logger.debug('no active transaction found - cannot set transaction name')
return
}
trans.name = name
}
Instrumentation.prototype.startSpan = function () {
if (!this.currentTransaction) {
this._agent.logger.debug('no active transaction found - cannot build new span')
return null
}
return this.currentTransaction.startSpan.apply(this.currentTransaction, arguments)
}
Instrumentation.prototype.bindFunction = function (original) {
if (typeof original !== 'function' || original.name === 'elasticAPMCallbackWrapper') return original
var ins = this
var trans = this.currentTransaction
var span = this.currentSpan
if (trans && !trans.sampled) {
return original
}
return elasticAPMCallbackWrapper
function elasticAPMCallbackWrapper () {
var prevTrans = ins.currentTransaction
ins.currentTransaction = trans
ins.bindingSpan = null
ins.activeSpan = span
var result = original.apply(this, arguments)
ins.currentTransaction = prevTrans
return result
}
}
Instrumentation.prototype.bindEmitter = function (emitter) {
var ins = this
var methods = [
'on',
'addListener'
]
if (semver.satisfies(process.versions.node, '>=6')) {
methods.push('prependListener')
}
shimmer.massWrap(emitter, methods, (original) => function (name, handler) {
return original.call(this, name, ins.bindFunction(handler))
})
}
Instrumentation.prototype._recoverTransaction = function (trans) {
if (this.currentTransaction === trans) return
this._agent.logger.debug('recovering from wrong currentTransaction %o', {
wrong: this.currentTransaction ? this.currentTransaction.id : undefined,
correct: trans.id,
trace: trans.traceId
})
this.currentTransaction = trans
}