Skip to content

Commit 0f403f5

Browse files
committed
feat: Support use of alias field
1 parent d21d462 commit 0f403f5

File tree

9 files changed

+203
-3
lines changed

9 files changed

+203
-3
lines changed

app/alias_included.html

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>RUM Integ Test</title>
5+
<script src="./loader_alias.js"></script>
6+
<link
7+
rel="icon"
8+
type="image/png"
9+
href="https://awsmedia.s3.amazonaws.com/favicon.ico"
10+
/>
11+
12+
<script>
13+
// Common to all test pages
14+
function dispatch() {
15+
cwr('dispatch');
16+
}
17+
18+
function clearRequestResponse() {
19+
document.getElementById('request_url').innerText = '';
20+
document.getElementById('request_header').innerText = '';
21+
document.getElementById('request_body').innerText = '';
22+
23+
document.getElementById('response_status').innerText = '';
24+
document.getElementById('response_header').innerText = '';
25+
document.getElementById('response_body').innerText = '';
26+
}
27+
28+
function disable() {
29+
cwr('disable');
30+
}
31+
32+
function enable() {
33+
cwr('enable');
34+
}
35+
</script>
36+
37+
<style>
38+
table {
39+
border-collapse: collapse;
40+
margin-top: 10px;
41+
margin-bottom: 10px;
42+
}
43+
44+
td,
45+
th {
46+
border: 1px solid black;
47+
text-align: left;
48+
padding: 8px;
49+
}
50+
</style>
51+
</head>
52+
<body>
53+
<p id="welcome">This application is used for RUM integ testing.</p>
54+
55+
<button id="disable" onclick="disable()">Disable</button>
56+
<button id="enable" onclick="enable()">Enable</button>
57+
<hr />
58+
<button id="dispatch" onclick="dispatch()">Dispatch</button>
59+
<button id="testButton">Test Button</button>
60+
61+
<hr />
62+
<span id="request"></span>
63+
<span id="response"></span>
64+
<table>
65+
<tr>
66+
<td>Request URL</td>
67+
<td id="request_url"></td>
68+
</tr>
69+
<tr>
70+
<td>Request Header</td>
71+
<td id="request_header"></td>
72+
</tr>
73+
<tr>
74+
<td>Request Body</td>
75+
<td id="request_body"></td>
76+
</tr>
77+
</table>
78+
<table>
79+
<tr>
80+
<td>Response Status Code</td>
81+
<td id="response_status"></td>
82+
</tr>
83+
<tr>
84+
<td>Response Header</td>
85+
<td id="response_header"></td>
86+
</tr>
87+
<tr>
88+
<td>Response Body</td>
89+
<td id="response_body"></td>
90+
</tr>
91+
</table>
92+
<img src="blank.png" alt="sample" />
93+
</body>
94+
</html>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Selector } from 'testcafe';
2+
import {
3+
STATUS_202,
4+
REQUEST_BODY,
5+
RESPONSE_STATUS
6+
} from '../test-utils/integ-test-utils';
7+
8+
const testButton: Selector = Selector(`#testButton`);
9+
const dispatch: Selector = Selector(`#dispatch`);
10+
11+
fixture('Alias Included').page('http://localhost:8080/alias_included.html');
12+
13+
test('when alias is included and events are recorded, PutRumEvents requests contains the alias', async (t: TestController) => {
14+
const browser = t.browser.name;
15+
// Skip firefox, till Firefox supports longtasks
16+
if (browser === 'Firefox') {
17+
return 'Test is skipped';
18+
}
19+
20+
await t
21+
.click(testButton)
22+
.wait(100)
23+
.click(dispatch)
24+
.wait(3000)
25+
.click(dispatch)
26+
.expect(RESPONSE_STATUS.textContent)
27+
.eql(STATUS_202.toString())
28+
.expect(REQUEST_BODY.textContent)
29+
.contains('BatchId');
30+
31+
const requestBody = JSON.parse(await REQUEST_BODY.textContent);
32+
await t.expect(requestBody['Alias']).eql('test123');
33+
});

src/dispatch/DataPlaneClient.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ declare type SerializedPutRumEventsRequest = {
3535
AppMonitorDetails: AppMonitorDetails;
3636
UserDetails: UserDetails;
3737
RumEvents: SerializedRumEvent[];
38+
Alias?: string;
3839
};
3940

4041
export declare type DataPlaneClientConfig = {
@@ -147,12 +148,15 @@ const serializeRequest = (
147148
request.RumEvents.forEach((e) =>
148149
serializedRumEvents.push(serializeEvent(e))
149150
);
150-
const serializedRequest: SerializedPutRumEventsRequest = {
151+
let serializedRequest: SerializedPutRumEventsRequest = {
151152
BatchId: request.BatchId,
152153
AppMonitorDetails: request.AppMonitorDetails,
153154
UserDetails: request.UserDetails,
154155
RumEvents: serializedRumEvents
155156
};
157+
if (request.Alias) {
158+
serializedRequest = { ...serializedRequest, Alias: request.Alias };
159+
}
156160
return serializedRequest;
157161
};
158162

src/dispatch/Dispatch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ export class Dispatch {
228228
BatchId: v4(),
229229
AppMonitorDetails: this.eventCache.getAppMonitorDetails(),
230230
UserDetails: this.eventCache.getUserDetails(),
231-
RumEvents: this.eventCache.getEventBatch()
231+
RumEvents: this.eventCache.getEventBatch(),
232+
Alias: this.config.alias
232233
};
233234
}
234235

src/dispatch/__tests__/Dispatch.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,4 +558,57 @@ describe('Dispatch tests', () => {
558558
expect(DataPlaneClient).toHaveBeenCalled();
559559
expect(sendFetch).toHaveBeenCalledTimes(1);
560560
});
561+
562+
test('When valid alias is provided then PutRumEvents request containing alias is sent', async () => {
563+
// Init
564+
const dispatch = new Dispatch(
565+
Utils.AWS_RUM_REGION,
566+
Utils.AWS_RUM_ENDPOINT,
567+
Utils.createDefaultEventCacheWithEvents(),
568+
{
569+
...DEFAULT_CONFIG,
570+
...{
571+
dispatchInterval: Utils.AUTO_DISPATCH_OFF,
572+
signing: false,
573+
alias: 'test123'
574+
}
575+
}
576+
);
577+
578+
// Run
579+
await expect(dispatch.dispatchFetch()).resolves.toBe(undefined);
580+
581+
// Assert
582+
expect(dispatch['createRequest']()['Alias']).toBeDefined();
583+
expect(dispatch['createRequest']()['Alias']).toBe('test123');
584+
585+
expect(DataPlaneClient).toHaveBeenCalled();
586+
expect(sendFetch).toHaveBeenCalledTimes(1);
587+
});
588+
589+
test('When no alias is provided then PutRumEvents request does not contain an alias', async () => {
590+
// Init
591+
const dispatch = new Dispatch(
592+
Utils.AWS_RUM_REGION,
593+
Utils.AWS_RUM_ENDPOINT,
594+
Utils.createDefaultEventCacheWithEvents(),
595+
{
596+
...DEFAULT_CONFIG,
597+
...{
598+
dispatchInterval: Utils.AUTO_DISPATCH_OFF,
599+
signing: false
600+
}
601+
}
602+
);
603+
604+
// Run
605+
await expect(dispatch.dispatchFetch()).resolves.toBe(undefined);
606+
607+
// Assert
608+
609+
expect(dispatch['createRequest']()['Alias']).toBeUndefined();
610+
611+
expect(DataPlaneClient).toHaveBeenCalled();
612+
expect(sendFetch).toHaveBeenCalledTimes(1);
613+
});
561614
});

src/dispatch/dataplane.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface PutRumEventsRequest {
1212
AppMonitorDetails: AppMonitorDetails;
1313
UserDetails: UserDetails;
1414
RumEvents: RumEvent[];
15+
Alias?: string;
1516
}
1617

1718
export interface AppMonitorDetails {

src/loader/loader-alias.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { loader } from './loader';
2+
import { showRequestClientBuilder } from '../test-utils/mock-http-handler';
3+
loader('cwr', 'abc123', '1.0', 'us-west-2', './rum_javascript_telemetry.js', {
4+
userIdRetentionDays: 1,
5+
dispatchInterval: 0,
6+
allowCookies: false,
7+
eventPluginsToLoad: [],
8+
telemetries: [],
9+
clientBuilder: showRequestClientBuilder,
10+
signing: false,
11+
alias: 'test123'
12+
});

src/orchestration/Orchestration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export interface Config {
159159
telemetries: Telemetry[];
160160
useBeacon: boolean;
161161
userIdRetentionDays: number;
162+
alias?: string;
162163
}
163164

164165
export interface PartialConfig

webpack/webpack.dev.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ module.exports = merge(common, {
3030
'./src/loader/loader-post-load-command-queue-test.js',
3131
loader_remote_config: './src/loader/loader-remote-config.js',
3232
loader_spa: './src/loader/loader-spa.js',
33-
loader_custom_events: './src/loader/loader-custom-events.js'
33+
loader_custom_events: './src/loader/loader-custom-events.js',
34+
loader_alias: './src/loader/loader-alias.js'
3435
},
3536
resolve: {
3637
extensions: ['.ts', '.js', '.json']

0 commit comments

Comments
 (0)