Skip to content

Commit 9b48c5e

Browse files
authored
chore: Improve metrics format (#117)
1 parent 3c08c6f commit 9b48c5e

File tree

3 files changed

+62
-69
lines changed

3 files changed

+62
-69
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ describe('Client Metrics support', () => {
2828

2929
const checkMetric = (metricName: string, detailObject: MetricDetail) => {
3030
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
31-
eventName: metricName,
31+
eventType: 'awsui',
32+
eventContext: metricName,
3233
eventDetail: JSON.stringify(detailObject),
3334
eventValue: '1',
3435
timestamp: expect.any(Number),
@@ -97,7 +98,8 @@ describe('Client Metrics support', () => {
9798

9899
metrics.sendMetric('name', 0, undefined);
99100
expect(window.parent.panorama).toHaveBeenCalledWith('trackCustomEvent', {
100-
eventName: 'name',
101+
eventType: 'awsui',
102+
eventContext: 'name',
101103
eventValue: '0',
102104
timestamp: expect.any(Number),
103105
});
@@ -110,7 +112,8 @@ describe('Client Metrics support', () => {
110112
test('delegates to window.panorama when defined', () => {
111113
metrics.sendMetric('name', 0, undefined);
112114
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
113-
eventName: 'name',
115+
eventType: 'awsui',
116+
eventContext: 'name',
114117
eventValue: '0',
115118
timestamp: expect.any(Number),
116119
});
@@ -121,7 +124,8 @@ describe('Client Metrics support', () => {
121124
test(`calls window.panorama when valid metric name used (${metricName})`, () => {
122125
metrics.sendMetric(metricName, 1, 'detail');
123126
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
124-
eventName: metricName,
127+
eventType: 'awsui',
128+
eventContext: metricName,
125129
eventValue: '1',
126130
eventDetail: 'detail',
127131
timestamp: expect.any(Number),
@@ -163,7 +167,8 @@ describe('Client Metrics support', () => {
163167
const validDetail = 'a'.repeat(4000);
164168
metrics.sendMetric('metricName', 1, validDetail);
165169
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
166-
eventName: 'metricName',
170+
eventType: 'awsui',
171+
eventContext: 'metricName',
167172
eventValue: '1',
168173
eventDetail: validDetail,
169174
timestamp: expect.any(Number),
@@ -186,7 +191,8 @@ describe('Client Metrics support', () => {
186191
test('logs a metric name only once', () => {
187192
metrics.sendMetricOnce('my-event', 1);
188193
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
189-
eventName: 'my-event',
194+
eventType: 'awsui',
195+
eventContext: 'my-event',
190196
eventValue: '1',
191197
timestamp: expect.any(Number),
192198
});
@@ -200,12 +206,14 @@ describe('Client Metrics support', () => {
200206
metrics.sendMetricOnce('my-event', 1);
201207
metrics.sendMetricOnce('My-Event', 2);
202208
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
203-
eventName: 'my-event',
209+
eventType: 'awsui',
210+
eventContext: 'my-event',
204211
eventValue: '1',
205212
timestamp: expect.any(Number),
206213
});
207214
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
208-
eventName: 'My-Event',
215+
eventType: 'awsui',
216+
eventContext: 'My-Event',
209217
eventValue: '2',
210218
timestamp: expect.any(Number),
211219
});
@@ -218,8 +226,9 @@ describe('Client Metrics support', () => {
218226
window.AWSC = undefined;
219227
metrics.sendMetricObject({ source: 'pkg', action: 'used', version: '5.0' }, 1);
220228
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
229+
eventType: 'awsui',
230+
eventContext: 'awsui_pkg_d50',
221231
eventDetail: '{"o":"main","s":"pkg","t":"default","a":"used","f":"react","v":"5.0"}',
222-
eventName: 'awsui_pkg_d50',
223232
eventValue: '1',
224233
timestamp: expect.any(Number),
225234
});

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

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,69 +23,47 @@ describe('PanoramaClient', () => {
2323
});
2424

2525
test('sends simple metrics', () => {
26-
panorama.sendMetric({ eventType: 'custom', eventValue: 'value' });
26+
panorama.sendMetric({ eventValue: 'value' });
2727

2828
expect(window.panorama).toHaveBeenCalledWith(
2929
'trackCustomEvent',
30-
expect.objectContaining({ eventType: 'custom', eventValue: 'value' })
30+
expect.objectContaining({ eventType: 'awsui', eventValue: 'value' })
3131
);
3232
});
3333

3434
test('converts objects to strings', () => {
35-
panorama.sendMetric({ eventType: 'custom', eventValue: { test: 'value' }, eventDetail: { test: 'detail' } });
35+
panorama.sendMetric({ eventValue: { test: 'value' }, eventDetail: { test: 'detail' } });
3636

3737
expect(window.panorama).toHaveBeenCalledWith(
3838
'trackCustomEvent',
39-
expect.objectContaining({ eventType: 'custom', eventValue: '{"test":"value"}', eventDetail: '{"test":"detail"}' })
39+
expect.objectContaining({ eventValue: '{"test":"value"}', eventDetail: '{"test":"detail"}' })
4040
);
4141
});
4242

4343
test('prints an error when event details are too long', () => {
4444
const eventDetail = 'a'.repeat(4001);
45-
panorama.sendMetric({ eventType: 'custom', eventDetail });
45+
panorama.sendMetric({ eventContext: 'custom', eventDetail });
4646

4747
expect(window.panorama).toHaveBeenCalledTimes(1);
4848
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
49-
eventName: 'awsui-metric-error',
49+
eventType: 'awsui',
50+
eventContext: 'awsui-metric-error',
5051
eventDetail: expect.stringMatching(/Event detail for metric is too long:.*/),
52+
timestamp: expect.any(Number),
5153
});
5254
expect(consoleSpy).toHaveBeenCalledWith(`Event detail for metric is too long: ${eventDetail}`);
5355
});
5456

55-
test('prints an error when event type is too long', () => {
56-
const eventType = 'a'.repeat(51);
57-
const errorMessage = `Event type for metric is too long: ${eventType}`;
58-
panorama.sendMetric({ eventType });
59-
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);
66-
});
67-
68-
test('prints an error when event name is too long', () => {
69-
const eventName = 'a'.repeat(1001);
70-
const errorMessage = `Event name for metric is too long: ${eventName}`;
71-
panorama.sendMetric({ eventName });
72-
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);
79-
});
80-
8157
test('prints an error when event value is too long', () => {
8258
const eventValue = 'a'.repeat(4001);
8359
panorama.sendMetric({ eventValue });
8460

8561
expect(window.panorama).toHaveBeenCalledTimes(1);
8662
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
87-
eventName: 'awsui-metric-error',
63+
eventType: 'awsui',
64+
eventContext: 'awsui-metric-error',
8865
eventDetail: expect.stringMatching(/Event value for metric is too long:.*/),
66+
timestamp: expect.any(Number),
8967
});
9068
expect(consoleSpy).toHaveBeenCalledWith(`Event value for metric is too long: ${eventValue}`);
9169
});
@@ -96,8 +74,10 @@ describe('PanoramaClient', () => {
9674

9775
expect(window.panorama).toHaveBeenCalledTimes(1);
9876
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
99-
eventName: 'awsui-metric-error',
77+
eventType: 'awsui',
78+
eventContext: 'awsui-metric-error',
10079
eventDetail: expect.stringMatching(/Event context for metric is too long:.*/),
80+
timestamp: expect.any(Number),
10181
});
10282
expect(consoleSpy).toHaveBeenCalledWith(`Event context for metric is too long: ${eventContext}`);
10383
});

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@ interface MetricsWindow extends Window {
1111
}
1212

1313
export interface MetricsV2EventItem {
14-
eventName?: string;
15-
eventType?: string;
1614
eventContext?: string;
1715
eventDetail?: string | Record<string, string | number | boolean>;
1816
eventValue?: string | Record<string, string | number | boolean>;
1917
timestamp?: number;
2018
}
2119

22-
type PanoramaFunction = (event: 'trackCustomEvent', data: MetricsV2EventItem) => void;
20+
interface PanoramaMetric {
21+
eventType: string;
22+
eventContext?: string;
23+
eventValue?: string;
24+
eventDetail?: string;
25+
timestamp: number;
26+
}
27+
28+
type PanoramaFunction = (event: 'trackCustomEvent', data: PanoramaMetric) => void;
29+
30+
const AWSUI_EVENT = 'awsui';
2331

2432
function validateLength(value: string | undefined, maxLength: number): boolean {
2533
return !value || value.length <= maxLength;
@@ -46,7 +54,7 @@ export class CLogClient {
4654
return;
4755
}
4856
const wasSent = new PanoramaClient().sendMetric({
49-
eventName: metricName,
57+
eventContext: metricName,
5058
eventDetail: detail,
5159
eventValue: `${value}`,
5260
timestamp: Date.now(),
@@ -91,33 +99,27 @@ export class PanoramaClient {
9199
if (!panorama) {
92100
return false;
93101
}
94-
if (typeof metric.eventDetail === 'object') {
95-
metric.eventDetail = JSON.stringify(metric.eventDetail);
96-
}
97-
if (typeof metric.eventValue === 'object') {
98-
metric.eventValue = JSON.stringify(metric.eventValue);
99-
}
100-
if (!validateLength(metric.eventName, 1000)) {
101-
this.onMetricError(`Event name for metric is too long: ${metric.eventName}`);
102-
return true;
103-
}
104-
if (!validateLength(metric.eventDetail, 4000)) {
105-
this.onMetricError(`Event detail for metric is too long: ${metric.eventDetail}`);
106-
return true;
107-
}
108-
if (!validateLength(metric.eventValue, 4000)) {
109-
this.onMetricError(`Event value for metric is too long: ${metric.eventValue}`);
102+
const payload: PanoramaMetric = {
103+
eventType: AWSUI_EVENT,
104+
timestamp: Date.now(),
105+
...metric,
106+
eventDetail: typeof metric.eventDetail === 'object' ? JSON.stringify(metric.eventDetail) : metric.eventDetail,
107+
eventValue: typeof metric.eventValue === 'object' ? JSON.stringify(metric.eventValue) : metric.eventValue,
108+
};
109+
110+
if (!validateLength(payload.eventDetail, 4000)) {
111+
this.onMetricError(`Event detail for metric is too long: ${payload.eventDetail}`);
110112
return true;
111113
}
112-
if (!validateLength(metric.eventContext, 4000)) {
113-
this.onMetricError(`Event context for metric is too long: ${metric.eventContext}`);
114+
if (!validateLength(payload.eventValue, 4000)) {
115+
this.onMetricError(`Event value for metric is too long: ${payload.eventValue}`);
114116
return true;
115117
}
116-
if (!validateLength(metric.eventType, 50)) {
117-
this.onMetricError(`Event type for metric is too long: ${metric.eventType}`);
118+
if (!validateLength(payload.eventContext, 4000)) {
119+
this.onMetricError(`Event context for metric is too long: ${payload.eventContext}`);
118120
return true;
119121
}
120-
panorama('trackCustomEvent', { timestamp: Date.now(), ...metric });
122+
panorama('trackCustomEvent', payload);
121123
return true;
122124
}
123125

@@ -126,8 +128,10 @@ export class PanoramaClient {
126128
const panorama = this.findPanorama(window);
127129
if (panorama) {
128130
panorama('trackCustomEvent', {
129-
eventName: 'awsui-metric-error',
131+
eventType: AWSUI_EVENT,
132+
eventContext: 'awsui-metric-error',
130133
eventDetail: message.slice(0, 4000),
134+
timestamp: Date.now(),
131135
});
132136
}
133137
}

0 commit comments

Comments
 (0)