-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
154 lines (124 loc) · 3.78 KB
/
index.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
'use strict'
const { randomFillSync } = require('random-poly-fill') // TODO: Remove when Node.js 6 is no longer supported
const SIZES = {
version: 1,
traceId: 16,
id: 8,
flags: 1,
parentId: 8,
// Aggregate sizes
ids: 24, // traceId + id
all: 34
}
const OFFSETS = {
version: 0,
traceId: SIZES.version,
id: SIZES.version + SIZES.traceId,
flags: SIZES.version + SIZES.ids,
// Additional parentId is stored after the header content
parentId: SIZES.version + SIZES.ids + SIZES.flags
}
const FLAGS = {
recorded: 0b00000001
}
function defineLazyProp (obj, prop, fn) {
Object.defineProperty(obj, prop, {
configurable: true,
enumerable: true,
get () {
const value = fn()
if (value !== undefined) {
Object.defineProperty(obj, prop, {
configurable: true,
enumerable: true,
value
})
}
return value
}
})
}
function hexSliceFn (buffer, offset, length) {
return () => buffer.slice(offset, length).toString('hex')
}
function maybeHexSliceFn (buffer, offset, length) {
const fn = hexSliceFn(buffer, offset, length)
return () => {
const value = fn()
// Check for any non-zero characters to identify a valid ID
if (/[1-9a-f]/.test(value)) {
return value
}
}
}
function makeChild (buffer) {
// Move current id into parentId region
buffer.copy(buffer, OFFSETS.parentId, OFFSETS.id, OFFSETS.flags)
// Generate new id
randomFillSync(buffer, OFFSETS.id, SIZES.id)
return new TraceParent(buffer)
}
function isValidHeader (header) {
return /^[\da-f]{2}-[\da-f]{32}-[\da-f]{16}-[\da-f]{2}$/.test(header)
}
// NOTE: The version byte is not fully supported yet, but is not important until
// we use the official header name rather than elastic-apm-traceparent.
// https://w3c.github.io/distributed-tracing/report-trace-context.html#versioning-of-traceparent
function headerToBuffer (header) {
const buffer = Buffer.alloc(SIZES.all)
buffer.write(header.replace(/-/g, ''), 'hex')
return buffer
}
function resume (header) {
return makeChild(headerToBuffer(header))
}
function start (sampled = false) {
const buffer = Buffer.alloc(SIZES.all)
// Generate new ids
randomFillSync(buffer, OFFSETS.traceId, SIZES.ids)
if (sampled) {
buffer[OFFSETS.flags] |= FLAGS.recorded
}
return new TraceParent(buffer)
}
const bufferSymbol = Symbol('trace-context-buffer')
class TraceParent {
constructor (buffer) {
this[bufferSymbol] = buffer
Object.defineProperty(this, 'recorded', {
value: !!(buffer[OFFSETS.flags] & FLAGS.recorded),
enumerable: true
})
defineLazyProp(this, 'version', hexSliceFn(buffer, OFFSETS.version, OFFSETS.traceId))
defineLazyProp(this, 'traceId', hexSliceFn(buffer, OFFSETS.traceId, OFFSETS.id))
defineLazyProp(this, 'id', hexSliceFn(buffer, OFFSETS.id, OFFSETS.flags))
defineLazyProp(this, 'flags', hexSliceFn(buffer, OFFSETS.flags, OFFSETS.parentId))
defineLazyProp(this, 'parentId', maybeHexSliceFn(buffer, OFFSETS.parentId))
}
static startOrResume (childOf, conf) {
if (childOf instanceof TraceParent) return childOf.child()
if (childOf && childOf._context instanceof TraceParent) return childOf._context.child()
return isValidHeader(childOf)
? resume(childOf)
: start(Math.random() <= conf.transactionSampleRate)
}
static fromString (header) {
return new TraceParent(headerToBuffer(header))
}
ensureParentId () {
let id = this.parentId
if (!id) {
randomFillSync(this[bufferSymbol], OFFSETS.parentId, SIZES.id)
id = this.parentId
}
return id
}
child () {
return makeChild(Buffer.from(this[bufferSymbol]))
}
toString () {
return `${this.version}-${this.traceId}-${this.id}-${this.flags}`
}
}
TraceParent.FLAGS = FLAGS
module.exports = TraceParent