Skip to content

Commit

Permalink
feat: Add callback option to config to capture context when starting …
Browse files Browse the repository at this point in the history
…transactions and spans
  • Loading branch information
cjr125 authored and Christopher Roberts committed Sep 23, 2024
1 parent 45e862d commit 88b2c85
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
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
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

0 comments on commit 88b2c85

Please sign in to comment.