From 2d0914a21e1f243cf3d8044b09031a7723f792a6 Mon Sep 17 00:00:00 2001 From: Abinet Date: Tue, 23 May 2023 13:41:56 -0700 Subject: [PATCH] fix: apply suggested changes --- pkgs/api-logs/src/types/LoggerOptions.ts | 4 +- .../web/page-view/src/instrumentation.ts | 67 ++++++++++++------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/pkgs/api-logs/src/types/LoggerOptions.ts b/pkgs/api-logs/src/types/LoggerOptions.ts index 367cd6543..a57d44a73 100644 --- a/pkgs/api-logs/src/types/LoggerOptions.ts +++ b/pkgs/api-logs/src/types/LoggerOptions.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { LogAttributes } from './LogRecord'; +import { Attributes } from '@opentelemetry/api'; export interface LoggerOptions { /** @@ -26,7 +26,7 @@ export interface LoggerOptions { /** * The instrumentation scope attributes to associate with emitted telemetry */ - scopeAttributes?: LogAttributes; + scopeAttributes?: Attributes; /** * Specifies whether the Trace Context should automatically be passed on to the LogRecords emitted by the Logger. diff --git a/pkgs/instrumentations/web/page-view/src/instrumentation.ts b/pkgs/instrumentations/web/page-view/src/instrumentation.ts index 5a14bac62..d2d93aa67 100644 --- a/pkgs/instrumentations/web/page-view/src/instrumentation.ts +++ b/pkgs/instrumentations/web/page-view/src/instrumentation.ts @@ -14,7 +14,10 @@ * limitations under the License. */ -import { InstrumentationBase } from '@opentelemetry/sandbox-instrumentation'; +import { + InstrumentationBase, + isWrapped, +} from '@opentelemetry/sandbox-instrumentation'; import { Logger, LogRecord } from '@opentelemetry/sandbox-api-logs'; @@ -49,7 +52,7 @@ export class PageViewEventInstrumentation extends InstrumentationBase { VERSION ); this.applyCustomLogAttributes = config.applyCustomLogAttributes; - this._wrapHistory(); + this._patchHistoryApi(); } init() {} @@ -135,31 +138,49 @@ export class PageViewEventInstrumentation extends InstrumentationBase { ); } - private _wrapHistory(): void { - const originalPushState = history.pushState; - const originalReplaceState = history.replaceState; - - history.pushState = ( - data: any, - title: string, - url?: string | null | undefined - ) => { - originalPushState.apply(history, [data, title, url]); - this._onVirtualPageView('pushState'); - this.oldUrl = location.href; + /** + * Patches the certain history api method + */ + _patchHistoryMethod(changeState: string) { + const plugin = this; + return (original: any) => { + return function patchHistoryMethod(this: History, ...args: unknown[]) { + const result = original.apply(this, args); + const url = location.href; + const oldUrl = plugin.oldUrl; + if (url !== oldUrl) { + plugin._onVirtualPageView(changeState); + plugin.oldUrl = location.href; + } + return result; + }; }; + } - history.replaceState = ( - data: any, - title: string, - url?: string | null | undefined - ) => { - originalReplaceState.apply(history, [data, title, url]); - this._onVirtualPageView('replaceState'); - this.oldUrl = location.href; - }; + private _patchHistoryApi(): void { + this._unpatchHistoryApi(); + + this._wrap( + history, + 'replaceState', + this._patchHistoryMethod('replaceState') + ); + this._wrap(history, 'pushState', this._patchHistoryMethod('pushState')); + } + /** + * unpatch the history api methods + */ + _unpatchHistoryApi() { + if (isWrapped(history.replaceState)) this._unwrap(history, 'replaceState'); + if (isWrapped(history.pushState)) this._unwrap(history, 'pushState'); } + /** + * + * @param logRecord + * @param applyCustomLogAttributes + * Add custom attributes to the log record + */ _applyCustomAttributes( logRecord: LogRecord, applyCustomLogAttributes: ApplyCustomLogAttributesFunction | undefined