Skip to content

Commit 88b2c85

Browse files
cjr125Christopher Roberts
authored andcommitted
feat: Add callback option to config to capture context when starting transactions and spans
1 parent 45e862d commit 88b2c85

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

docs/configuration.asciidoc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,33 @@ This is useful on scenarios where the APM server is behind a reverse proxy that
410410

411411
NOTE: If APM Server is deployed in an origin different than the page’s origin, you will need to
412412
<<configuring-cors, configure Cross-Origin Resource Sharing (CORS)>>.
413+
414+
415+
[function]
416+
[[transaction-context-callback]]
417+
==== `transactionContextCallback`
418+
419+
* *Type:* Function
420+
* *Default:* `null`
421+
422+
`transactionContextCallback` allows the agent to specify a function to be called when starting automatically instrumented transactions and spans and return
423+
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.
424+
425+
The following example illustrates an example which captures the stack trace:
426+
427+
[source,js]
428+
----
429+
var options = {
430+
transactionContextCallback: () => {
431+
let stack
432+
try {
433+
throw new Error('')
434+
}
435+
catch (error) {
436+
stack = (error as Error).stack || ''
437+
}
438+
stack = stack.split('\n').map(function (line) { return line.trim(); })
439+
return { stack };
440+
}
441+
}
442+
----

packages/rum-core/src/common/config-service.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ class Config {
9494
context: {},
9595
session: false,
9696
apmRequest: null,
97-
sendCredentials: false
97+
sendCredentials: false,
98+
transactionContextCallback: null
9899
}
99100

100101
this.events = new EventHandler()

packages/rum-core/src/performance-monitoring/transaction-service.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,15 @@ class TransactionService {
101101

102102
createOptions(options) {
103103
const config = this._config.config
104-
let presetOptions = { transactionSampleRate: config.transactionSampleRate }
104+
let presetOptions = {
105+
transactionSampleRate: config.transactionSampleRate
106+
}
107+
if (config.transactionContextCallback) {
108+
presetOptions = {
109+
...presetOptions,
110+
transactionContextCallback: config.transactionContextCallback
111+
}
112+
}
105113
let perfOptions = extend(presetOptions, options)
106114
if (perfOptions.managed) {
107115
perfOptions = extend(
@@ -483,6 +491,13 @@ class TransactionService {
483491
)
484492
}
485493

494+
if (this._config.config.transactionContextCallback) {
495+
options = {
496+
...options,
497+
tags: this._config.config.transactionContextCallback()
498+
}
499+
}
500+
486501
const span = tr.startSpan(name, type, options)
487502
if (__DEV__) {
488503
this._logger.debug(

packages/rum-core/src/performance-monitoring/transaction.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ class Transaction extends SpanBase {
5454

5555
this.sampleRate = this.options.transactionSampleRate
5656
this.sampled = Math.random() <= this.sampleRate
57+
58+
if (this.options.transactionContextCallback) {
59+
this.options = {
60+
...this.options,
61+
tags: this.options.transactionContextCallback()
62+
}
63+
}
5764
}
5865

5966
addMarks(obj) {

packages/rum/src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ declare module '@elastic/apm-rum' {
107107
method: string
108108
payload?: string
109109
headers?: Record<string, string>
110-
}) => boolean
110+
}) => boolean,
111+
transactionContextCallback?: (...args: any[]) => any
111112
}
112113

113114
type Init = (options?: AgentConfigOptions) => ApmBase

0 commit comments

Comments
 (0)