Skip to content

Commit 322c54e

Browse files
Qardwatson
authored andcommitted
feat(transaction): restructure span_count and include total (#553)
1 parent 2f155c5 commit 322c54e

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

lib/instrumentation/transaction.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ Transaction.prototype.toJSON = function () {
142142
timestamp: this.timestamp,
143143
result: String(this.result),
144144
sampled: this.sampled,
145-
context: null
145+
context: undefined,
146+
span_count: {
147+
started: this._builtSpans
148+
}
146149
}
147150

148151
if (this.sampled) {
@@ -158,11 +161,7 @@ Transaction.prototype.toJSON = function () {
158161

159162
// Only include dropped count when spans have been dropped.
160163
if (this._droppedSpans > 0) {
161-
payload.span_count = {
162-
dropped: {
163-
total: this._droppedSpans
164-
}
165-
}
164+
payload.span_count.dropped = this._droppedSpans
166165
}
167166

168167
if (this.req) {

test/instrumentation/transaction.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ test('#_encode() - ended', function (t) {
239239
var trans = new Transaction(ins._agent)
240240
trans.end()
241241
const payload = trans._encode()
242-
t.deepEqual(Object.keys(payload), ['id', 'name', 'type', 'duration', 'timestamp', 'result', 'sampled', 'context'])
242+
t.deepEqual(Object.keys(payload), ['id', 'name', 'type', 'duration', 'timestamp', 'result', 'sampled', 'context', 'span_count'])
243243
t.equal(typeof payload.id, 'string')
244244
t.equal(payload.id, trans.id)
245245
t.equal(payload.name, 'unnamed')
@@ -263,7 +263,7 @@ test('#_encode() - with meta data', function (t) {
263263
trans.setCustomContext({ baz: 1 })
264264
trans.end()
265265
const payload = trans._encode()
266-
t.deepEqual(Object.keys(payload), ['id', 'name', 'type', 'duration', 'timestamp', 'result', 'sampled', 'context'])
266+
t.deepEqual(Object.keys(payload), ['id', 'name', 'type', 'duration', 'timestamp', 'result', 'sampled', 'context', 'span_count'])
267267
t.equal(typeof payload.id, 'string')
268268
t.equal(payload.id, trans.id)
269269
t.equal(payload.name, 'foo')
@@ -284,7 +284,7 @@ test('#_encode() - http request meta data', function (t) {
284284
trans.req = mockRequest()
285285
trans.end()
286286
const payload = trans._encode()
287-
t.deepEqual(Object.keys(payload), ['id', 'name', 'type', 'duration', 'timestamp', 'result', 'sampled', 'context'])
287+
t.deepEqual(Object.keys(payload), ['id', 'name', 'type', 'duration', 'timestamp', 'result', 'sampled', 'context', 'span_count'])
288288
t.equal(typeof payload.id, 'string')
289289
t.equal(payload.id, trans.id)
290290
t.equal(payload.name, 'POST unknown route')
@@ -325,6 +325,39 @@ test('#_encode() - http request meta data', function (t) {
325325
t.end()
326326
})
327327

328+
test('#_encode() - with spans', function (t) {
329+
t.plan(9)
330+
var ins = mockInstrumentation(function () {
331+
t.pass('should end the transaction')
332+
})
333+
334+
var trans = new Transaction(ins._agent, 'single-name', 'type')
335+
trans.result = 'result'
336+
var span = trans.buildSpan()
337+
span.start('span')
338+
span.end()
339+
trans.end()
340+
341+
const payload = trans._encode()
342+
t.equal(payload.name, 'single-name')
343+
t.equal(payload.type, 'type')
344+
t.equal(payload.result, 'result')
345+
t.equal(payload.timestamp, new Date(trans._timer.start).toISOString())
346+
t.ok(payload.duration > 0, 'should have a duration >0ms')
347+
t.ok(payload.duration < 100, 'should have a duration <100ms')
348+
t.deepEqual(payload.context, {
349+
user: {},
350+
tags: {},
351+
custom: {}
352+
})
353+
354+
t.deepEqual(payload.span_count, {
355+
started: 1
356+
})
357+
358+
t.end()
359+
})
360+
328361
test('#_encode() - dropped spans', function (t) {
329362
t.plan(9)
330363
var ins = mockInstrumentation(function () {
@@ -359,9 +392,8 @@ test('#_encode() - dropped spans', function (t) {
359392
})
360393

361394
t.deepEqual(payload.span_count, {
362-
dropped: {
363-
total: 1
364-
}
395+
started: 2,
396+
dropped: 1
365397
})
366398

367399
t.end()

0 commit comments

Comments
 (0)