Skip to content

Commit

Permalink
remove sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Sep 7, 2024
1 parent 6dced38 commit c4d5016
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 79 deletions.
53 changes: 1 addition & 52 deletions src/__tests__/extensions/web-vitals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('web vitals', () => {
onCapture.mockClear()
posthog = await createPosthogInstance(uuidv7(), {
_onCapture: onCapture,
capture_performance: { web_vitals: true, web_vitals_metrics: clientConfig },
capture_performance: { web_vitals: true, web_vitals_allowed_metrics: clientConfig },
// sometimes pageviews sneak in and make asserting on mock capture tricky
capture_pageview: false,
})
Expand Down Expand Up @@ -234,55 +234,4 @@ describe('web vitals', () => {
}
)
})

describe.each(['client', 'server'])('sampling', (configSource) => {
describe.each([
[0, 0, 0],
[1, 100, 100],
[0.5, 40, 60],
])(`with sample rate of %f`, (sampleRate: number, min: number, max: number) => {
beforeEach(async () => {
// we need a set of fake web vitals handlers, so we can manually trigger the events
assignableWindow.__PosthogExtensions__ = {}
assignableWindow.__PosthogExtensions__.postHogWebVitalsCallbacks = {
onLCP: (cb: any) => {
onLCPCallback = cb
},
onCLS: (cb: any) => {
onCLSCallback = cb
},
onFCP: (cb: any) => {
onFCPCallback = cb
},
onINP: (cb: any) => {
onINPCallback = cb
},
}
})

it(`with config from ${configSource} captures roughly half the time when sample rate is 0.5`, async () => {
posthog = await createPosthogInstance(uuidv7(), {
_onCapture: onCapture,
capture_performance: {
web_vitals: true,
web_vitals_sample_rate: configSource === 'client' ? sampleRate : undefined,
},
})

posthog.webVitalsAutocapture!.afterDecideResponse({
capturePerformance: {
web_vitals: true,
web_vitals_sample_rate: configSource === 'server' ? sampleRate : undefined,
},
} as DecideResponse)

for (let i = 0; i < 100; i++) {
emitAllMetrics()
}

expect(onCapture.mock.calls.length).toBeGreaterThanOrEqual(min)
expect(onCapture.mock.calls.length).toBeLessThanOrEqual(max)
})
})
})
})
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const HEATMAPS_ENABLED_SERVER_SIDE = '$heatmaps_enabled_server_side'
export const EXCEPTION_CAPTURE_ENABLED_SERVER_SIDE = '$exception_capture_enabled_server_side'
export const EXCEPTION_CAPTURE_ENDPOINT_SUFFIX = '$exception_capture_endpoint_suffix'
export const WEB_VITALS_ENABLED_SERVER_SIDE = '$web_vitals_enabled_server_side'
export const WEB_VITALS_SAMPLE_RATE = '$web_vitals_sample_rate'
export const WEB_VITALS_ALLOWED_METRICS = '$web_vitals_allowed_metrics'
export const SESSION_RECORDING_ENABLED_SERVER_SIDE = '$session_recording_enabled_server_side'
export const CONSOLE_LOG_RECORDING_ENABLED_SERVER_SIDE = '$console_log_recording_enabled_server_side'
Expand Down
29 changes: 5 additions & 24 deletions src/extensions/web-vitals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PostHog } from '../../posthog-core'
import { DecideResponse, SupportedWebVitalsMetrics } from '../../types'
import { logger } from '../../utils/logger'
import { isBoolean, isNullish, isNumber, isObject, isUndefined } from '../../utils/type-utils'
import { WEB_VITALS_ALLOWED_METRICS, WEB_VITALS_ENABLED_SERVER_SIDE, WEB_VITALS_SAMPLE_RATE } from '../../constants'
import { WEB_VITALS_ALLOWED_METRICS, WEB_VITALS_ENABLED_SERVER_SIDE } from '../../constants'
import { assignableWindow, window } from '../../utils/globals'
import Config from '../../config'

Expand All @@ -28,20 +28,11 @@ export class WebVitalsAutocapture {
this.startIfEnabled()
}

public get sampleRate(): number | undefined {
const clientConfigSampleRate = isObject(this.instance.config.capture_performance)
? this.instance.config.capture_performance?.web_vitals_sample_rate
: undefined
return isUndefined(clientConfigSampleRate)
? this.instance.persistence?.props[WEB_VITALS_SAMPLE_RATE]
: clientConfigSampleRate
}

public get allowedMetrics(): SupportedWebVitalsMetrics[] {
const clientConfigMetricAllowList: SupportedWebVitalsMetrics[] | undefined = isObject(
this.instance.config.capture_performance
)
? this.instance.config.capture_performance?.web_vitals_metrics
? this.instance.config.capture_performance?.web_vitals_allowed_metrics
: undefined
return !isUndefined(clientConfigMetricAllowList)
? clientConfigMetricAllowList
Expand Down Expand Up @@ -75,20 +66,16 @@ export class WebVitalsAutocapture {

public afterDecideResponse(response: DecideResponse) {
const webVitalsOptIn = isObject(response.capturePerformance) && !!response.capturePerformance.web_vitals
const webVitalsSampleRate = isObject(response.capturePerformance)
? response.capturePerformance.web_vitals_sample_rate
: undefined

const allowedMetrics = isObject(response.capturePerformance)
? response.capturePerformance.web_vitals_metrics
? response.capturePerformance.web_vitals_allowed_metrics
: undefined

if (this.instance.persistence) {
this.instance.persistence.register({
[WEB_VITALS_ENABLED_SERVER_SIDE]: webVitalsOptIn,
})
this.instance.persistence.register({
[WEB_VITALS_SAMPLE_RATE]: webVitalsSampleRate,
})

this.instance.persistence.register({
[WEB_VITALS_ALLOWED_METRICS]: allowedMetrics,
})
Expand Down Expand Up @@ -151,12 +138,6 @@ export class WebVitalsAutocapture {
return
}

const isSampled = isUndefined(this.sampleRate) ? true : Math.random() < this.sampleRate
if (!isSampled) {
logger.info(LOGGER_PREFIX + 'Dropping metric due to sample rate', metric)
return
}

this.buffer = this.buffer || { url: undefined, metrics: [], firstMetricTimestamp: undefined }

const $currentUrl = this._currentURL()
Expand Down
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,13 @@ export interface PerformanceCaptureConfig {
* if not set this defaults to 15 minutes
*/
__web_vitals_max_value?: number
web_vitals_sample_rate?: number
/**
* By default all 4 metrics are captured
* You can set this config to restrict which metrics are captured
* e.g. ['CLS', 'FCP'] to only capture those two metrics
* NB setting this does not override whether the capture is enabled
*/
web_vitals_metrics?: SupportedWebVitalsMetrics[]
web_vitals_allowed_metrics?: SupportedWebVitalsMetrics[]
}

export interface HeatmapConfig {
Expand Down

0 comments on commit c4d5016

Please sign in to comment.