Skip to content

Commit 533f0bf

Browse files
jhackshawJeff Hackshawqhanam
authored
feat: allow customization of default attributes (#430)
Co-authored-by: Jeff Hackshaw <[email protected]> Co-authored-by: Quinn Hanam <[email protected]>
1 parent c501375 commit 533f0bf

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

docs/configuration.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ custom attributes with the `aws:` prefix, or they may be overwritten by future
6767
versions of the CloudWatch RUM web client.
6868

6969
The RUM web client also records a set of [default
70-
attributes](https://github.com/aws-observability/aws-rum-web/blob/main/src/event-schemas/meta-data.json).
71-
You cannot overwrite default attributes with custom attributes.
70+
attributes](https://github.com/aws-observability/aws-rum-web/blob/main/src/event-schemas/meta-data.json). Overriding default attributes can have unintended consequences in the Cloudwatch RUM console.
7271

7372
| Field Name | Type | Default | Description |
7473
| --- | --- | --- | --- |

src/event-cache/__tests__/EventCache.integ.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('EventCache tests', () => {
7575
});
7676
});
7777

78-
test('meta data contains default attributes not overridden from custom attributes', async () => {
78+
test('default meta data can be overriden by custom attributes', async () => {
7979
// Init
8080
const EVENT1_SCHEMA = 'com.amazon.rum.event1';
8181
const config = {
@@ -88,7 +88,8 @@ describe('EventCache tests', () => {
8888
domain: 'overridden.console.aws.amazon.com',
8989
browserLanguage: 'en-UK',
9090
browserName: 'Chrome',
91-
deviceType: 'Mac'
91+
deviceType: 'Mac',
92+
platformType: 'other'
9293
}
9394
}
9495
};
@@ -99,10 +100,10 @@ describe('EventCache tests', () => {
99100
'aws:client': INSTALL_MODULE,
100101
'aws:clientVersion': WEB_CLIENT_VERSION,
101102
domain: 'overridden.console.aws.amazon.com',
102-
browserLanguage: 'en-US',
103-
browserName: 'WebKit',
104-
deviceType: 'desktop',
105-
platformType: 'web',
103+
browserLanguage: 'en-UK',
104+
browserName: 'Chrome',
105+
deviceType: 'Mac',
106+
platformType: 'other',
106107
pageId: '/console/home'
107108
};
108109

src/sessions/SessionManager.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ export class SessionManager {
139139
public addSessionAttributes(sessionAttributes: {
140140
[k: string]: string | number | boolean;
141141
}) {
142-
this.attributes = { ...sessionAttributes, ...this.attributes };
143-
this.attributes.domain =
144-
(sessionAttributes.domain as string) || this.attributes.domain;
142+
this.attributes = { ...this.attributes, ...sessionAttributes };
145143
}
146144

147145
public getUserId(): string {

src/sessions/__tests__/SessionManager.test.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,16 +810,40 @@ describe('SessionManager tests', () => {
810810
});
811811

812812
test('when domain is in custom session attributes then domain is overridden', async () => {
813+
// Init
813814
const sessionManager = defaultSessionManager({
814815
...DEFAULT_CONFIG
815816
});
816817

817-
sessionManager.addSessionAttributes({
818-
domain: 'overridden'
818+
const sessionAttributes = {
819+
domain: 'preferred.domain'
820+
};
821+
822+
sessionManager.addSessionAttributes(sessionAttributes);
823+
824+
const actualSessionAttributes = sessionManager.getAttributes();
825+
826+
// Assert
827+
expect(actualSessionAttributes.domain).toEqual(
828+
sessionAttributes.domain
829+
);
830+
});
831+
832+
test('when custom session attributes have the same key as built in attributies then custom session attributes are used', async () => {
833+
// Init
834+
const sessionManager = defaultSessionManager({
835+
...DEFAULT_CONFIG
819836
});
820837

838+
const sessionAttributes = {
839+
title: 'override'
840+
};
841+
842+
sessionManager.addSessionAttributes(sessionAttributes);
843+
821844
const actualSessionAttributes = sessionManager.getAttributes();
822845

823-
expect(actualSessionAttributes.domain).toEqual('overridden');
846+
// Assert
847+
expect(actualSessionAttributes.title).toEqual(sessionAttributes.title);
824848
});
825849
});

0 commit comments

Comments
 (0)