Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Disable session tracking when missing browser API #1140

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/supportability-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ A timeslice metric is harvested to the JSE/XHR consumer. An aggregation service
* Session/Inactive/Seen
<!--- Duration of Session at time of ending --->
* Session/Duration/Ms
<!--- Capture SMs for Session trace if active (ptid is setwhen returned byReplay ingest). Retain these SMs while we are working through the Session_replay Feature --->
* PageSession/Feature/SessionTrace/Duration/Ms
<!--- Capture SM when session tracking (trace, replay, and manager) could not be started due to missing PerformanceNavigationTiming API. --->
* Session/Disabled/MissingPerformanceNavigationTiming/Seen

### AJAX
<!--- Ajax Events were Excluded because they matched the Agent beacon --->
Expand Down
6 changes: 1 addition & 5 deletions src/features/session_trace/aggregate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ export class Aggregate extends AggregateBase {
registerHandler('trace-jserror', (...args) => this.traceStorage.storeErrorAgg(...args), this.featureName, this.ee)
registerHandler('pvtAdded', (...args) => this.traceStorage.processPVT(...args), this.featureName, this.ee)

if (typeof PerformanceNavigationTiming !== 'undefined') {
this.traceStorage.storeTiming(globalScope.performance?.getEntriesByType?.('navigation')[0])
} else {
this.traceStorage.storeTiming(globalScope.performance?.timing)
}
this.traceStorage.storeTiming(globalScope.performance?.getEntriesByType?.('navigation')[0])

/** Only start actually harvesting if running in full mode at init time */
if (this.mode === MODE.FULL) this.startHarvesting()
Expand Down
2 changes: 1 addition & 1 deletion src/features/utils/feature-gates.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This affects session manager; it should only prevent traces & replay as discussed.

Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import { isBrowserScope } from '../../common/constants/runtime'
* @returns {boolean}
*/
export const canEnableSessionTracking = (agentId) => {
return isBrowserScope && getConfigurationValue(agentId, 'privacy.cookies_enabled') === true
return isBrowserScope && getConfigurationValue(agentId, 'privacy.cookies_enabled') === true && typeof PerformanceNavigationTiming !== 'undefined'
}
8 changes: 8 additions & 0 deletions src/features/utils/instrument-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { getConfigurationValue } from '../../common/config/config'
import { hasReplayPrerequisite } from '../session_replay/shared/utils'
import { canEnableSessionTracking } from './feature-gates'
import { single } from '../../common/util/invoke'
import { handle } from '../../common/event-emitter/handle'
import { SUPPORTABILITY_METRIC_CHANNEL } from '../metrics/constants'

/**
* Base class for instrumenting a feature.
Expand Down Expand Up @@ -77,12 +79,18 @@ export class InstrumentBase extends FeatureBase {
loadedSuccessfully = resolve
})

const performanceNavigationTimingSupportabilityMetric = single(() => {
handle(SUPPORTABILITY_METRIC_CHANNEL, ['Session/Disabled/MissingPerformanceNavigationTiming/Seen'], undefined, FEATURE_NAMES.metrics, this.ee)
})

const importLater = async () => {
let session
try {
if (canEnableSessionTracking(this.agentIdentifier)) { // would require some setup before certain features start
const { setupAgentSession } = await import(/* webpackChunkName: "session-manager" */ './agent-session')
session = setupAgentSession(this.agentIdentifier)
} else if (typeof PerformanceNavigationTiming === 'undefined') {
performanceNavigationTimingSupportabilityMetric()
}
} catch (e) {
warn(20, e)
Expand Down
6 changes: 6 additions & 0 deletions tests/components/session_trace/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ jest.mock('../../../src/common/window/load', () => ({
__esModule: true,
onWindowLoad: jest.fn(cb => cb())
}))
jest.mock('../../../src/features/utils/feature-gates', () => ({
__esModule: true,
canEnableSessionTracking: jest.fn(() => true)
}))

const aggregator = new Aggregator({ agentIdentifier: 'abcd', ee })

describe('session trace', () => {
let traceInstrument, traceAggregate

beforeAll(async () => {
traceInstrument = new SessionTrace('abcd', aggregator)
await traceInstrument.onAggregateImported
traceAggregate = traceInstrument.featAggregate
traceAggregate.ee.emit('rumresp', [{ st: 1, sts: 1 }])
})

beforeEach(() => {
traceAggregate.traceStorage.trace = {}
traceAggregate.sentTrace = null
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/features/utils/feature-gates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ jest.enableAutomock()
jest.unmock('../../../../src/features/utils/feature-gates')

let agentIdentifier
let originalPerformanceNavigationTiming

beforeEach(() => {
agentIdentifier = faker.string.uuid()
originalPerformanceNavigationTiming = global.PerformanceNavigationTiming
})

afterEach(() => {
global.PerformanceNavigationTiming = originalPerformanceNavigationTiming
})

describe('enableSessionTracking', () => {
test('should return false when not browser scope', async () => {
jest.replaceProperty(runtimeConstantsModule, 'isBrowserScope', false)
jest.mocked(configModule.getConfigurationValue).mockReturnValue(true)
global.PerformanceNavigationTiming = jest.fn()

expect(canEnableSessionTracking(agentIdentifier)).toEqual(false)
expect(configModule.getConfigurationValue).not.toHaveBeenCalled()
Expand All @@ -24,6 +31,16 @@ describe('enableSessionTracking', () => {
test('should return false when session tracking disabled', async () => {
jest.replaceProperty(runtimeConstantsModule, 'isBrowserScope', true)
jest.mocked(configModule.getConfigurationValue).mockReturnValue(false)
global.PerformanceNavigationTiming = jest.fn()

expect(canEnableSessionTracking(agentIdentifier)).toEqual(false)
expect(configModule.getConfigurationValue).toHaveBeenCalledWith(agentIdentifier, 'privacy.cookies_enabled')
})

test('should return false when PerformanceNavigationTiming API is undefined', () => {
jest.replaceProperty(runtimeConstantsModule, 'isBrowserScope', true)
jest.mocked(configModule.getConfigurationValue).mockReturnValue(true)
global.PerformanceNavigationTiming = undefined

expect(canEnableSessionTracking(agentIdentifier)).toEqual(false)
expect(configModule.getConfigurationValue).toHaveBeenCalledWith(agentIdentifier, 'privacy.cookies_enabled')
Expand All @@ -32,6 +49,7 @@ describe('enableSessionTracking', () => {
test('should return true when stars align', () => {
jest.replaceProperty(runtimeConstantsModule, 'isBrowserScope', true)
jest.mocked(configModule.getConfigurationValue).mockReturnValue(true)
global.PerformanceNavigationTiming = jest.fn()

expect(canEnableSessionTracking(agentIdentifier)).toEqual(true)
expect(configModule.getConfigurationValue).toHaveBeenCalledWith(agentIdentifier, 'privacy.cookies_enabled')
Expand Down
Loading