From ebed67b4935b7fd61f627c5e099812d2fb46f23b Mon Sep 17 00:00:00 2001 From: Abinet Date: Mon, 11 Sep 2023 14:29:57 -0700 Subject: [PATCH] allow adding custom data to event --- .../web/web-vitals/src/web-vitals.ts | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/pkgs/instrumentations/web/web-vitals/src/web-vitals.ts b/pkgs/instrumentations/web/web-vitals/src/web-vitals.ts index 2946b9426..938e508c6 100644 --- a/pkgs/instrumentations/web/web-vitals/src/web-vitals.ts +++ b/pkgs/instrumentations/web/web-vitals/src/web-vitals.ts @@ -18,41 +18,40 @@ import { InstrumentationBase, InstrumentationConfig, } from '@opentelemetry/sandbox-instrumentation'; -import { events } from '@opentelemetry/sandbox-sdk-events'; +import { Event } from '@opentelemetry/sandbox-api-events'; +import { EventEmitter } from '@opentelemetry/sandbox-sdk-events'; import { ReportOpts, Metric } from 'web-vitals'; import * as webVitals from 'web-vitals'; -import { VERSION } from './version'; +import { WebVitalsInstrumentationConfig, ApplyCustomEventDataFunction } from './types'; const DEFAULT_METRIC_NAMES: Metric['name'][] = ['CLS', 'FID', 'LCP']; - - -export interface WebVitalsInstrumentationConfig - extends InstrumentationConfig, - ReportOpts { - /** - * List of web-vitals to instrument ('CLS', 'FID', 'LCP', 'FCP', 'INP', 'TTFB') - * By default 'CLS', 'FID and 'LCP' are instrumented - */ - metricsToTrack?: Metric['name'][]; -} +const VERSION = '1'; export class WebVitalsInstrumentation extends InstrumentationBase { - private _metrics: Metric['name'][]; + applyCustomEventData: ApplyCustomEventDataFunction | undefined = undefined; + emitter: EventEmitter | null = null; + private _metricsToTrack: Metric['name'][]; private _reportAllChanges: ReportOpts['reportAllChanges']; private _durationThreshold: ReportOpts['durationThreshold']; + constructor(config?: WebVitalsInstrumentationConfig) { + super('@opentelemetry/sandbox-instrumentation-web-vitals', VERSION, config); - this._metrics = config?.metrics || DEFAULT_METRIC_NAMES; + this._metricsToTrack = config?.metricsToTrack || DEFAULT_METRIC_NAMES; this._reportAllChanges = config?.reportAllChanges; this._durationThreshold = config?.durationThreshold; + this.applyCustomEventData = config?.applyCustomEventData; + this.emitter = new EventEmitter( + 'web-vitals', + VERSION, + 'browser' + ); } private _onReport = (metric: Metric) => { - const eventEmitter = events.getEventEmitter('web-vitals', VERSION); - let webVitalEvent - eventEmitter.emit({ + const webVitalEvent = { name: 'web_vital', data: { value: metric.value, @@ -61,12 +60,16 @@ export class WebVitalsInstrumentation extends InstrumentationBase { rating: metric.rating, navigationType: metric.navigationType, name: metric.name, - }, - }); + } + }; + if(this.applyCustomEventData) { + this._applyCustomEventData(webVitalEvent,this.applyCustomEventData); + } + this.emitter.emit(webVitalEvent); }; override enable() { - this._metrics.forEach(metric => { + this._metricsToTrack.forEach(metric => { webVitals[`on${metric}`](this._onReport, { reportAllChanges: this._reportAllChanges, durationThreshold: this._durationThreshold, @@ -75,4 +78,18 @@ export class WebVitalsInstrumentation extends InstrumentationBase { } init(): void {} + /** + * + * @param event + * @param applyCustomEventData + * Add custom data to the event + */ + _applyCustomEventData( + event: Event, + applyCustomEventData: ApplyCustomEventDataFunction | undefined, + ) { + if (applyCustomEventData) { + applyCustomEventData(event) + } + } }