Skip to content

Commit

Permalink
feat: setRecorded for traceparent/tracecontext (#2687)
Browse files Browse the repository at this point in the history
* feat: setRecorded for traceparent/tracecontext

Part of #2302
  • Loading branch information
astorm authored May 12, 2022
1 parent a289d44 commit 8b6c11a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/tracecontext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class TraceContext {
setter(carrier, 'tracestate', tracestate)
}
}

setRecorded () {
return this.traceparent.setRecorded()
}
}

TraceContext.FLAGS = TraceParent.FLAGS
Expand Down
14 changes: 12 additions & 2 deletions lib/tracecontext/traceparent.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ class TraceParent {
constructor (buffer) {
this[bufferSymbol] = buffer
Object.defineProperty(this, 'recorded', {
value: !!(buffer[OFFSETS.flags] & FLAGS.recorded),
enumerable: true
enumerable: true,
get: function () {
return !!(this[bufferSymbol][OFFSETS.flags] & FLAGS.recorded)
}
})

defineLazyProp(this, 'version', hexSliceFn(buffer, OFFSETS.version, OFFSETS.traceId))
Expand Down Expand Up @@ -144,6 +146,14 @@ class TraceParent {
return makeChild(Buffer.from(this[bufferSymbol]))
}

setRecorded (value) {
if (value) {
this[bufferSymbol][OFFSETS.flags] |= FLAGS.recorded
} else {
this[bufferSymbol][OFFSETS.flags] &= ~FLAGS.recorded
}
}

toString () {
return `${this.version}-${this.traceId}-${this.id}-${this.flags}`
}
Expand Down
14 changes: 14 additions & 0 deletions test/tracecontext/traceparent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,17 @@ test('ensureParentId', t => {

t.end()
})

test('setRecorded', t => {
const traceParent = TraceParent.fromString(header)

t.ok(traceParent.recorded)

traceParent.setRecorded(false)
t.ok(!traceParent.recorded)

traceParent.setRecorded(true)
t.ok(traceParent.recorded)

t.end()
})

0 comments on commit 8b6c11a

Please sign in to comment.