-
Notifications
You must be signed in to change notification settings - Fork 225
/
transaction.js
275 lines (232 loc) · 7.57 KB
/
transaction.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'use strict'
var afterAll = require('after-all-results')
var truncate = require('unicode-byte-truncate')
var uuid = require('uuid')
var config = require('../config')
var getPathFromRequest = require('./express-utils').getPathFromRequest
var parsers = require('../parsers')
var Span = require('./span')
var symbols = require('../symbols')
var Timer = require('./timer')
module.exports = Transaction
function Transaction (agent, name, type) {
Object.defineProperty(this, 'name', {
configurable: true,
enumerable: true,
get () {
// Fall back to a somewhat useful name in case no _defaultName is set.
// This might happen if res.writeHead wasn't called.
return this._customName ||
this._defaultName ||
(this.req ? this.req.method + ' unknown route (unnamed)' : 'unnamed')
},
set (name) {
if (this.ended) {
agent.logger.debug('tried to set transaction.name on already ended transaction %o', { id: this.id })
return
}
agent.logger.debug('setting transaction name %o', { id: this.id, name: name })
this._customName = name
}
})
Object.defineProperty(this, 'result', {
configurable: true,
enumerable: true,
get () {
return this._result
},
set (result) {
if (this.ended) {
agent.logger.debug('tried to set transaction.result on already ended transaction %o', { id: this.id })
return
}
agent.logger.debug('setting transaction result %o', { id: this.id, result: result })
this._result = result
}
})
this.id = uuid.v4()
this._defaultName = name || ''
this._customName = ''
this._user = null
this._custom = null
this._tags = null
this.type = type || 'custom'
this.result = 'success'
this.spans = []
this._builtSpans = []
this._droppedSpans = 0
this.ended = false
this._abortTime = 0
this._agent = agent
this._agent._instrumentation.currentTransaction = this
// Random sampling
this.sampled = Math.random() <= this._agent._conf.transactionSampleRate
agent.logger.debug('start transaction %o', { id: this.id, name: name, type: type })
this._timer = new Timer()
}
Transaction.prototype.setUserContext = function (context) {
if (!context) return
this._user = Object.assign(this._user || {}, context)
}
Transaction.prototype.setCustomContext = function (context) {
if (!context) return
this._custom = Object.assign(this._custom || {}, context)
}
Transaction.prototype.setTag = function (key, value) {
if (!key) return false
if (!this._tags) this._tags = {}
var skey = key.replace(/[.*]/g, '_')
if (key !== skey) {
this._agent.logger.warn('Illegal characters used in tag key: %s', key)
}
this._tags[skey] = truncate(String(value), config.INTAKE_STRING_MAX_SIZE)
return true
}
Transaction.prototype.addTags = function (tags) {
if (!tags) return false
var keys = Object.keys(tags)
for (let key of keys) {
if (!this.setTag(key, tags[key])) {
return false
}
}
return true
}
Transaction.prototype.buildSpan = function () {
if (!this.sampled) {
return null
}
if (this.ended) {
this._agent.logger.debug('transaction already ended - cannot build new span %o', { id: this.id })
return null
}
if (this._builtSpans.length >= this._agent._conf.transactionMaxSpans) {
this._droppedSpans++
return null
}
var span = new Span(this)
this._builtSpans.push(span)
return span
}
Transaction.prototype.toJSON = function () {
var payload = {
id: this.id,
name: this.name,
type: this.type,
duration: this.duration(),
timestamp: new Date(this._timer.start).toISOString(),
result: String(this.result),
sampled: this.sampled,
context: null,
spans: null
}
if (this.sampled) {
payload.context = {
user: Object.assign(
{},
this.req && parsers.getUserContextFromRequest(this.req),
this._user
),
tags: this._tags || {},
custom: this._custom || {}
}
// Only include dropped count when spans have been dropped.
if (this._droppedSpans > 0) {
payload.span_count = {
dropped: {
total: this._droppedSpans
}
}
}
if (this.req) {
var config = this._agent._conf.captureBody
var captureBody = config === 'transactions' || config === 'all'
payload.context.request = parsers.getContextFromRequest(this.req, captureBody)
}
if (this.res) {
payload.context.response = parsers.getContextFromResponse(this.res)
}
}
return payload
}
Transaction.prototype._encode = function (cb) {
var self = this
if (!this.ended) return cb(new Error('cannot encode un-ended transaction'))
var payload = this.toJSON()
var next = afterAll(function (err, spans) {
if (err) return cb(err)
if (self.sampled) {
payload.spans = spans
}
cb(null, payload)
})
this.spans.forEach(function (span) {
span._encode(next())
})
}
Transaction.prototype.duration = function () {
if (!this.ended) {
this._agent.logger.debug('tried to call duration() on un-ended transaction %o', { id: this.id, name: this.name, type: this.type })
return null
}
return this._timer.duration()
}
Transaction.prototype.setDefaultName = function (name) {
this._agent.logger.debug('setting default transaction name: %s %o', name, { id: this.id })
this._defaultName = name
}
Transaction.prototype.setDefaultNameFromRequest = function () {
var req = this.req
var path = getPathFromRequest(req)
if (!path) {
this._agent.logger.debug('could not extract route name from request %o', {
url: req.url,
type: typeof path,
null: path === null, // because typeof null === 'object'
route: !!req.route,
regex: req.route ? !!req.route.regexp : false,
mountstack: req[symbols.expressMountStack] ? req[symbols.expressMountStack].length : false,
id: this.id
})
path = 'unknown route'
}
this.setDefaultName(req.method + ' ' + path)
}
Transaction.prototype.end = function (result) {
if (this.ended) {
this._agent.logger.debug('tried to call transaction.end() on already ended transaction %o', { id: this.id })
return
}
if (result !== undefined) {
this.result = result
}
if (!this._defaultName && this.req) this.setDefaultNameFromRequest()
this._builtSpans.forEach(function (span) {
if (span.ended || !span.started) return
span.truncate()
})
this._timer.end()
this.ended = true
var trans = this._agent._instrumentation.currentTransaction
// These two edge-cases should normally not happen, but if the hooks into
// Node.js doesn't work as intended it might. In that case we want to
// gracefully handle it. That involves ignoring all spans under the given
// transaction as they will most likely be incomplete. We still want to send
// the transaction without any spans as it's still valuable data.
if (!trans) {
this._agent.logger.debug('WARNING: no currentTransaction found %o', { current: trans, spans: this.spans.length, id: this.id })
this.spans = []
} else if (trans !== this) {
this._agent.logger.debug('WARNING: transaction is out of sync %o', { spans: this.spans.length, id: this.id, other: trans.id })
this.spans = []
}
this._agent._instrumentation.addEndedTransaction(this)
this._agent.logger.debug('ended transaction %o', { id: this.id, type: this.type, result: this.result, name: this.name })
}
Transaction.prototype._recordEndedSpan = function (span) {
if (this.ended) {
this._agent.logger.debug('Can\'t record ended span after parent transaction have ended - ignoring %o', { id: this.id, span: span.name })
return
}
this.spans.push(span)
}