Skip to content

Commit 143f6f0

Browse files
authored
Support cookie names unique to appmonitor. (#82)
1 parent ece1306 commit 143f6f0

File tree

7 files changed

+60
-6
lines changed

7 files changed

+60
-6
lines changed

docs/cdn_installation.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ The snippet below shows several configuration options with the body of the snipp
7777
| pageIdFormat | String | `'PATH'` | The portion of the `window.location` that will be used as the page ID. Options include `PATH`, `HASH` and `PATH_AND_HASH`.<br/><br/>For example, consider the URL `https://amazonaws.com/home?param=true#content`<br/><br/>`PATH`: `/home`<br/>`HASH`: `#content`<br/>`PATH_AND_HASH`: `/home#content` |
7878
| pagesToInclude | RegExp[] | `[]` | A list of regular expressions which specify the `window.location` values for which the web client will record data. Pages are matched using the `RegExp.test()` function.<br/><br/>For example, when `pagesToInclude: [ /\/home/ ]`, then data from `https://amazonaws.com/home` will be included, and `https://amazonaws.com/` will not be included. |
7979
| pagesToExclude | RegExp[] | `[]` | A list of regular expressions which specify the `window.location` values for which the web client will record data. Pages are matched using the `RegExp.test()` function.<br/><br/>For example, when `pagesToExclude: [ /\/home/ ]`, then data from `https://amazonaws.com/home` will be excluded, and `https://amazonaws.com/` will not be excluded. |
80-
| recordResourceUrl | Boolean | `true` | When this field is `false`, the web client will not record the URLs of resources downloaded by your appliation.<br/><br/> Some types of resources (e.g., profile images) may be referenced by URLs which contain PII. If this applies to your application, you must set this field to `false` to comply with CloudWatch RUM's shared responsibility model. |
80+
| recordResourceUrl | Boolean | `true` | When this field is `false`, the web client will not record the URLs of resources downloaded by your application.<br/><br/> Some types of resources (e.g., profile images) may be referenced by URLs which contain PII. If this applies to your application, you must set this field to `false` to comply with CloudWatch RUM's shared responsibility model. |
8181
| sessionEventLimit | Number | `200` | The maximum number of events to record during a single session. |
8282
| sessionSampleRate | Number | `1` | The proportion of sessions that will be recorded by the web client, specified as a unit interval (a number greater than or equal to 0 and less than or equal to 1). When this field is `0`, no sessions will be recorded. When this field is `1`, all sessions will be recorded. |
8383
| telemetries | [Telemetry Config Array](#telemetry-config-array) | `[]` | See [Telemetry Config Array](#telemetry-config-array) |
@@ -90,6 +90,7 @@ The snippet below shows several configuration options with the body of the snipp
9090
| path | String | `/` | See https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#define_where_cookies_are_sent |
9191
| sameSite | Boolean | `true` | See https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#define_where_cookies_are_sent |
9292
| secure | Boolean | `true` | See https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#define_where_cookies_are_sent |
93+
| unique | Boolean | `false` | When this field is `false`, the session cookie name is `cwr_s`. When this field is true `true`, the session cookie name is `cwr_s_[AppMonitor Id]`.<br/><br/>Set this field to `true` when multiple AppMonitors will monitor the same page. For example, this might be the case if one AppMonitor is used for logged-in users, and a second AppMonitor is used for guest users. |
9394

9495
## Telemetry Config Array
9596

src/event-cache/EventCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Config } from '../orchestration/Orchestration';
55
import { PageManager } from '../sessions/PageManager';
66
import {
77
AppMonitorDetails,
8-
PutRumEventsRequest,
98
UserDetails,
109
RumEvent
1110
} from '../dispatch/dataplane';
@@ -41,6 +40,7 @@ export class EventCache {
4140
this.enabled = true;
4241
this.pageManager = new PageManager(config, this.recordEvent);
4342
this.sessionManager = new SessionManager(
43+
applicationDetails,
4444
config,
4545
this.recordSessionInitEvent,
4646
this.pageManager

src/orchestration/Orchestration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export enum PAGE_ID_FORMAT {
4343
}
4444

4545
export type PartialCookieAttributes = {
46+
unique?: boolean;
4647
domain?: string;
4748
path?: string;
4849
sameSite?: string;
@@ -88,6 +89,7 @@ export type PartialConfig = {
8889

8990
export const defaultCookieAttributes = (): CookieAttributes => {
9091
return {
92+
unique: false,
9193
domain: window.location.hostname,
9294
path: '/',
9395
sameSite: 'Strict',
@@ -120,6 +122,7 @@ export const defaultConfig = (cookieAttributes: CookieAttributes): Config => {
120122
};
121123

122124
export type CookieAttributes = {
125+
unique: boolean;
123126
domain: string;
124127
path: string;
125128
sameSite: string;

src/orchestration/__tests__/Orchestration.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Config, Orchestration } from '../Orchestration';
1+
import { Orchestration } from '../Orchestration';
22
import { Dispatch } from '../../dispatch/Dispatch';
33
import { EventCache } from '../../event-cache/EventCache';
44
import { DomEventPlugin } from '../../plugins/event-plugins/DomEventPlugin';
@@ -133,6 +133,7 @@ describe('Orchestration tests', () => {
133133
allowCookies: false,
134134
batchLimit: 100,
135135
cookieAttributes: {
136+
unique: false,
136137
domain: window.location.hostname,
137138
path: '/',
138139
sameSite: 'Strict',

src/sessions/SessionManager.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Page, PageManager } from './PageManager';
66

77
import { UAParser } from 'ua-parser-js';
88
import { SESSION_COOKIE_NAME, USER_COOKIE_NAME } from '../utils/constants';
9+
import { AppMonitorDetails } from '../dispatch/dataplane';
910

1011
export const NIL_UUID = '00000000-0000-0000-0000-000000000000';
1112

@@ -58,6 +59,7 @@ export type Attributes = {
5859
export class SessionManager {
5960
private pageManager: PageManager;
6061

62+
private appMonitorDetails: AppMonitorDetails;
6163
private userExpiry: Date;
6264
private sessionExpiry: Date;
6365
private userId!: string;
@@ -67,10 +69,12 @@ export class SessionManager {
6769
private attributes: Attributes;
6870

6971
constructor(
72+
appMonitorDetails: AppMonitorDetails,
7073
config: Config,
7174
record: RecordSessionInitEvent,
7275
pageManager: PageManager
7376
) {
77+
this.appMonitorDetails = appMonitorDetails;
7478
this.config = config;
7579
this.record = record;
7680
this.pageManager = pageManager;
@@ -155,7 +159,7 @@ export class SessionManager {
155159
private createOrRenewSessionCookie(session: Session, expires: Date) {
156160
if (btoa) {
157161
storeCookie(
158-
SESSION_COOKIE_NAME,
162+
this.sessionCookieName(),
159163
btoa(JSON.stringify(session)),
160164
this.config.cookieAttributes,
161165
undefined,
@@ -180,7 +184,7 @@ export class SessionManager {
180184

181185
private getSessionFromCookie() {
182186
if (this.useCookies()) {
183-
const cookie: string = getCookie(SESSION_COOKIE_NAME);
187+
const cookie: string = getCookie(this.sessionCookieName());
184188

185189
if (cookie && atob) {
186190
try {
@@ -262,4 +266,11 @@ export class SessionManager {
262266
private sample(): boolean {
263267
return Math.random() < this.config.sessionSampleRate;
264268
}
269+
270+
private sessionCookieName(): string {
271+
if (this.config.cookieAttributes.unique) {
272+
return `${SESSION_COOKIE_NAME}_${this.appMonitorDetails.id}`;
273+
}
274+
return SESSION_COOKIE_NAME;
275+
}
265276
}

src/sessions/__tests__/SessionManager.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import { Config } from '../../orchestration/Orchestration';
1717
import { mockRandom } from 'jest-mock-random';
1818
import { PageManager } from '../PageManager';
1919
import { SESSION_COOKIE_NAME, USER_COOKIE_NAME } from '../../utils/constants';
20-
import { DEFAULT_CONFIG } from '../../test-utils/test-utils';
20+
import {
21+
APPLICATION_ID,
22+
APP_MONITOR_DETAILS,
23+
DEFAULT_CONFIG
24+
} from '../../test-utils/test-utils';
2125

2226
const NAVIGATION = 'navigation';
2327
const SESSION_COOKIE_EXPIRES = 30 * 60;
@@ -54,6 +58,7 @@ const mockRecord = jest.fn();
5458

5559
const defaultSessionManager = (config) => {
5660
return new SessionManager(
61+
APP_MONITOR_DETAILS,
5762
config,
5863
mockRecord,
5964
new PageManager(config, mockRecord)
@@ -347,6 +352,7 @@ describe('SessionManager tests', () => {
347352
...{ sessionLengthSeconds: 3600, allowCookies: true }
348353
};
349354
const sessionManager = new SessionManager(
355+
APP_MONITOR_DETAILS,
350356
config,
351357
mockRecord,
352358
new PageManager(config, mockRecord)
@@ -632,4 +638,31 @@ describe('SessionManager tests', () => {
632638
expect(userIdFromCookie1).toEqual('');
633639
expect(userIdFromCookie2).toEqual(sessionManager.getUserId());
634640
});
641+
642+
test('when unique cookie names are used then the application id is appended to the cookie names', async () => {
643+
// Init
644+
const config = {
645+
...DEFAULT_CONFIG,
646+
...{
647+
allowCookies: true,
648+
userIdRetentionDays: 1,
649+
cookieAttributes: {
650+
...DEFAULT_CONFIG.cookieAttributes,
651+
...{ unique: true }
652+
}
653+
}
654+
};
655+
656+
const sessionManager = defaultSessionManager(config);
657+
658+
sessionManager.getSession();
659+
const sessionIdFromCookie = JSON.parse(
660+
atob(getCookie(`${SESSION_COOKIE_NAME}_${APPLICATION_ID}`))
661+
).sessionId;
662+
663+
// Assert
664+
expect(sessionIdFromCookie).toEqual(
665+
sessionManager.getSession().sessionId
666+
);
667+
});
635668
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as utils from '../../utils/cookies-utils';
33
const COOKIE_PREFIX = 'rum_cookies_util_test';
44

55
const COOKIE_ATTRIBUTES = {
6+
unique: false,
67
domain: 'amazon.com',
78
path: '/',
89
sameSite: 'Strict',
@@ -62,13 +63,15 @@ describe('Cookies utils tests', () => {
6263
const cookieValueB = 'valueB';
6364

6465
const COOKIE_ATTRIBUTES_A = {
66+
unique: false,
6567
domain: 'amazon.com',
6668
path: '/',
6769
sameSite: 'Strict',
6870
secure: true
6971
};
7072

7173
const COOKIE_ATTRIBUTES_B = {
74+
unique: false,
7275
domain: 'aws.amazon.com',
7376
path: '/',
7477
sameSite: 'Strict',
@@ -96,13 +99,15 @@ describe('Cookies utils tests', () => {
9699
const cookieValueB = 'valueB';
97100

98101
const COOKIE_ATTRIBUTES_A = {
102+
unique: false,
99103
domain: 'amazon.com',
100104
path: '/',
101105
sameSite: 'Strict',
102106
secure: true
103107
};
104108

105109
const COOKIE_ATTRIBUTES_B = {
110+
unique: false,
106111
domain: 'amazon.com',
107112
path: '/console',
108113
sameSite: 'Strict',

0 commit comments

Comments
 (0)