From c4d50160f6a2f34da1365f28a8fc1df326b671c0 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Sat, 7 Sep 2024 12:13:30 +0100 Subject: [PATCH] remove sampling --- src/__tests__/extensions/web-vitals.test.ts | 53 +-------------------- src/constants.ts | 1 - src/extensions/web-vitals/index.ts | 29 ++--------- src/types.ts | 3 +- 4 files changed, 7 insertions(+), 79 deletions(-) diff --git a/src/__tests__/extensions/web-vitals.test.ts b/src/__tests__/extensions/web-vitals.test.ts index 8cb2e95c0..37233f0c5 100644 --- a/src/__tests__/extensions/web-vitals.test.ts +++ b/src/__tests__/extensions/web-vitals.test.ts @@ -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, }) @@ -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) - }) - }) - }) }) diff --git a/src/constants.ts b/src/constants.ts index 95fc99142..f9e608e17 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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' diff --git a/src/extensions/web-vitals/index.ts b/src/extensions/web-vitals/index.ts index 747bdc6d3..622fde5e0 100644 --- a/src/extensions/web-vitals/index.ts +++ b/src/extensions/web-vitals/index.ts @@ -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' @@ -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 @@ -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, }) @@ -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() diff --git a/src/types.ts b/src/types.ts index e56b011e7..bedd03c98 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 {