Skip to content

Commit

Permalink
refactor: move isPutRumEvent to http utils
Browse files Browse the repository at this point in the history
  • Loading branch information
williazz committed Jun 15, 2023
1 parent e44c803 commit c601c12
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/plugins/event-plugins/ResourcePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../../utils/common-utils';
import { ResourceEvent } from '../../events/resource-event';
import { PERFORMANCE_RESOURCE_EVENT_TYPE } from '../utils/constant';
import { isPutRumEventCall } from '../utils/http-utils';

export const RESOURCE_EVENT_PLUGIN_ID = 'resource';

Expand Down Expand Up @@ -120,20 +121,11 @@ export class ResourcePlugin extends InternalPlugin {
}
};

private isPutRumEventCall(eventData: PerformanceResourceTiming) {
const pathRegex =
/.*\/application\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/events/;
const entryUrl = new URL(eventData.name);
const hasSameHost =
entryUrl.host === this.context.config.endpointUrl.host;
return hasSameHost && pathRegex.test(entryUrl.pathname);
}

recordResourceEvent = (entryData: PerformanceResourceTiming): void => {
// Ignore calls to PutRumEvents (i.e., the CloudWatch RUM data
// plane), otherwise we end up in an infinite loop of recording
// PutRumEvents.
if (this.isPutRumEventCall(entryData)) {
if (isPutRumEventCall(entryData.name, this.context.config)) {
return;
}
if (this.context?.record) {
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/utils/http-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Config } from 'orchestration/Orchestration';
import {
Http,
Subsegment,
Expand Down Expand Up @@ -232,3 +233,11 @@ const uint8ArrayToHexString = (bytes: Uint8Array): string => {
}
return hexString;
};

export const isPutRumEventCall = (url: string, config: Config) => {
const pathRegex =
/.*\/application\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/events/;
const entryUrl = new URL(url);
const hasSameHost = entryUrl.host === config.endpointUrl.host;
return hasSameHost && pathRegex.test(entryUrl.pathname);
};

0 comments on commit c601c12

Please sign in to comment.