Skip to content

Commit 3de0643

Browse files
cjr125cjr125
authored andcommitted
fix: pr comments
1 parent c2daa11 commit 3de0643

File tree

5 files changed

+66
-41
lines changed

5 files changed

+66
-41
lines changed

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,8 @@ class Span extends SpanBase {
4040
}
4141
this.sync = this.options.sync
4242

43-
if (
44-
this.options.spanContextCallback &&
45-
typeof this.options.spanContextCallback === 'function'
46-
) {
47-
let tags
48-
try {
49-
tags = this.options.spanContextCallback()
50-
this.addLabels(tags)
51-
} catch (e) {
52-
console.error('Failed to execute span context callback', e)
53-
}
43+
if (this.options.spanContextCallback) {
44+
this.addLabels(this.options.spanContextCallback())
5445
}
5546
}
5647

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,32 @@ class TransactionService {
104104
let presetOptions = {
105105
transactionSampleRate: config.transactionSampleRate
106106
}
107-
if (config.transactionContextCallback) {
108-
presetOptions = {
109-
...presetOptions,
110-
transactionContextCallback: config.transactionContextCallback
107+
if (typeof config.transactionContextCallback === 'function') {
108+
const tCC = function () {
109+
let tags = {}
110+
try {
111+
tags = config.transactionContextCallback()
112+
} catch (err) {
113+
this._logger.error(
114+
'Failed to execute transaction context callback',
115+
err
116+
)
117+
}
118+
return tags
111119
}
120+
presetOptions.transactionContextCallback = tCC.bind(this)
112121
}
113-
if (config.spanContextCallback) {
114-
presetOptions = {
115-
...presetOptions,
116-
spanContextCallback: config.spanContextCallback
122+
if (typeof config.spanContextCallback === 'function') {
123+
const sCC = function () {
124+
let tags = {}
125+
try {
126+
tags = config.spanContextCallback()
127+
} catch (err) {
128+
this._logger.error('Failed to execute span context callback', err)
129+
}
130+
return tags
117131
}
132+
presetOptions.spanContextCallback = sCC.bind(this)
118133
}
119134
let perfOptions = extend(presetOptions, options)
120135
if (perfOptions.managed) {
@@ -298,7 +313,7 @@ class TransactionService {
298313
if (name === NAME_UNKNOWN && pageLoadTransactionName) {
299314
tr.name = pageLoadTransactionName
300315
}
301-
316+
302317
/**
303318
* Capture the TBT as span after observing for all long task entries
304319
* and once performance observer is disconnected

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,8 @@ class Transaction extends SpanBase {
5555
this.sampleRate = this.options.transactionSampleRate
5656
this.sampled = Math.random() <= this.sampleRate
5757

58-
if (
59-
this.options.transactionContextCallback &&
60-
typeof this.options.transactionContextCallback === 'function'
61-
) {
62-
let tags
63-
try {
64-
tags = this.options.transactionContextCallback()
65-
this.addLabels(tags)
66-
} catch (e) {
67-
console.error('Failed to execute transaction context callback', e)
68-
}
58+
if (this.options.transactionContextCallback) {
59+
this.addLabels(this.options.transactionContextCallback())
6960
}
7061
}
7162

@@ -111,14 +102,8 @@ class Transaction extends SpanBase {
111102
}
112103
let opts = extend({}, options)
113104

114-
if (
115-
this.options.spanContextCallback &&
116-
typeof this.options.spanContextCallback === 'function'
117-
) {
118-
opts = {
119-
...opts,
120-
spanContextCallback: this.options.spanContextCallback
121-
}
105+
if (this.options.spanContextCallback) {
106+
opts.spanContextCallback = this.options.spanContextCallback
122107
}
123108

124109
opts.onEnd = trc => {

packages/rum-core/test/performance-monitoring/transaction-service.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,40 @@ describe('TransactionService', function () {
706706
}
707707
sp1.end()
708708
})
709+
710+
it('should safely catch and log errors for an invalid callback', () => {
711+
logger = new LoggingService()
712+
spyOn(logger, 'error')
713+
714+
config.setConfig({
715+
transactionContextCallback: () => {
716+
throw new Error('Error in transaction callback')
717+
},
718+
spanContextCallback: () => {
719+
throw new Error('Error in span callback')
720+
}
721+
})
722+
const transactionService = new TransactionService(logger, config)
723+
724+
const tr1 = transactionService.startTransaction(
725+
'transaction1',
726+
'transaction'
727+
)
728+
expect(logger.error).toHaveBeenCalledWith(
729+
'Failed to execute transaction context callback',
730+
new Error('Error in transaction callback')
731+
)
732+
logger.error.calls.reset()
733+
734+
const sp1 = tr1.startSpan('span1', 'span')
735+
expect(logger.error).toHaveBeenCalledWith(
736+
'Failed to execute span context callback',
737+
new Error('Error in span callback')
738+
)
739+
740+
sp1.end()
741+
tr1.end()
742+
})
709743
})
710744

711745
it('should truncate active spans after transaction ends', () => {

packages/rum/src/index.d.ts

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

0 commit comments

Comments
 (0)