Skip to content

Commit af4781d

Browse files
committed
fix: update ids to correct v2 api format (elastic#584)
All the new id's are hex encoded random numbers. Their sizes are: - error id: 128 bit - transaction id: 64 bit - span id: 64 bit - trace id: 128 bit The implemented id generator is temporary (except error id's) and will be replaced by the OpenTracing id engine once that lands.
1 parent 3123389 commit af4781d

File tree

14 files changed

+76
-40
lines changed

14 files changed

+76
-40
lines changed

lib/agent.js

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

3+
var crypto = require('crypto')
34
var http = require('http')
45
var parseUrl = require('url').parse
56
var path = require('path')
@@ -8,7 +9,6 @@ var afterAll = require('after-all-results')
89
var ElasticAPMHttpClient = require('elastic-apm-http-client')
910
var isError = require('core-util-is').isError
1011
var ancestors = require('require-ancestors')
11-
var uuid = require('uuid')
1212

1313
var config = require('./config')
1414
var connect = require('./middleware/connect')
@@ -243,7 +243,7 @@ Agent.prototype.captureError = function (err, opts, cb) {
243243
var agent = this
244244
var trans = this.currentTransaction
245245
var timestamp = new Date().toISOString()
246-
var id = uuid.v4()
246+
var id = crypto.randomBytes(128 / 8).toString('hex')
247247
var req = opts && opts.request instanceof IncomingMessage
248248
? opts.request
249249
: trans && trans.req

lib/instrumentation/span.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
var crypto = require('crypto')
4+
35
var afterAll = require('after-all-results')
46
var Value = require('async-value-promise')
57

@@ -12,6 +14,7 @@ const TEST = process.env.ELASTIC_APM_TEST
1214
module.exports = Span
1315

1416
function Span (transaction) {
17+
this.id = crypto.randomBytes(64 / 8).toString('hex') // TODO: Replace with correct id once OT is ready
1518
this.transaction = transaction
1619
this.started = false
1720
this.ended = false
@@ -144,7 +147,9 @@ Span.prototype._encode = function (cb) {
144147
}
145148

146149
var payload = {
147-
transactionId: self.transaction.id,
150+
id: self.id,
151+
transaction_id: self.transaction.id,
152+
trace_id: self.transaction.traceId,
148153
timestamp: self.transaction.timestamp,
149154
name: self.name,
150155
type: self.type,

lib/instrumentation/transaction.js

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

3+
var crypto = require('crypto')
4+
35
var truncate = require('unicode-byte-truncate')
4-
var uuid = require('uuid')
56

67
var config = require('../config')
78
var getPathFromRequest = require('./express-utils').getPathFromRequest
@@ -58,7 +59,8 @@ function Transaction (agent, name, type) {
5859
}
5960
})
6061

61-
this.id = uuid.v4()
62+
this.id = crypto.randomBytes(64 / 8).toString('hex') // TODO: Replace with correct id once OT is ready
63+
this.traceId = crypto.randomBytes(128 / 8).toString('hex') // TODO: Replace with correct id once OT is ready
6264
this._defaultName = name || ''
6365
this._customName = ''
6466
this._user = null
@@ -136,6 +138,7 @@ Transaction.prototype.buildSpan = function () {
136138
Transaction.prototype.toJSON = function () {
137139
var payload = {
138140
id: this.id,
141+
trace_id: this.traceId,
139142
name: this.name,
140143
type: this.type,
141144
duration: this.duration(),

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@
8888
"set-cookie-serde": "^1.0.0",
8989
"sql-summary": "^1.0.1",
9090
"stackman": "^3.0.2",
91-
"unicode-byte-truncate": "^1.0.0",
92-
"uuid": "^3.2.1"
91+
"unicode-byte-truncate": "^1.0.0"
9392
},
9493
"devDependencies": {
9594
"@commitlint/cli": "^7.0.0",

test/agent.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,18 @@ test('#captureError()', function (t) {
572572
})
573573
})
574574

575+
t.test('generate error id', function (t) {
576+
t.plan(1 + APMServerWithDefaultAsserts.asserts)
577+
APMServerWithDefaultAsserts(t, {}, { expect: 'error' })
578+
.on('listening', function () {
579+
this.agent.captureError(new Error('foo'))
580+
})
581+
.on('data-error', function (data) {
582+
t.ok(/^[\da-f]{32}$/.test(data.id), `should have valid id (was: ${data.id})`)
583+
t.end()
584+
})
585+
})
586+
575587
t.test('should send a plain text message to the server', function (t) {
576588
t.plan(1 + APMServerWithDefaultAsserts.asserts)
577589
APMServerWithDefaultAsserts(t, {}, { expect: 'error' })

test/instrumentation/index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ 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))
28+
t.ok(/^[\da-f]{32}$/.test(trans.trace_id))
2829
t.equal(trans.name, 'foo' + index)
2930
t.equal(trans.type, 'bar' + index)
3031
t.ok(trans.duration > 0, 'duration should be >0ms')
@@ -36,7 +37,7 @@ test('basic', function (t) {
3637
const name = 't' + index + i
3738
const span = findObjInArray(data.spans, 'name', name)
3839
t.ok(span, 'should have span named ' + name)
39-
t.equal(span.transactionId, trans.id, 'should belong to correct transaction')
40+
t.equal(span.transaction_id, trans.id, 'should belong to correct transaction')
4041
t.equal(span.type, 'type')
4142
t.ok(span.start > 0, 'span start should be >0ms')
4243
t.ok(span.start < 100, 'span start should be <100ms')
@@ -88,7 +89,7 @@ test('same tick', function (t) {
8889
const name = 't' + i
8990
const span = findObjInArray(data.spans, 'name', name)
9091
t.ok(span, 'should have span named ' + name)
91-
t.equal(span.transactionId, trans.id, 'should belong to correct transaction')
92+
t.equal(span.transaction_id, trans.id, 'should belong to correct transaction')
9293
}
9394
t.end()
9495
})
@@ -111,7 +112,7 @@ test('serial - no parents', function (t) {
111112
const name = 't' + i
112113
const span = findObjInArray(data.spans, 'name', name)
113114
t.ok(span, 'should have span named ' + name)
114-
t.equal(span.transactionId, trans.id, 'should belong to correct transaction')
115+
t.equal(span.transaction_id, trans.id, 'should belong to correct transaction')
115116
}
116117
t.end()
117118
})
@@ -138,7 +139,7 @@ test('serial - with parents', function (t) {
138139
const name = 't' + i
139140
const span = findObjInArray(data.spans, 'name', name)
140141
t.ok(span, 'should have span named ' + name)
141-
t.equal(span.transactionId, trans.id, 'should belong to correct transaction')
142+
t.equal(span.transaction_id, trans.id, 'should belong to correct transaction')
142143
}
143144
t.end()
144145
})
@@ -165,7 +166,7 @@ test('stack branching - no parents', function (t) {
165166
const name = 't' + i
166167
const span = findObjInArray(data.spans, 'name', name)
167168
t.ok(span, 'should have span named ' + name)
168-
t.equal(span.transactionId, trans.id, 'should belong to correct transaction')
169+
t.equal(span.transaction_id, trans.id, 'should belong to correct transaction')
169170
}
170171
t.end()
171172
})
@@ -191,7 +192,7 @@ test('currentTransaction missing - recoverable', function (t) {
191192
const name = 't0'
192193
const span = findObjInArray(data.spans, 'name', name)
193194
t.ok(span, 'should have span named ' + name)
194-
t.equal(span.transactionId, trans.id, 'should belong to correct transaction')
195+
t.equal(span.transaction_id, trans.id, 'should belong to correct transaction')
195196
t.end()
196197
})
197198
var ins = agent._instrumentation
@@ -219,7 +220,7 @@ test('currentTransaction missing - not recoverable - last span failed', function
219220
const name = 't0'
220221
const span = findObjInArray(data.spans, 'name', name)
221222
t.ok(span, 'should have span named ' + name)
222-
t.equal(span.transactionId, trans.id, 'should belong to correct transaction')
223+
t.equal(span.transaction_id, trans.id, 'should belong to correct transaction')
223224
t.end()
224225
})
225226
var ins = agent._instrumentation
@@ -250,7 +251,7 @@ test('currentTransaction missing - not recoverable - middle span failed', functi
250251
for (const name of names) {
251252
const span = findObjInArray(data.spans, 'name', name)
252253
t.ok(span, 'should have span named ' + name)
253-
t.equal(span.transactionId, trans.id, 'should belong to correct transaction')
254+
t.equal(span.transaction_id, trans.id, 'should belong to correct transaction')
254255
}
255256
t.end()
256257
})
@@ -374,7 +375,8 @@ test('unsampled transactions do not include spans', function (t) {
374375
t.equal(data.transactions.length, 1)
375376

376377
data.transactions.forEach(function (trans) {
377-
t.ok(/[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}/.test(trans.id))
378+
t.ok(/^[\da-f]{16}$/.test(trans.id))
379+
t.ok(/^[\da-f]{32}$/.test(trans.trace_id))
378380
t.ok(trans.duration > 0, 'duration should be >0ms')
379381
t.ok(trans.duration < 100, 'duration should be <100ms')
380382
t.notOk(Number.isNaN((new Date(trans.timestamp)).getTime()))

test/instrumentation/modules/express-queue.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ function done (t, query) {
8585
t.comment('request ' + (i + 1))
8686
t.equal(trans.name, 'GET /', 'name should be GET /')
8787
t.equal(trans.type, 'request', 'type should be request')
88-
t.equal(data.spans.filter(span => span.transactionId === trans.id).length, 1, 'transaction should have 1 span')
89-
const span = findObjInArray(data.spans, 'transactionId', trans.id)
88+
t.equal(data.spans.filter(span => span.transaction_id === trans.id).length, 1, 'transaction should have 1 span')
89+
const span = findObjInArray(data.spans, 'transaction_id', trans.id)
9090
t.equal(span.name, 'foo', 'span name should be foo')
9191
t.equal(span.type, 'bar', 'span name should be bar')
9292
t.ok(span.start + span.duration < trans.duration, 'span should have valid timings')

test/instrumentation/modules/http/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test('request', function (t) {
2121

2222
var root = data.transactions[1]
2323
t.equal(root.name, 'GET /')
24-
const span = findObjInArray(data.spans, 'transactionId', root.id)
24+
const span = findObjInArray(data.spans, 'transaction_id', root.id)
2525
t.equal(span.name, 'GET localhost:' + server.address().port + '/test')
2626

2727
server.close()

test/instrumentation/modules/http2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ isSecure.forEach(secure => {
280280
var root = data.transactions[1]
281281
assertPath(t, root, secure, port, '/')
282282

283-
var span = findObjInArray(data.spans, 'transactionId', root.id)
283+
var span = findObjInArray(data.spans, 'transaction_id', root.id)
284284
t.ok(span, 'root transaction should have span')
285285
t.equal(span.type, 'ext.http2')
286286
t.equal(span.name, `undefined http${secure ? 's' : ''}://localhost:${port}/sub`)
@@ -340,7 +340,7 @@ 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 = /^[\da-f]{16}$/
344344
var matchTimestamp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/
345345

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

test/instrumentation/modules/mysql/mysql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ factories.forEach(function (f) {
328328
t.deepEqual(names, ['bar', 'baz', 'foo'])
329329

330330
data.transactions.forEach(function (trans) {
331-
const span = findObjInArray(data.spans, 'transactionId', trans.id)
331+
const span = findObjInArray(data.spans, 'transaction_id', trans.id)
332332
t.ok(span, 'transaction should have span')
333333
t.equal(span.name, 'SELECT')
334334
t.equal(span.type, 'db.mysql.query')

0 commit comments

Comments
 (0)