Skip to content

Commit c1e62db

Browse files
author
Stephen Belanger
committed
refactor(context): use shared buffer and string getters
1 parent c51573e commit c1e62db

File tree

2 files changed

+67
-93
lines changed

2 files changed

+67
-93
lines changed

lib/instrumentation/context.js

Lines changed: 46 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,57 @@
22

33
const crypto = require('crypto')
44

5-
function fromHex (hex) {
6-
return Buffer.from(hex, 'hex')
7-
}
8-
9-
function toHex (buffer) {
10-
return buffer.toString('hex')
5+
const OFFSETS = {
6+
version: 0,
7+
traceId: 1,
8+
id: 17,
9+
flags: 25
1110
}
1211

1312
class TraceContext {
14-
constructor ({ version, traceId, id, flags, parentId } = {}) {
15-
// 1 byte
16-
this.version = version
17-
// 16 bytes
18-
this.traceId = traceId
19-
// 8 bytes
20-
this.id = id
21-
// 1 byte
22-
this.flags = flags
23-
// 8 bytes
24-
this.parentId = parentId
13+
constructor (buffer) {
14+
this.buffer = buffer
2515
}
2616

27-
static create (sampled = false) {
28-
const bytes = crypto.randomBytes(24)
29-
const flags = Buffer.alloc(1)
30-
31-
// Load sampled flag into first bit of flags
32-
if (sampled) {
33-
flags[0] |= 0x01
34-
} else {
35-
flags[0] &= ~0x01
36-
}
37-
38-
return new TraceContext({
39-
version: Buffer.alloc(1),
40-
traceId: bytes.slice(0, 16),
41-
id: bytes.slice(16),
42-
flags: flags
43-
})
17+
get version () {
18+
return this.buffer.slice(
19+
OFFSETS.version,
20+
OFFSETS.traceId
21+
).toString('hex')
22+
}
23+
get traceId () {
24+
return this.buffer.slice(
25+
OFFSETS.traceId,
26+
OFFSETS.id
27+
).toString('hex')
28+
}
29+
get id () {
30+
return this.buffer.slice(
31+
OFFSETS.id,
32+
OFFSETS.flags
33+
).toString('hex')
34+
}
35+
get flags () {
36+
return this.buffer.slice(
37+
OFFSETS.flags
38+
).toString('hex')
4439
}
4540

46-
static fromString (header) {
47-
const [
48-
version,
49-
traceId,
50-
id,
51-
flags
52-
] = header.split('-').map(fromHex)
53-
54-
return new TraceContext({
55-
version,
56-
traceId,
57-
id,
58-
flags
59-
})
41+
static create (sampled = false) {
42+
const buffer = Buffer.alloc(26)
43+
crypto.randomFillSync(buffer, 1, 24)
44+
if (sampled) buffer[26] = 0x01
45+
return new TraceContext(buffer)
6046
}
6147

6248
child () {
63-
const { version, traceId, id: parentId, flags } = this
64-
const id = crypto.randomBytes(8)
49+
const buffer = Buffer.from(this.buffer)
50+
crypto.randomFillSync(buffer, 17, 8)
51+
return new TraceContext(buffer)
52+
}
6553

66-
return new TraceContext({
67-
version,
68-
traceId,
69-
id,
70-
flags,
71-
parentId
72-
})
54+
static fromString (header) {
55+
return new TraceContext(Buffer.from(header.replace(/-/g, ''), 'hex'))
7356
}
7457

7558
toString () {
@@ -80,16 +63,16 @@ class TraceContext {
8063
this.flags
8164
]
8265

83-
return parts.map(toHex).join('-')
66+
return parts.join('-')
8467
}
8568

8669
toJSON () {
8770
return {
88-
version: this.version.toString('hex'),
89-
traceId: this.traceId.toString('hex'),
90-
id: this.id.toString('hex'),
91-
flags: this.flags.toString('hex'),
92-
parentId: this.parentId && this.parentId.toString('hex')
71+
version: this.version,
72+
traceId: this.traceId,
73+
id: this.id,
74+
flags: this.flags,
75+
header: this.toString()
9376
}
9477
}
9578
}

test/instrumentation/context.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,18 @@ const flags = Buffer.alloc(1)
1212

1313
const header = `00-${traceId.toString('hex')}-${id.toString('hex')}-00`
1414

15-
test('constructor', t => {
16-
const context = new TraceContext({
17-
version,
18-
traceId,
19-
id,
20-
flags
21-
})
22-
23-
t.ok(context instanceof TraceContext, 'has a trace context object')
24-
t.ok(version.equals(context.version), 'version matches')
25-
t.ok(traceId.equals(context.traceId), 'traceId matches')
26-
t.ok(id.equals(context.id), 'id matches')
27-
t.ok(flags.equals(context.flags), 'flags matches')
28-
29-
t.end()
30-
})
15+
function toHex (buffer) {
16+
return buffer.toString('hex')
17+
}
3118

3219
test('fromString', t => {
3320
const context = TraceContext.fromString(header)
3421

3522
t.ok(context instanceof TraceContext, 'has a trace context object')
36-
t.ok(version.equals(context.version), 'version matches')
37-
t.ok(traceId.equals(context.traceId), 'traceId matches')
38-
t.ok(id.equals(context.id), 'id matches')
39-
t.ok(flags.equals(context.flags), 'flags matches')
23+
t.equal(version.toString('hex'), context.version, 'version matches')
24+
t.equal(traceId.toString('hex'), context.traceId, 'traceId matches')
25+
t.equal(id.toString('hex'), context.id, 'id matches')
26+
t.equal(flags.toString('hex'), context.flags, 'flags matches')
4027

4128
t.end()
4229
})
@@ -55,7 +42,12 @@ test('toJSON', t => {
5542
traceId: traceId.toString('hex'),
5643
id: id.toString('hex'),
5744
flags: flags.toString('hex'),
58-
parentId: undefined
45+
header: [
46+
version,
47+
traceId,
48+
id,
49+
flags
50+
].map(toHex).join('-')
5951
}, 'trace context serializes fields to hex strings, in JSON form')
6052

6153
t.end()
@@ -65,10 +57,10 @@ test('create', t => {
6557
const context = TraceContext.create()
6658

6759
t.ok(context instanceof TraceContext, 'has a trace context object')
68-
t.ok(version.equals(context.version), 'version matches')
69-
t.notOk(traceId.equals(context.traceId), 'has new traceId')
70-
t.notOk(id.equals(context.id), 'has new spanId')
71-
t.ok(flags.equals(context.flags), 'flags matches')
60+
t.equal(version.toString('hex'), context.version, 'version matches')
61+
t.notEqual(traceId.toString('hex'), context.traceId, 'has new traceId')
62+
t.notEqual(id.toString('hex'), context.id, 'has new id')
63+
t.equal(flags.toString('hex'), context.flags, 'flags matches')
7264

7365
t.end()
7466
})
@@ -78,11 +70,10 @@ test('child', t => {
7870
const context = parent.child()
7971

8072
t.ok(context instanceof TraceContext, 'has a trace context object')
81-
t.ok(version.equals(context.version), 'version matches')
82-
t.ok(traceId.equals(context.traceId), 'traceId matches')
83-
t.notOk(id.equals(context.id), 'has new id')
84-
t.ok(flags.equals(context.flags), 'flags matches')
85-
t.ok(parent.id.equals(context.parentId), 'parentId matches parent.id')
73+
t.equal(version.toString('hex'), context.version, 'version matches')
74+
t.equal(traceId.toString('hex'), context.traceId, 'has new traceId')
75+
t.notEqual(id.toString('hex'), context.id, 'has new id')
76+
t.equal(flags.toString('hex'), context.flags, 'flags matches')
8677

8778
t.end()
8879
})

0 commit comments

Comments
 (0)