-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Removed transaction from segment. Introduced a new enterSegment and enterTransaction to make context propagation more clear #2646
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -961,19 +961,21 @@ | |
|
||
const shim = this.shim | ||
const tracer = this.agent.tracer | ||
const parent = tracer.getTransaction() | ||
const parentTx = tracer.getTransaction() | ||
|
||
assignCLMSymbol(shim, handle) | ||
return tracer.transactionNestProxy('web', function startWebSegment() { | ||
const tx = tracer.getTransaction() | ||
const context = tracer.getContext() | ||
const tx = context?.transaction | ||
const parent = context?.segment | ||
|
||
if (!tx) { | ||
return handle.apply(this, arguments) | ||
} | ||
|
||
if (tx === parent) { | ||
if (tx === parentTx) { | ||
logger.debug('not creating nested transaction %s using transaction %s', url, tx.id) | ||
return tracer.addSegment(url, null, null, true, handle) | ||
return tracer.addSegment(url, null, parent, true, handle) | ||
} | ||
|
||
logger.debug( | ||
|
@@ -985,10 +987,16 @@ | |
tx.nameState.setName(NAMES.CUSTOM, null, NAMES.ACTION_DELIMITER, url) | ||
tx.url = url | ||
tx.applyUserNamingRules(tx.url) | ||
tx.baseSegment = tracer.createSegment(url, recordWeb) | ||
tx.baseSegment = tracer.createSegment({ | ||
name: url, | ||
recorder: recordWeb, | ||
transaction: tx, | ||
parent | ||
}) | ||
const newContext = context.enterSegment({ transaction: tx, segment: tx.baseSegment }) | ||
tx.baseSegment.start() | ||
|
||
const boundHandle = tracer.bindFunction(handle, tx.baseSegment) | ||
const boundHandle = tracer.bindFunction(handle, newContext) | ||
maybeAddCLMAttributes(handle, tx.baseSegment) | ||
let returnResult = boundHandle.call(this) | ||
if (returnResult && shim.isPromise(returnResult)) { | ||
|
@@ -1061,19 +1069,21 @@ | |
const tracer = this.agent.tracer | ||
const shim = this.shim | ||
const txName = group + '/' + name | ||
const parent = tracer.getTransaction() | ||
const parentTx = tracer.getTransaction() | ||
|
||
assignCLMSymbol(shim, handle) | ||
return tracer.transactionNestProxy('bg', function startBackgroundSegment() { | ||
const tx = tracer.getTransaction() | ||
const context = tracer.getContext() | ||
const tx = context?.transaction | ||
const parent = context?.segment | ||
|
||
if (!tx) { | ||
return handle.apply(this, arguments) | ||
} | ||
|
||
if (tx === parent) { | ||
if (tx === parentTx) { | ||
logger.debug('not creating nested transaction %s using transaction %s', txName, tx.id) | ||
return tracer.addSegment(txName, null, null, true, handle) | ||
return tracer.addSegment(txName, null, parent, true, handle) | ||
} | ||
|
||
logger.debug( | ||
|
@@ -1085,11 +1095,17 @@ | |
) | ||
|
||
tx._partialName = txName | ||
tx.baseSegment = tracer.createSegment(name, recordBackground) | ||
tx.baseSegment = tracer.createSegment({ | ||
name, | ||
recorder: recordBackground, | ||
transaction: tx, | ||
parent | ||
}) | ||
const newContext = context.enterSegment({ transaction: tx, segment: tx.baseSegment }) | ||
tx.baseSegment.partialName = group | ||
tx.baseSegment.start() | ||
|
||
const boundHandle = tracer.bindFunction(handle, tx.baseSegment) | ||
const boundHandle = tracer.bindFunction(handle, newContext) | ||
maybeAddCLMAttributes(handle, tx.baseSegment) | ||
let returnResult = boundHandle.call(this) | ||
if (returnResult && shim.isPromise(returnResult)) { | ||
|
@@ -1524,12 +1540,13 @@ | |
const metadata = {} | ||
|
||
const segment = this.agent.tracer.getSegment() | ||
if (!segment) { | ||
const transaction = this.agent.tracer.getTransaction() | ||
if (!(segment || transaction)) { | ||
logger.debug('No transaction found when calling API#getTraceMetadata') | ||
} else if (!this.agent.config.distributed_tracing.enabled) { | ||
logger.debug('Distributed tracing disabled when calling API#getTraceMetadata') | ||
} else { | ||
metadata.traceId = segment.transaction.traceId | ||
metadata.traceId = transaction.traceId | ||
|
||
const spanId = segment.getSpanId() | ||
if (spanId) { | ||
|
@@ -1547,7 +1564,7 @@ | |
* @param {string} params.traceId Identifier for the feedback event. | ||
* Obtained from {@link getTraceMetadata}. | ||
* @param {string} params.category A tag for the event. | ||
* @param {string} params.rating A indicator of how useful the message was. | ||
Check warning on line 1567 in api.js
|
||
* @param {string} [params.message] The message that triggered the event. | ||
* @param {object} [params.metadata] Additional key-value pairs to associate | ||
* with the recorded event. | ||
|
@@ -1901,7 +1918,7 @@ | |
transaction.ignoreApdex = true | ||
} | ||
|
||
/** | ||
Check warning on line 1921 in api.js
|
||
* Run a function with the passed in LLM context as the active context and return its return value. | ||
* | ||
* @example | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
module.exports = class Context { | ||
constructor(transaction, segment) { | ||
this._transaction = transaction | ||
this._segment = segment | ||
} | ||
|
||
get segment() { | ||
return this._segment | ||
} | ||
|
||
get transaction() { | ||
return this._transaction | ||
} | ||
|
||
enterSegment({ segment, transaction = this.transaction }) { | ||
return new this.constructor(transaction, segment) | ||
} | ||
|
||
enterTransaction(transaction) { | ||
return new this.constructor(transaction, transaction.trace.root) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we will eventually remove
segment
from the parameters and then retrieve it from the transaction in the function body?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand. Recorders are registered at the start of recording of a segment. In tracer they are added to transaction and binding the appropriate segment:
Then once a transaction end it iterates over all the recorders and calls the function with the scope and the transaction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this work make
transaction
the entry point of the API? As in, wouldn't we attempt to retrieve a segment from the transaction?