Skip to content

Commit a385b9b

Browse files
cjr125Christopher Roberts
authored andcommitted
feat: Add callback option to config to capture context when starting transactions and spans
1 parent 65ebcab commit a385b9b

5 files changed

Lines changed: 57 additions & 3 deletions

File tree

docs/configuration.asciidoc

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

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

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

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

101102
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(
@@ -485,6 +493,13 @@ class TransactionService {
485493
)
486494
}
487495

496+
if (this._config.config.transactionContextCallback) {
497+
options = {
498+
...options,
499+
tags: this._config.config.transactionContextCallback()
500+
}
501+
}
502+
488503
const span = tr.startSpan(name, type, options)
489504
if (__DEV__) {
490505
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)