File tree Expand file tree Collapse file tree 5 files changed +58
-3
lines changed Expand file tree Collapse file tree 5 files changed +58
-3
lines changed Original file line number Diff line number Diff line change @@ -410,3 +410,33 @@ This is useful on scenarios where the APM server is behind a reverse proxy that
410410
411411NOTE: 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+ ----
Original file line number Diff line number Diff 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 ( )
Original file line number Diff line number Diff 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 (
@@ -202,6 +210,7 @@ class TransactionService {
202210
203211 startTransaction ( name , type , options ) {
204212 const perfOptions = this . createOptions ( options )
213+
205214 let tr
206215 /**
207216 * Flag that decides whether we have to fire the `onstart`
@@ -483,6 +492,13 @@ class TransactionService {
483492 )
484493 }
485494
495+ if ( this . _config . config . transactionContextCallback ) {
496+ options = {
497+ ...options ,
498+ tags : this . _config . config . transactionContextCallback ( )
499+ }
500+ }
501+
486502 const span = tr . startSpan ( name , type , options )
487503 if ( __DEV__ ) {
488504 this . _logger . debug (
Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments