From e1c4fca289430f7a5e2b90f31fe1b6232054c269 Mon Sep 17 00:00:00 2001 From: Elliot Kirk Date: Thu, 20 Feb 2025 14:20:20 -0800 Subject: [PATCH 1/2] feat(web-sdk): add webVitalsInstrumentation.trackAttribution, enable by default --- packages/core/src/config/types.ts | 9 +++++++- .../web-sdk/src/config/makeCoreConfig.test.ts | 16 +++++++++++++ .../webVitals/instrumentation.test.ts | 23 ++++++++++++++++--- .../webVitals/instrumentation.ts | 9 +++++--- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/packages/core/src/config/types.ts b/packages/core/src/config/types.ts index 2032b66e7..f7f7be56f 100644 --- a/packages/core/src/config/types.ts +++ b/packages/core/src/config/types.ts @@ -159,7 +159,7 @@ export interface Config

{ trackResources?: boolean; /** - * Track web vitals attribution data (default: false) + * Track web vitals attribution data (default: true) */ trackWebVitalsAttribution?: boolean; @@ -178,6 +178,13 @@ export interface Config

{ * for measuring these metrics in production. */ reportAllChanges?: boolean; + + /** + * Track web vitals attribution data (default: true) + * + * Functionally the same as setting `trackWebVitalsAttribution` to true. + */ + trackAttribution?: boolean; }; /** diff --git a/packages/web-sdk/src/config/makeCoreConfig.test.ts b/packages/web-sdk/src/config/makeCoreConfig.test.ts index a0c4208c3..ce1de88b5 100644 --- a/packages/web-sdk/src/config/makeCoreConfig.test.ts +++ b/packages/web-sdk/src/config/makeCoreConfig.test.ts @@ -99,6 +99,22 @@ describe('config', () => { expect(config?.webVitalsInstrumentation?.reportAllChanges).toBe(true); }); + it('enables web vitals feature when webVitalsInstrumentation.trackAttribution is true', () => { + const browserConfig = { + url: 'http://example.com/my-collector', + app: {}, + webVitalsInstrumentation: { + reportAllChanges: true, + trackAttribution: true, + }, + }; + const config = makeCoreConfig(browserConfig); + + expect(config).toBeTruthy(); + expect(config?.webVitalsInstrumentation?.reportAllChanges).toBe(true); + expect(config?.webVitalsInstrumentation?.trackAttribution).toBe(true); + }); + it('merges configured urls with default URLs into ignoreUrls list', () => { const browserConfig = { url: 'http://example.com/my-collector', diff --git a/packages/web-sdk/src/instrumentations/webVitals/instrumentation.test.ts b/packages/web-sdk/src/instrumentations/webVitals/instrumentation.test.ts index c56010313..b276fd6a9 100644 --- a/packages/web-sdk/src/instrumentations/webVitals/instrumentation.test.ts +++ b/packages/web-sdk/src/instrumentations/webVitals/instrumentation.test.ts @@ -13,7 +13,7 @@ describe('WebVitals Instrumentation', () => { jest.clearAllMocks(); }); - it('load WebVitalsBasic by default', () => { + it('load WebVitalsWithAttribution by default', () => { const transport = new MockTransport(); initializeFaro( @@ -23,16 +23,33 @@ describe('WebVitals Instrumentation', () => { }) ); + expect(WebVitalsBasic).toHaveBeenCalledTimes(0); + expect(WebVitalsWithAttribution).toHaveBeenCalledTimes(1); + }); + + it('load WebVitalsBasic when trackWebVitalAttribution is false', () => { + const transport = new MockTransport(); + + initializeFaro( + mockConfig({ + trackWebVitalsAttribution: false, + transports: [transport], + instrumentations: [new WebVitalsInstrumentation()], + }) + ); + expect(WebVitalsBasic).toHaveBeenCalledTimes(1); expect(WebVitalsWithAttribution).toHaveBeenCalledTimes(0); }); - it('load WebVitalsWithAttribution when trackWebVitalAttribution is true', () => { + it('load WebVitalsWithAttribution when webVitalsInstrumentation.trackAttribution is true', () => { const transport = new MockTransport(); initializeFaro( mockConfig({ - trackWebVitalsAttribution: true, + webVitalsInstrumentation: { + trackAttribution: true, + }, transports: [transport], instrumentations: [new WebVitalsInstrumentation()], }) diff --git a/packages/web-sdk/src/instrumentations/webVitals/instrumentation.ts b/packages/web-sdk/src/instrumentations/webVitals/instrumentation.ts index 3430c3ac1..a31fbc3da 100644 --- a/packages/web-sdk/src/instrumentations/webVitals/instrumentation.ts +++ b/packages/web-sdk/src/instrumentations/webVitals/instrumentation.ts @@ -14,9 +14,12 @@ export class WebVitalsInstrumentation extends BaseInstrumentation { } private intializeWebVitalsInstrumentation() { - if (this.config?.trackWebVitalsAttribution) { - return new WebVitalsWithAttribution(this.api.pushMeasurement, this.config.webVitalsInstrumentation); + if ( + this.config?.trackWebVitalsAttribution === false || + this.config?.webVitalsInstrumentation?.trackAttribution === false + ) { + return new WebVitalsBasic(this.api.pushMeasurement, this.config.webVitalsInstrumentation); } - return new WebVitalsBasic(this.api.pushMeasurement, this.config.webVitalsInstrumentation); + return new WebVitalsWithAttribution(this.api.pushMeasurement, this.config.webVitalsInstrumentation); } } From c9cdfec6a27a58936c0646ec123b0054b0f17c31 Mon Sep 17 00:00:00 2001 From: Elliot Kirk Date: Thu, 20 Feb 2025 14:29:39 -0800 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 553880986..7ca5da4ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,10 @@ - Feature (`@grafana/faro-web-sdk`): Provide a `webVitalsInstrumentation.reportAllChanges` option to report all changes for web vitals (#981) -- feat (`@grafana/faro-web-sdk`): Enhance user meta properties to align with OTEL semantic +- Feature (`@grafana/faro-web-sdk`): Provide a `webVitalsInstrumentation.trackAttribution` option to track + web vitals attribution (#991) +- Feature (`@grafana/faro-web-sdk`): Change `trackWebVitalsAttribution` default value to true (#991) +- Feature (`@grafana/faro-web-sdk`): Enhance user meta properties to align with OTEL semantic conventions for user attributes (#990) - Chore (`@grafana/faro-web-tracing`): Add user attributes to spans (#990)