Skip to content
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: Add callback option to config to capture context when starting transactions and spans #1525

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/configuration.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,33 @@ This is useful on scenarios where the APM server is behind a reverse proxy that

NOTE: If APM Server is deployed in an origin different than the page’s origin, you will need to
<<configuring-cors, configure Cross-Origin Resource Sharing (CORS)>>.


[function]
[[transaction-context-callback]]
==== `transactionContextCallback`

* *Type:* Function
* *Default:* `null`

`transactionContextCallback` allows the agent to specify a function to be called when starting automatically instrumented transactions and spans and return
context to be set as tags. This enables the agent to capture the context when instrumented events are fired from files which do not import the RUM agent library.

The following example illustrates an example which captures the stack trace:

[source,js]
----
var options = {
transactionContextCallback: () => {
let stack
try {
throw new Error('')
}
catch (error) {
stack = (error as Error).stack || ''
}
stack = stack.split('\n').map(function (line) { return line.trim(); })
return { stack };
}
}
----
3 changes: 2 additions & 1 deletion packages/rum-core/src/common/config-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class Config {
context: {},
session: false,
apmRequest: null,
sendCredentials: false
sendCredentials: false,
transactionContextCallback: null
}

this.events = new EventHandler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ class TransactionService {

createOptions(options) {
const config = this._config.config
let presetOptions = { transactionSampleRate: config.transactionSampleRate }
let presetOptions = {
transactionSampleRate: config.transactionSampleRate
}
if (config.transactionContextCallback) {
presetOptions = {
...presetOptions,
transactionContextCallback: config.transactionContextCallback
}
}
let perfOptions = extend(presetOptions, options)
if (perfOptions.managed) {
perfOptions = extend(
Expand Down Expand Up @@ -483,6 +491,13 @@ class TransactionService {
)
}

if (this._config.config.transactionContextCallback) {
options = {
...options,
tags: this._config.config.transactionContextCallback()
}
}

const span = tr.startSpan(name, type, options)
if (__DEV__) {
this._logger.debug(
Expand Down
7 changes: 7 additions & 0 deletions packages/rum-core/src/performance-monitoring/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ class Transaction extends SpanBase {

this.sampleRate = this.options.transactionSampleRate
this.sampled = Math.random() <= this.sampleRate

if (this.options.transactionContextCallback) {
this.options = {
...this.options,
tags: this.options.transactionContextCallback()
}
}
}

addMarks(obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,34 @@ describe('TransactionService', function () {

transaction.end(pageLoadTime + 1000)
})

it('should capture tags from dispatch context', done => {
config.setConfig({
transactionContextCallback: () => {
let stack
try {
throw new Error('')
}
catch (error) {
stack = error.stack || ''
}
stack = stack.split('\n').map(function (line) { return line.trim(); })
return { stack };
}
})
const transactionService = new TransactionService(logger, config)

const tr1 = transactionService.startTransaction(
'transaction1',
'transaction'
)

tr1.onEnd = () => {
expect(tr1.options.tags.stack).toBeTruthy()
done()
}
tr1.end()
})
})

it('should truncate active spans after transaction ends', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/rum/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ declare module '@elastic/apm-rum' {
method: string
payload?: string
headers?: Record<string, string>
}) => boolean
}) => boolean,
transactionContextCallback?: (...args: any[]) => any
}

type Init = (options?: AgentConfigOptions) => ApmBase
Expand Down