Skip to content

Commit

Permalink
allow adding custom data to event
Browse files Browse the repository at this point in the history
  • Loading branch information
Abinet18 committed Sep 11, 2023
1 parent 10f3778 commit ebed67b
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions pkgs/instrumentations/web/web-vitals/src/web-vitals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
}
}
}

0 comments on commit ebed67b

Please sign in to comment.