Skip to content

Commit

Permalink
fix: Updated shim.createSegment and shim.handleMqTracingHeaders t…
Browse files Browse the repository at this point in the history
…o be backwards compatible with the changes to context manager
  • Loading branch information
bizob2828 committed Dec 18, 2024
1 parent 359a266 commit 4756a75
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/shim/message-shim/subscribe-consume.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function createConsumerWrapper({ shim, spec, consumer }) {
}
}
if (msgDesc.headers) {
shim.handleMqTracingHeaders(msgDesc.headers, tx.baseSegment, tx, shim._transportType)
shim.handleMqTracingHeaders(msgDesc.headers, tx.baseSegment, shim._transportType, tx)
}

shim.logger.trace('Started message transaction %s named %s', tx.id, txName)
Expand Down
1 change: 1 addition & 0 deletions lib/shim/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,7 @@ function createSegment(name, recorder, parent) {
}

const transaction = this.tracer.getTransaction()
parent = parent || this.getActiveSegment()
const spec = new specs.SegmentSpec(opts)
return _rawCreateSegment({ shim: this, spec, parent, transaction })
}
Expand Down
7 changes: 4 additions & 3 deletions lib/shim/transaction-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function setTransactionName(name) {
/**
* Retrieves whatever CAT headers may be in the given headers.
*
* - `handleMqTracingHeaders(headers [, segment [, transportType]])`
* - `handleMqTracingHeaders(headers [, segment ] [, transportType], [, transaction])`
*
* @memberof TransactionShim.prototype
*
Expand All @@ -195,11 +195,11 @@ function setTransactionName(name) {
* @param {TraceSegment} [segment]
* The trace segment to associate the header data with. If no segment is
* provided then the currently active segment is used.
* @param {Transaction} transaction active transaction
* @param {string} [transportType]
* The transport type that brought the headers. Usually `HTTP` or `HTTPS`.
* @param {Transaction} transaction active transaction
*/
function handleMqTracingHeaders(headers, segment, transaction, transportType) {
function handleMqTracingHeaders(headers, segment, transportType, transaction) {
// TODO: replace functionality when CAT fully removed.

if (!headers) {
Expand All @@ -216,6 +216,7 @@ function handleMqTracingHeaders(headers, segment, transaction, transportType) {

// Check that we're in an active transaction.
const currentSegment = segment || this.getSegment()
transaction = transaction || this.tracer.getTransaction()
if (!currentSegment || !transaction.isActive()) {
this.logger.trace('Not processing headers for CAT or DT, not in an active transaction.')
return
Expand Down
2 changes: 1 addition & 1 deletion test/unit/shim/shim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2117,7 +2117,7 @@ test('Shim', async function (t) {
helper.runInTransaction(agent, function (tx) {
const args = [wrappable.getActiveSegment]
const segment = wrappable.getActiveSegment()
const parent = shim.createSegment({ name: 'test segment', parent: segment })
const parent = shim.createSegment('test-segment')
shim.bindCallbackSegment({}, args, shim.LAST, parent)
const cbSegment = args[0]()

Expand Down
28 changes: 14 additions & 14 deletions test/unit/shim/transaction-shim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ test('TransactionShim', async function (t) {
assert.ok(!segment.catTransaction)
assert.ok(!segment.getAttributes().transaction_guid)

shim.handleMqTracingHeaders(headers, segment, tx)
shim.handleMqTracingHeaders(headers, segment, null, tx)

assert.ok(!tx.incomingCatId)
assert.ok(!tx.referringTransactionGuid)
Expand All @@ -465,7 +465,7 @@ test('TransactionShim', async function (t) {
assert.ok(!segment.catTransaction)
assert.ok(!segment.getAttributes().transaction_guid)

shim.handleMqTracingHeaders(headers, segment, tx)
shim.handleMqTracingHeaders(headers, segment, null, tx)

assert.ok(!tx.incomingCatId)
assert.ok(!tx.referringTransactionGuid)
Expand All @@ -488,7 +488,7 @@ test('TransactionShim', async function (t) {
assert.ok(!segment.getAttributes().transaction_guid)

assert.doesNotThrow(function () {
shim.handleMqTracingHeaders(null, segment, tx)
shim.handleMqTracingHeaders(null, segment, null, tx)
})

assert.ok(!tx.incomingCatId)
Expand Down Expand Up @@ -516,7 +516,7 @@ test('TransactionShim', async function (t) {

helper.runInTransaction(agent, shim.BG, function (tx2) {
assert.notEqual(tx2, tx)
shim.handleMqTracingHeaders(headers, segment, tx)
shim.handleMqTracingHeaders(headers, segment, null, tx)
})

assert.equal(tx.incomingCatId, '9876#id')
Expand All @@ -541,7 +541,7 @@ test('TransactionShim', async function (t) {
assert.ok(!tx.tripId)
assert.ok(!tx.referringPathHash)

shim.handleMqTracingHeaders(headers, null, tx)
shim.handleMqTracingHeaders(headers)

assert.equal(tx.incomingCatId, '9876#id')
assert.equal(tx.referringTransactionGuid, 'trans id')
Expand All @@ -568,7 +568,7 @@ test('TransactionShim', async function (t) {

helper.runInTransaction(agent, shim.BG, function (tx2) {
assert.notEqual(tx2, tx)
shim.handleMqTracingHeaders(headers, segment, tx)
shim.handleMqTracingHeaders(headers, segment, null, tx)
})

assert.equal(tx.incomingCatId, '9876#id')
Expand All @@ -592,7 +592,7 @@ test('TransactionShim', async function (t) {
helper.runInTransaction(agent, function (tx) {
const headers = { traceparent, tracestate }
const segment = shim.getSegment()
shim.handleMqTracingHeaders(headers, segment, tx)
shim.handleMqTracingHeaders(headers, segment)

const outboundHeaders = {}
tx.insertDistributedTraceHeaders(outboundHeaders)
Expand All @@ -615,7 +615,7 @@ test('TransactionShim', async function (t) {
helper.runInTransaction(agent, function (tx) {
const headers = { traceparent }
const segment = shim.getSegment()
shim.handleMqTracingHeaders(headers, segment, tx)
shim.handleMqTracingHeaders(headers, segment, null, tx)

const outboundHeaders = {}
tx.insertDistributedTraceHeaders(outboundHeaders)
Expand All @@ -638,7 +638,7 @@ test('TransactionShim', async function (t) {
helper.runInTransaction(agent, function (tx) {
const headers = { traceparent, tracestate }
const segment = shim.getSegment()
shim.handleMqTracingHeaders(headers, segment, tx)
shim.handleMqTracingHeaders(headers, segment, null, tx)

const outboundHeaders = {}
tx.insertDistributedTraceHeaders(outboundHeaders)
Expand All @@ -660,7 +660,7 @@ test('TransactionShim', async function (t) {
helper.runInTransaction(agent, function (tx) {
const headers = { traceparent, tracestate }
const segment = shim.getSegment()
shim.handleMqTracingHeaders(headers, segment, tx)
shim.handleMqTracingHeaders(headers, segment, null, tx)

const outboundHeaders = {}
tx.insertDistributedTraceHeaders(outboundHeaders)
Expand All @@ -687,7 +687,7 @@ test('TransactionShim', async function (t) {

helper.runInTransaction(agent, shim.BG, function (tx2) {
assert.notEqual(tx2, tx)
shim.handleMqTracingHeaders(headers, segment, tx2)
shim.handleMqTracingHeaders(headers, segment, null, tx2)
})

assert.equal(segment.catId, '6789#app')
Expand All @@ -712,7 +712,7 @@ test('TransactionShim', async function (t) {
assert.ok(!segment.catTransaction)
assert.ok(!segment.getAttributes().transaction_guid)

shim.handleMqTracingHeaders(headers, null, tx)
shim.handleMqTracingHeaders(headers, null, null, tx)

assert.equal(segment.catId, '6789#app')
assert.equal(segment.catTransaction, 'app data transaction name')
Expand All @@ -738,7 +738,7 @@ test('TransactionShim', async function (t) {

helper.runInTransaction(agent, shim.BG, function (tx2) {
assert.notEqual(tx2, tx)
shim.handleMqTracingHeaders(headers, segment, tx2)
shim.handleMqTracingHeaders(headers, segment, null, tx2)
})

assert.equal(segment.catId, '6789#app')
Expand All @@ -764,7 +764,7 @@ test('TransactionShim', async function (t) {
assert.ok(!segment.catTransaction)
assert.ok(!segment.getAttributes().transaction_guid)

shim.handleMqTracingHeaders(headers, null, tx)
shim.handleMqTracingHeaders(headers, null, null, tx)

assert.ok(!segment.catId)
assert.ok(!segment.catTransaction)
Expand Down

0 comments on commit 4756a75

Please sign in to comment.