Skip to content

Commit

Permalink
chore: coordinate pagehidden multitransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
devcorpio committed Dec 21, 2023
1 parent 087d811 commit f7c3ca2
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 31 deletions.
23 changes: 20 additions & 3 deletions packages/rum-core/src/common/observers/page-visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,26 @@ export function observePageVisibility(configService, transactionService) {
* @param transactionService
*/
function onPageHidden(configService, transactionService) {
reportInp(transactionService)
const inpTr = reportInp(transactionService)
// we don't want to flush the queue for every transaction that is ended when page becomes hidden
// and given the async nature of the ending process of transactions
// will need to coordinate the flushing process
if (inpTr) {
const unobserve = configService.observeEvent(QUEUE_ADD_TRANSACTION, () => {
// At this point the INP transaction is in the queue
// so let's end the managed transaction if any and flush
endManagedTransaction(configService, transactionService)
unobserve()
})
} else {
// In the absence of INP transaction
// let's just end the managed transaction if any and flush
endManagedTransaction(configService, transactionService)
}
}

// Ends an ongoing transaction if any and flushes the events queue
// Ends an ongoing managed transaction if any and flushes the events queue
function endManagedTransaction(configService, transactionService) {
const tr = transactionService.getCurrentTransaction()
if (tr) {
const unobserve = configService.observeEvent(QUEUE_ADD_TRANSACTION, () => {
Expand All @@ -91,7 +108,7 @@ function onPageHidden(configService, transactionService) {
unobserve()
})

tr.endPageHidden()
tr.end()
} else {
configService.dispatchEvent(QUEUE_FLUSH)
state.lastHiddenStart = now()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function reportInp(transactionService) {
// make sure that transaction's duration is > 0
// we do the +1 because INP can be also reported as 0
const endTime = startTime + inp + 1
inpTr.endPageHidden(endTime)
inpTr.end(endTime)

// restart INP tracking information
// since users can enter and leave the page multiple times
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ class TransactionService {
() => {
const { name, type } = tr
let { lastHiddenStart } = state

if (lastHiddenStart >= tr._start && !tr.endedPageHidden) {
if (lastHiddenStart >= tr._start) {
if (__DEV__) {
this._logger.debug(
`transaction(${tr.id}, ${name}, ${type}) was discarded! The page was hidden during the transaction!`
Expand Down
11 changes: 0 additions & 11 deletions packages/rum-core/src/performance-monitoring/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class Transaction extends SpanBase {

this.sampleRate = this.options.transactionSampleRate
this.sampled = Math.random() <= this.sampleRate

this.endedPageHidden = false
}

addMarks(obj) {
Expand Down Expand Up @@ -198,15 +196,6 @@ class Transaction extends SpanBase {
isManaged() {
return !!this.options.managed
}

/*
Makes sure that a transaction ended while the page was becoming hidden
it's not discarded for that very same reason.
*/
endPageHidden(endTime) {
this.endedPageHidden = true
this.end(endTime)
}
}

export default Transaction
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('observePageVisibility', () => {

it('should end the transaction', () => {
const transaction = createTransaction()
const endTransactionSpy = spyOn(transaction, 'endPageHidden')
const endTransactionSpy = spyOn(transaction, 'end')
spyOn(transactionService, 'getCurrentTransaction').and.returnValue(
transaction
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ if (utils.isPerfTypeSupported(EVENT)) {
it('should create page-exit transaction', () => {
const hardNavigatedPage = performance.getEntriesByType('navigation')[0]
const restoreINPSpy = spyOnFunction(inpProcessor, 'restoreINPState')
const endSpy = spyOnFunction(Transaction.prototype, 'endPageHidden')
const endSpy = spyOnFunction(Transaction.prototype, 'end')
endSpy.and.callThrough() // To make sure we can calculate the transaction duration

const tr = reportInp(transactionService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,18 +741,6 @@ describe('TransactionService', function () {
state.lastHiddenStart = lastHiddenStart
})

it('should not discard transaction ended when page was becoming hidden', async () => {
let { lastHiddenStart } = state
state.lastHiddenStart = performance.now() + 1000
let tr = transactionService.startTransaction('test-name', 'test-type')
await tr.endPageHidden()
expect(logger.debug).not.toHaveBeenCalledWith(
`transaction(${tr.id}, ${tr.name}, ${tr.type}) was discarded! The page was hidden during the transaction!`
)

state.lastHiddenStart = lastHiddenStart
})

it('should set session information on transaction', () => {
config.setConfig({ session: true })
const tr = new Transaction('test', 'test')
Expand Down

0 comments on commit f7c3ca2

Please sign in to comment.