Skip to content

Commit

Permalink
fix: apply suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Abinet18 committed May 23, 2023
1 parent 0cb9661 commit 2d0914a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
4 changes: 2 additions & 2 deletions pkgs/api-logs/src/types/LoggerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { LogAttributes } from './LogRecord';
import { Attributes } from '@opentelemetry/api';

export interface LoggerOptions {
/**
Expand All @@ -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.
Expand Down
67 changes: 44 additions & 23 deletions pkgs/instrumentations/web/page-view/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -49,7 +52,7 @@ export class PageViewEventInstrumentation extends InstrumentationBase<unknown> {
VERSION
);
this.applyCustomLogAttributes = config.applyCustomLogAttributes;
this._wrapHistory();
this._patchHistoryApi();
}

init() {}
Expand Down Expand Up @@ -135,31 +138,49 @@ export class PageViewEventInstrumentation extends InstrumentationBase<unknown> {
);
}

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
Expand Down

0 comments on commit 2d0914a

Please sign in to comment.