Skip to content

Commit 5ae5804

Browse files
authored
feat: Report metric errors (#110)
1 parent 257144b commit 5ae5804

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

src/internal/base-component/__tests__/panorama-metrics.test.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,61 @@ describe('PanoramaClient', () => {
4444
const eventDetail = 'a'.repeat(4001);
4545
panorama.sendMetric({ eventType: 'custom', eventDetail });
4646

47-
expect(window.panorama).not.toHaveBeenCalled();
47+
expect(window.panorama).toHaveBeenCalledTimes(1);
48+
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
49+
eventName: 'awsui-metric-error',
50+
eventDetail: expect.stringMatching(/Event detail for metric is too long:.*/),
51+
});
4852
expect(consoleSpy).toHaveBeenCalledWith(`Event detail for metric is too long: ${eventDetail}`);
4953
});
5054

5155
test('prints an error when event type is too long', () => {
5256
const eventType = 'a'.repeat(51);
57+
const errorMessage = `Event type for metric is too long: ${eventType}`;
5358
panorama.sendMetric({ eventType });
5459

55-
expect(window.panorama).not.toHaveBeenCalled();
56-
expect(consoleSpy).toHaveBeenCalledWith(`Event type for metric is too long: ${eventType}`);
60+
expect(window.panorama).toHaveBeenCalledTimes(1);
61+
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
62+
eventName: 'awsui-metric-error',
63+
eventDetail: errorMessage,
64+
});
65+
expect(consoleSpy).toHaveBeenCalledWith(errorMessage);
5766
});
5867

5968
test('prints an error when event name is too long', () => {
6069
const eventName = 'a'.repeat(1001);
70+
const errorMessage = `Event name for metric is too long: ${eventName}`;
6171
panorama.sendMetric({ eventName });
6272

63-
expect(window.panorama).not.toHaveBeenCalled();
64-
expect(consoleSpy).toHaveBeenCalledWith(`Event name for metric is too long: ${eventName}`);
73+
expect(window.panorama).toHaveBeenCalledTimes(1);
74+
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
75+
eventName: 'awsui-metric-error',
76+
eventDetail: errorMessage,
77+
});
78+
expect(consoleSpy).toHaveBeenCalledWith(errorMessage);
6579
});
6680

6781
test('prints an error when event value is too long', () => {
6882
const eventValue = 'a'.repeat(4001);
6983
panorama.sendMetric({ eventValue });
7084

71-
expect(window.panorama).not.toHaveBeenCalled();
85+
expect(window.panorama).toHaveBeenCalledTimes(1);
86+
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
87+
eventName: 'awsui-metric-error',
88+
eventDetail: expect.stringMatching(/Event value for metric is too long:.*/),
89+
});
7290
expect(consoleSpy).toHaveBeenCalledWith(`Event value for metric is too long: ${eventValue}`);
7391
});
7492

7593
test('prints an error when event context is too long', () => {
7694
const eventContext = 'a'.repeat(4001);
7795
panorama.sendMetric({ eventContext });
7896

79-
expect(window.panorama).not.toHaveBeenCalled();
97+
expect(window.panorama).toHaveBeenCalledTimes(1);
98+
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
99+
eventName: 'awsui-metric-error',
100+
eventDetail: expect.stringMatching(/Event context for metric is too long:.*/),
101+
});
80102
expect(consoleSpy).toHaveBeenCalledWith(`Event context for metric is too long: ${eventContext}`);
81103
});
82104

src/internal/base-component/metrics/log-clients.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export interface MetricsV2EventItem {
1919
timestamp?: number;
2020
}
2121

22+
type PanoramaFunction = (event: 'trackCustomEvent', data: MetricsV2EventItem) => void;
23+
2224
function validateLength(value: string | undefined, maxLength: number): boolean {
2325
return !value || value.length <= maxLength;
2426
}
@@ -86,7 +88,7 @@ export class PanoramaClient {
8688
*/
8789
sendMetric(metric: MetricsV2EventItem): boolean {
8890
const panorama = this.findPanorama(window);
89-
if (typeof panorama !== 'function') {
91+
if (!panorama) {
9092
return false;
9193
}
9294
if (typeof metric.eventDetail === 'object') {
@@ -96,30 +98,41 @@ export class PanoramaClient {
9698
metric.eventValue = JSON.stringify(metric.eventValue);
9799
}
98100
if (!validateLength(metric.eventName, 1000)) {
99-
console.error(`Event name for metric is too long: ${metric.eventName}`);
101+
this.onMetricError(`Event name for metric is too long: ${metric.eventName}`);
100102
return true;
101103
}
102104
if (!validateLength(metric.eventDetail, 4000)) {
103-
console.error(`Event detail for metric is too long: ${metric.eventDetail}`);
105+
this.onMetricError(`Event detail for metric is too long: ${metric.eventDetail}`);
104106
return true;
105107
}
106108
if (!validateLength(metric.eventValue, 4000)) {
107-
console.error(`Event value for metric is too long: ${metric.eventValue}`);
109+
this.onMetricError(`Event value for metric is too long: ${metric.eventValue}`);
108110
return true;
109111
}
110112
if (!validateLength(metric.eventContext, 4000)) {
111-
console.error(`Event context for metric is too long: ${metric.eventContext}`);
113+
this.onMetricError(`Event context for metric is too long: ${metric.eventContext}`);
112114
return true;
113115
}
114116
if (!validateLength(metric.eventType, 50)) {
115-
console.error(`Event type for metric is too long: ${metric.eventType}`);
117+
this.onMetricError(`Event type for metric is too long: ${metric.eventType}`);
116118
return true;
117119
}
118120
panorama('trackCustomEvent', { timestamp: Date.now(), ...metric });
119121
return true;
120122
}
121123

122-
private findPanorama(currentWindow?: MetricsWindow): any | undefined {
124+
private onMetricError(message: string) {
125+
console.error(message);
126+
const panorama = this.findPanorama(window);
127+
if (panorama) {
128+
panorama('trackCustomEvent', {
129+
eventName: 'awsui-metric-error',
130+
eventDetail: message.slice(0, 4000),
131+
});
132+
}
133+
}
134+
135+
private findPanorama(currentWindow?: MetricsWindow): PanoramaFunction | undefined {
123136
try {
124137
if (typeof currentWindow?.panorama === 'function') {
125138
return currentWindow?.panorama;

0 commit comments

Comments
 (0)