Skip to content

Commit fe39c08

Browse files
author
Stephen Belanger
committed
feat(context): use TraceContext in spans and transactions
1 parent 7e9c5c3 commit fe39c08

File tree

6 files changed

+32
-17
lines changed

6 files changed

+32
-17
lines changed

lib/instrumentation/context.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ function toHex (buffer) {
99
}
1010

1111
class TraceContext {
12-
constructor (opts = {}) {
12+
constructor ({ version, traceId, id, flags, parentId } = {}) {
1313
// 1 byte
14-
this.version = opts.version
14+
this.version = version
1515
// 16 bytes
16-
this.traceId = opts.traceId
16+
this.traceId = traceId
1717
// 8 bytes
18-
this.id = opts.id
18+
this.id = id
1919
// 1 byte
20-
this.flags = opts.flags
20+
this.flags = flags
2121
// 8 bytes
22-
this.parentId = opts.parentId
22+
this.parentId = parentId
2323
}
2424

2525
static create () {

lib/instrumentation/span.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
var afterAll = require('after-all-results')
44
var Value = require('async-value-promise')
5-
var uuid = require('uuid')
65

76
var parsers = require('../parsers')
87
var stackman = require('../stackman')
@@ -13,7 +12,7 @@ const TEST = process.env.ELASTIC_APM_TEST
1312
module.exports = Span
1413

1514
function Span (transaction) {
16-
this.id = uuid.v4()
15+
this.context = transaction.context.child()
1716
this.transaction = transaction
1817
this.started = false
1918
this.ended = false
@@ -24,6 +23,12 @@ function Span (transaction) {
2423
this._stackObj = null
2524
this._agent = transaction._agent
2625

26+
Object.defineProperty(this, 'id', {
27+
get () {
28+
return this.context.id.toString('hex')
29+
}
30+
})
31+
2732
this._agent.logger.debug('init span %o', {id: this.transaction.id})
2833
}
2934

@@ -145,8 +150,11 @@ Span.prototype._encode = function (cb) {
145150
self._agent.logger.warn('could not capture stack trace for span %o', {id: self.transaction.id, name: self.name, type: self.type, err: err.message})
146151
}
147152

153+
var context = self.context.toJSON()
154+
148155
var payload = {
149-
id: self.id,
156+
id: context.id,
157+
traceId: context.traceId,
150158
transactionId: self.transaction.id,
151159
timestamp: self.transaction.timestamp,
152160
name: self.name,

lib/instrumentation/transaction.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

33
var truncate = require('unicode-byte-truncate')
4-
var uuid = require('uuid')
54

65
var config = require('../config')
6+
var TraceContext = require('./context')
77
var getPathFromRequest = require('./express-utils').getPathFromRequest
88
var parsers = require('../parsers')
99
var Span = require('./span')
@@ -58,7 +58,13 @@ function Transaction (agent, name, type) {
5858
}
5959
})
6060

61-
this.id = uuid.v4()
61+
Object.defineProperty(this, 'id', {
62+
get () {
63+
return this.context.id.toString('hex')
64+
}
65+
})
66+
67+
this.context = TraceContext.create()
6268
this._defaultName = name || ''
6369
this._customName = ''
6470
this._user = null

test/instrumentation/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test('basic', function (t) {
2424
t.equal(data.spans.length, 4)
2525

2626
data.transactions.forEach(function (trans, index) {
27-
t.ok(/[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}/.test(trans.id))
27+
t.ok(/[\da-f]{16}/.test(trans.id))
2828
t.equal(trans.name, 'foo' + index)
2929
t.equal(trans.type, 'bar' + index)
3030
t.ok(trans.duration > 0, 'duration should be >0ms')
@@ -374,7 +374,7 @@ test('unsampled transactions do not include spans', function (t) {
374374
t.equal(data.transactions.length, 1)
375375

376376
data.transactions.forEach(function (trans) {
377-
t.ok(/[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}/.test(trans.id))
377+
t.ok(/[\da-f]{16}/.test(trans.id))
378378
t.ok(trans.duration > 0, 'duration should be >0ms')
379379
t.ok(trans.duration < 100, 'duration should be <100ms')
380380
t.notOk(Number.isNaN((new Date(trans.timestamp)).getTime()))

test/instrumentation/modules/http2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ isSecure.forEach(secure => {
340340
})
341341
})
342342

343-
var matchId = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
343+
// var matchId = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
344+
var matchId = /^[0-9a-f]{16}/
344345
var matchTimestamp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/
345346

346347
function assertPath (t, trans, secure, port, path) {

test/instrumentation/span.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ test('#_encode() - ended unnamed', function myTest1 (t) {
108108
span.end()
109109
span._encode(function (err, payload) {
110110
t.error(err)
111-
t.deepEqual(Object.keys(payload), ['id', 'transactionId', 'timestamp', 'name', 'type', 'start', 'duration', 'stacktrace'])
111+
t.deepEqual(Object.keys(payload), ['id', 'traceId', 'transactionId', 'timestamp', 'name', 'type', 'start', 'duration', 'stacktrace'])
112112
t.equal(typeof payload.id, 'string')
113113
t.equal(payload.id, span.id)
114114
t.equal(payload.transactionId, trans.id)
@@ -130,7 +130,7 @@ test('#_encode() - ended named', function myTest2 (t) {
130130
span.end()
131131
span._encode(function (err, payload) {
132132
t.error(err)
133-
t.deepEqual(Object.keys(payload), ['id', 'transactionId', 'timestamp', 'name', 'type', 'start', 'duration', 'stacktrace'])
133+
t.deepEqual(Object.keys(payload), ['id', 'traceId', 'transactionId', 'timestamp', 'name', 'type', 'start', 'duration', 'stacktrace'])
134134
t.equal(payload.transactionId, trans.id)
135135
t.equal(payload.timestamp, trans.timestamp)
136136
t.notOk(Number.isNaN(Date.parse(payload.timestamp)))
@@ -152,7 +152,7 @@ test('#_encode() - disabled stack traces', function (t) {
152152
span.end()
153153
span._encode(function (err, payload) {
154154
t.error(err)
155-
t.deepEqual(Object.keys(payload), ['id', 'transactionId', 'timestamp', 'name', 'type', 'start', 'duration'])
155+
t.deepEqual(Object.keys(payload), ['id', 'traceId', 'transactionId', 'timestamp', 'name', 'type', 'start', 'duration'])
156156
t.equal(payload.transactionId, trans.id)
157157
t.equal(payload.timestamp, trans.timestamp)
158158
t.notOk(Number.isNaN(Date.parse(payload.timestamp)))

0 commit comments

Comments
 (0)