Skip to content

Commit 3aa61b4

Browse files
authored
feat: introduce _experimental_discardDataAfterInactivity (#1670)
1 parent 185453c commit 3aa61b4

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

packages/session-recorder/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ const SplunkRumRecorder = {
272272
return
273273
}
274274

275-
if (currentState.state === 'expired-duration' && recorder) {
275+
if (
276+
currentState.state === 'expired-duration' ||
277+
(currentState.state === 'expired-inactivity' &&
278+
SplunkRum._processedOptions?._experimental_discardDataAfterInactivity)
279+
) {
276280
recorder?.stop()
277281
recorder?.destroy()
278282
recorder = undefined

packages/web/src/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,18 @@ export const SplunkRum: SplunkOtelWebType = {
564564
}
565565
})
566566

567-
this.attributesProcessor = new SplunkSpanAttributesProcessor(this.sessionManager, this.userManager, {
568-
...(deploymentEnvironment
569-
? { 'deployment.environment': deploymentEnvironment, 'environment': deploymentEnvironment }
570-
: {}),
571-
...(version ? { 'app.version': version } : {}),
572-
...processedOptions.globalAttributes,
573-
})
567+
this.attributesProcessor = new SplunkSpanAttributesProcessor(
568+
this.sessionManager,
569+
this.userManager,
570+
{
571+
...(deploymentEnvironment
572+
? { 'deployment.environment': deploymentEnvironment, 'environment': deploymentEnvironment }
573+
: {}),
574+
...(version ? { 'app.version': version } : {}),
575+
...processedOptions.globalAttributes,
576+
},
577+
processedOptions._experimental_discardDataAfterInactivity,
578+
)
574579

575580
const spanProcessors: SpanProcessor[] = [this.attributesProcessor]
576581

packages/web/src/managers/session-manager/session-manager.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,13 @@ describe('SessionManager', () => {
250250
startTime: expect.any(Number),
251251
state: 'active',
252252
},
253-
previousState: null,
253+
previousState: {
254+
expiresAt: expect.any(Number),
255+
id: expect.any(String),
256+
source: 'web',
257+
startTime: expect.any(Number),
258+
state: 'active',
259+
},
254260
}),
255261
)
256262
})
@@ -508,7 +514,13 @@ describe('SessionManager', () => {
508514
startTime: expect.any(Number),
509515
state: 'active',
510516
},
511-
previousState: null,
517+
previousState: {
518+
expiresAt: expect.any(Number),
519+
id: expect.any(String),
520+
source: 'web',
521+
startTime: expect.any(Number),
522+
state: 'active',
523+
},
512524
}),
513525
)
514526
})

packages/web/src/managers/session-manager/session-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class SessionManager {
5151
private set session(state: SessionState) {
5252
const previousState = this._session
5353
this._session = state
54+
this.previousSessionState = previousState
5455
this.sessionHistory.set(state.id, state)
5556

5657
diag.debug('SessionManager: Updating session', { currentState: state, previousState })

packages/web/src/splunk-span-attributes-processor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class SplunkSpanAttributesProcessor implements SpanProcessor {
2828
private sessionManager: SessionManager,
2929
private userManager: UserManager,
3030
globalAttributes: Attributes,
31+
private readonly _experimental_discardDataAfterInactivity: boolean = false,
3132
) {
3233
this._globalAttributes = globalAttributes ?? {}
3334
}
@@ -63,7 +64,13 @@ export class SplunkSpanAttributesProcessor implements SpanProcessor {
6364

6465
const sessionState = this.sessionManager.getSessionState()
6566

66-
if (sessionState.state !== 'expired-duration') {
67+
// Negated condition was hard to read
68+
if (
69+
sessionState.state === 'expired-duration' ||
70+
(sessionState.state === 'expired-inactivity' && this._experimental_discardDataAfterInactivity)
71+
) {
72+
// Do not attach session ID to the span
73+
} else {
6774
span.setAttribute('splunk.rumSessionId', sessionState.id)
6875
}
6976

packages/web/src/types/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ export interface SplunkOtelWebConfig {
9797
*/
9898
_experimental_dataAttributesToCapture?: string[]
9999

100+
/**
101+
* Experimental: If true, no additional span will be sent after 15 minutes of user inactivity (it will be dropped).
102+
* @default false
103+
*/
104+
_experimental_discardDataAfterInactivity?: boolean
105+
100106
/**
101107
* @deprecated Please use `spaMetrics` instead.
102108
*/

0 commit comments

Comments
 (0)