Skip to content

Commit 8377316

Browse files
authored
fix: ignore PutRumEvents for PerformanceResource events (#712)
1 parent c111546 commit 8377316

File tree

7 files changed

+40
-67
lines changed

7 files changed

+40
-67
lines changed

package-lock.json

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"npm-run-all": "^4.1.5",
100100
"prettier": "2.8.4",
101101
"standard-version": "^9.3.2",
102-
"testcafe": "^3.3.0",
102+
"testcafe": "^3.7.2",
103103
"testcafe-reporter-html": "^1.4.6",
104104
"testcafe-reporter-json": "^1.0.0",
105105
"ts-jest": "^29.0.3",

src/plugins/event-plugins/ResourcePlugin.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { InternalPlugin } from '../InternalPlugin';
22
import {
33
getResourceFileType,
4-
isPutRumEventsCall,
5-
shuffle
4+
isPutRumEventsCall
65
} from '../../utils/common-utils';
76
import { ResourceEvent } from '../../events/resource-event';
87
import { PERFORMANCE_RESOURCE_EVENT_TYPE } from '../utils/constant';
@@ -56,33 +55,27 @@ export class ResourcePlugin extends InternalPlugin {
5655
};
5756

5857
recordPerformanceEntries = (list: PerformanceEntryList) => {
59-
const recordAll: PerformanceEntry[] = [];
60-
const sample: PerformanceEntry[] = [];
61-
62-
list.filter((e) => e.entryType === RESOURCE)
63-
.filter((e) => !this.config.ignore(e))
64-
.forEach((event) => {
65-
const { name, initiatorType } =
66-
event as PerformanceResourceTiming;
67-
const type = getResourceFileType(name, initiatorType);
68-
if (this.config.recordAllTypes.includes(type)) {
69-
recordAll.push(event);
70-
} else if (this.config.sampleTypes.includes(type)) {
71-
sample.push(event);
72-
}
73-
});
58+
list.forEach((event) => {
59+
if (event.entryType !== RESOURCE || this.config.ignore(event)) {
60+
// ignore
61+
return;
62+
}
7463

75-
// Record all events for resources in recordAllTypes
76-
recordAll.forEach((r) =>
77-
this.recordResourceEvent(r as PerformanceResourceTiming)
78-
);
64+
const { name, initiatorType } = event as PerformanceResourceTiming;
65+
const type = getResourceFileType(name, initiatorType);
7966

80-
// Record events from resources in sample until we hit the resource limit
81-
shuffle(sample);
82-
while (sample.length > 0 && this.eventCount < this.config.eventLimit) {
83-
this.recordResourceEvent(sample.pop() as PerformanceResourceTiming);
84-
this.eventCount++;
85-
}
67+
if (this.config.recordAllTypes.includes(type)) {
68+
// Record all events for resources in recordAllTypes
69+
this.recordResourceEvent(event as PerformanceResourceTiming);
70+
} else if (
71+
this.config.sampleTypes.includes(type) &&
72+
this.eventCount < this.config.eventLimit
73+
) {
74+
// Record sample types
75+
this.recordResourceEvent(event as PerformanceResourceTiming);
76+
this.eventCount++;
77+
}
78+
});
8679
};
8780

8881
recordResourceEvent = ({

src/plugins/event-plugins/__tests__/ResourcePlugin.test.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ describe('ResourcePlugin tests', () => {
3434
});
3535

3636
test('When resource event is present then event is recorded', async () => {
37-
// Setup
38-
mockRandom(0); // Retain order in shuffle
39-
4037
const plugin: ResourcePlugin = buildResourcePlugin();
4138

4239
// Run
@@ -60,9 +57,6 @@ describe('ResourcePlugin tests', () => {
6057
});
6158

6259
test('when recordResourceUrl is false then the resource name is not recorded', async () => {
63-
// Setup
64-
mockRandom(0); // Retain order in shuffle
65-
6660
const plugin: ResourcePlugin = buildResourcePlugin();
6761
const mockContext = Object.assign({}, context, {
6862
config: { ...DEFAULT_CONFIG, recordResourceUrl: false }
@@ -162,28 +156,15 @@ describe('ResourcePlugin tests', () => {
162156
const plugin: ResourcePlugin = buildResourcePlugin({ eventLimit: 4 });
163157

164158
// Run
165-
mockRandom(0.99); // Retain order in shuffle
166-
plugin.load(context);
167-
mockRandom(0); // Reverse order in shuffle
168159
plugin.load(context);
169160

170161
// Assert
171162
expect(record.mock.calls[0][1]).toEqual(
172-
expect.objectContaining({
173-
targetUrl: imageResourceEventB.name
174-
})
175-
);
176-
expect(record.mock.calls[1][1]).toEqual(
177163
expect.objectContaining({
178164
targetUrl: imageResourceEventA.name
179165
})
180166
);
181-
expect(record.mock.calls[2][1]).toEqual(
182-
expect.objectContaining({
183-
targetUrl: imageResourceEventA.name
184-
})
185-
);
186-
expect(record.mock.calls[3][1]).toEqual(
167+
expect(record.mock.calls[1][1]).toEqual(
187168
expect.objectContaining({
188169
targetUrl: imageResourceEventB.name
189170
})

src/test-utils/mock-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ export const createDocumentResource = (url: string) => {
166166
};
167167

168168
export const putRumEventsDocument = createDocumentResource(
169-
'https://dataplane.rum.us-west-2.amazonaws.com/application/aa17a42c-e737-48f7-adaf-2e0905f48073/events'
169+
'https://dataplane.rum.us-west-2.amazonaws.com/appmonitors/aa17a42c-e737-48f7-adaf-2e0905f48073'
170170
);
171171
export const putRumEventsGammaDocument = createDocumentResource(
172-
'https://dataplane.rum.us-west-2.amazonaws.com/gamma/application/aa17a42c-e737-48f7-adaf-2e0905f48073/events'
172+
'https://dataplane.rum.us-west-2.amazonaws.com/gamma/appmonitors/aa17a42c-e737-48f7-adaf-2e0905f48073'
173173
);
174174
export const dataPlaneDocument = createDocumentResource(
175175
'https://dataplane.rum.us-west-2.amazonaws.com/user'

src/utils/__tests__/common-utils.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('Common utils tests', () => {
109109
test('when url is has endpoint host and path then it is a PutRumEvents call', async () => {
110110
const endpointHost = 'dataplane.rum.us-west-2.amazonaws.com';
111111
const resourceUrl =
112-
'https://dataplane.rum.us-west-2.amazonaws.com/gamma/application/aa17a42c-e737-48f7-adaf-2e0905f48073/events';
112+
'https://dataplane.rum.us-west-2.amazonaws.com/gamma/appmonitors/aa17a42c-e737-48f7-adaf-2e0905f48073/events';
113113
expect(utils.isPutRumEventsCall(resourceUrl, endpointHost)).toBe(true);
114114
});
115115

@@ -130,8 +130,15 @@ describe('Common utils tests', () => {
130130
test('when url is invalid then it is not a PutRumEvents call', async () => {
131131
const endpointHost = 'dataplane.rum.us-west-2.amazonaws.com';
132132
const resourceUrl =
133-
'dataplane.rum.us-west-2.amazonaws.com/gamma/application/aa17a42c-e737-48f7-adaf-2e0905f48073/events';
133+
'dataplane.rum.us-west-2.amazonaws.com/gamma/appmonitors/aa17a42c-e737-48f7-adaf-2e0905f48073/events';
134134
expect(() => new URL(endpointHost)).toThrowError();
135135
expect(utils.isPutRumEventsCall(resourceUrl, endpointHost)).toBe(false);
136136
});
137+
138+
test('when url has endpoint host, correct path and query params then it is a PutRumEvents call', async () => {
139+
const endpointHost = 'dataplane.rum.us-east-1.amazonaws.com';
140+
const resourceUrl =
141+
'https://dataplane.rum.us-east-1.amazonaws.com/appmonitors/00000000-0000-0000-0000-000000000000?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=<content-sha256>&X-Amz-Credential=<access-key>%2F20251120%2Fus-east-1%2Frum%2Faws4_request&X-Amz-Date=20251120T161054Z&X-Amz-Expires=60';
142+
expect(utils.isPutRumEventsCall(resourceUrl, endpointHost)).toBe(true);
143+
});
137144
});

src/utils/common-utils.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,6 @@ const extensions = [
9090
}
9191
];
9292

93-
export const shuffle = (a: any[]) => {
94-
for (let i = a.length - 1; i > 0; i--) {
95-
const j = Math.floor(Math.random() * (i + 1));
96-
const v = a[i];
97-
a[i] = a[j];
98-
a[j] = v;
99-
}
100-
};
101-
10293
export const getResourceFileType = (
10394
url: string,
10495
initiatorType?: string
@@ -225,7 +216,7 @@ export const isLongTaskSupported = () => {
225216

226217
/** PutRumEvents regex pattern */
227218
const putRumEventsPattern =
228-
/.*\/application\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/events/;
219+
/\/appmonitors\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/;
229220

230221
export const isPutRumEventsCall = (
231222
url: string,

0 commit comments

Comments
 (0)