Skip to content

Commit

Permalink
chore: do not spawn session from long task in hidden state
Browse files Browse the repository at this point in the history
  • Loading branch information
Joozty committed Nov 28, 2024
1 parent c9b430c commit 49c99ef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/web/src/SplunkLongTaskInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { InstrumentationBase, InstrumentationConfig } from '@opentelemetry/instrumentation'

import { VERSION } from './version'
import { parseCookieToSessionState } from './session'

const LONGTASK_PERFORMANCE_TYPE = 'longtask'
const MODULE_NAME = 'splunk-longtask'
Expand Down Expand Up @@ -52,6 +53,11 @@ export class SplunkLongTaskInstrumentation extends InstrumentationBase {
init(): void {}

private _createSpanFromEntry(entry: PerformanceEntry) {
if (document.visibilityState === 'hidden' && !parseCookieToSessionState()) {
// session expired, we do not want to spawn new session in hidden state from longtask
return
}

const span = this.tracer.startSpan(LONGTASK_PERFORMANCE_TYPE, {
startTime: entry.startTime,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function pastMaxAge(startTime: number): boolean {
return startTime > now || now > startTime + MaxSessionAgeMillis
}

function parseCookieToSessionState() {
export function parseCookieToSessionState(): { id: string; startTime: number } | undefined {
const rawValue = findCookieValue(COOKIE_NAME)
if (!rawValue) {
return undefined
Expand All @@ -70,7 +70,7 @@ function parseCookieToSessionState() {
return undefined
}

let ss: any = undefined
let ss: { id: string; startTime: number } | undefined = undefined
try {
ss = JSON.parse(decoded)
} catch {
Expand Down

0 comments on commit 49c99ef

Please sign in to comment.