Skip to content

Commit b0c1ac1

Browse files
authored
refactor: Added check to only set a FakeSpan if transaction.agent.otelSpanKey exists (newrelic#3071)
1 parent 3cfb082 commit b0c1ac1

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

lib/context-manager/context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ module.exports = class Context {
4040
* @returns {Context} a newly constructed context
4141
*/
4242
enterTransaction(transaction) {
43-
return new this.constructor(transaction, transaction.trace.root)
43+
return new this.constructor(transaction, transaction?.trace?.root)
4444
}
4545
}

lib/otel/context.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ module.exports = class OtelContext extends Context {
2424
* @returns {OtelContext} a newly constructed context
2525
*/
2626
enterSegment({ segment, transaction = this._transaction }) {
27-
this._otelCtx.set(transaction.agent.otelSpanKey, new FakeSpan(segment, transaction))
27+
if (transaction?.agent?.otelSpanKey) {
28+
this._otelCtx.set(transaction.agent.otelSpanKey, new FakeSpan(segment, transaction))
29+
}
2830
return new this.constructor(transaction, segment, this._otelCtx)
2931
}
3032

@@ -36,8 +38,10 @@ module.exports = class OtelContext extends Context {
3638
* @returns {OtelContext} a newly constructed context
3739
*/
3840
enterTransaction(transaction) {
39-
this._otelCtx.set(transaction.agent.otelSpanKey, new FakeSpan(transaction.trace.root, transaction))
40-
return new this.constructor(transaction, transaction.trace.root, this._otelCtx)
41+
if (transaction?.agent?.otelSpanKey) {
42+
this._otelCtx.set(transaction.agent.otelSpanKey, new FakeSpan(transaction.trace.root, transaction))
43+
}
44+
return new this.constructor(transaction, transaction?.trace?.root, this._otelCtx)
4145
}
4246

4347
/**

test/unit/lib/otel/context.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ test('should add transaction and trace root to otel ctx', (t) => {
8585
})
8686
})
8787

88+
test('should not add transaction and trace root to otel ctx when undefined', (t) => {
89+
const { agent } = t.nr
90+
const ctx = otel.context.active()
91+
const newContext = ctx.enterTransaction()
92+
const fakeSpan = newContext.getValue(agent.otelSpanKey)
93+
assert.equal(fakeSpan, undefined)
94+
})
95+
8896
test('should add segment to otel ctx', (t) => {
8997
const { agent } = t.nr
9098
const ctx = otel.context.active()
@@ -97,3 +105,25 @@ test('should add segment to otel ctx', (t) => {
97105
traceId: newContext.transaction.traceId
98106
})
99107
})
108+
109+
test('should add segment to otel when both segment and transaction are passed in', (t) => {
110+
const { agent } = t.nr
111+
const ctx = otel.context.active()
112+
const transaction = { agent, traceId: 'traceId' }
113+
const segment = { id: 'segmentId' }
114+
const newContext = ctx.enterSegment({ segment, transaction })
115+
const fakeSpan = newContext.getValue(agent.otelSpanKey)
116+
assert.deepEqual(fakeSpan, {
117+
segmentId: segment.id,
118+
traceId: newContext.transaction.traceId
119+
})
120+
})
121+
122+
test('should not set fake span if transaction.agent.otelSpanKey is null', (t) => {
123+
const { agent } = t.nr
124+
const ctx = otel.context.active()
125+
const segment = { id: 'segmentId' }
126+
const newContext = ctx.enterSegment({ segment })
127+
const fakeSpan = newContext.getValue(agent.otelSpanKey)
128+
assert.equal(fakeSpan, undefined)
129+
})

0 commit comments

Comments
 (0)