Skip to content

Commit

Permalink
feat: Send metrics no panorama API as a fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Sep 9, 2024
1 parent 877ec32 commit b2dccc2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/internal/base-component/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ describe('Client Metrics support', () => {
});

test('does nothing when window.AWSC.Clog is undefined', () => {
window.AWSC = undefined;
window.AWSC = {};
metrics.sendMetric('name', 0); // only proves no exception thrown
});

test('uses panorama API as fallback when AWSC.Clog.log is unavailable', () => {
delete window.AWSC;
metrics.sendMetric('name', 0);
expect(window.panorama).toHaveBeenCalledWith('trackCustomEvent', {
eventName: 'name',
eventValue: '0',
timestamp: expect.any(Number),
});
});

test('does nothing when window.AWSC.Clog.log is undefined', () => {
window.AWSC = {
Clog: undefined,
Expand Down
12 changes: 12 additions & 0 deletions src/internal/base-component/metrics/log-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface MetricsWindow extends Window {
}

export interface MetricsV2EventItem {
eventName?: string;
eventType?: string;
eventContext?: string;
eventDetail?: string | Record<string, string | number | boolean>;
Expand Down Expand Up @@ -45,6 +46,13 @@ export class CLogClient {
const AWSC = this.findAWSC(window);
if (typeof AWSC === 'object' && typeof AWSC.Clog === 'object' && typeof AWSC.Clog.log === 'function') {
AWSC.Clog.log(metricName, value, detail);
} else {
new PanoramaClient().sendMetric({
eventName: metricName,
eventDetail: detail,
eventValue: `${value}`,
timestamp: Date.now(),
});
}
}

Expand Down Expand Up @@ -81,6 +89,10 @@ export class PanoramaClient {
if (typeof metric.eventValue === 'object') {
metric.eventValue = JSON.stringify(metric.eventValue);
}
if (!validateLength(metric.eventName, 1000)) {
console.error(`Event name for metric is too long: ${metric.eventName}`);
return;

Check warning on line 94 in src/internal/base-component/metrics/log-clients.ts

View check run for this annotation

Codecov / codecov/patch

src/internal/base-component/metrics/log-clients.ts#L93-L94

Added lines #L93 - L94 were not covered by tests
}
if (!validateLength(metric.eventDetail, 4000)) {
console.error(`Event detail for metric is too long: ${metric.eventDetail}`);
return;
Expand Down

0 comments on commit b2dccc2

Please sign in to comment.