generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmetrics.ts
More file actions
101 lines (85 loc) · 3.04 KB
/
metrics.ts
File metadata and controls
101 lines (85 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { CLogClient, PanoramaClient, MetricsV2EventItem } from './log-clients';
import { buildMetricDetail, buildMetricName, jsonStringify } from './formatters';
import { ComponentConfiguration, MetricsLogItem } from './interfaces';
const oneTimeMetrics = new Set<string>();
// In case we need to override the theme for VR.
let theme = '';
function setTheme(newTheme: string) {
theme = newTheme;
}
export class Metrics {
readonly source: string;
readonly packageVersion: string;
private clog = new CLogClient();
private panorama = new PanoramaClient();
constructor(source: string, packageVersion: string) {
this.source = source;
this.packageVersion = packageVersion;
}
initMetrics(theme: string) {
setTheme(theme);
}
/**
* Calls Console Platform's client logging JS API with provided metric name, value, and detail.
* Does nothing if Console Platform client logging JS is not present in page.
*/
sendMetric(metricName: string, value: number, detail?: string): void {
if (!theme) {
// Metrics need to be initialized first (initMetrics)
console.error('Metrics need to be initialized first.');
return;
}
this.clog.sendMetric(metricName, value, detail);
}
/**
* Calls Console Platform's client v2 logging JS API with provided metric name and detail.
* Does nothing if Console Platform client logging JS is not present in page.
*/
sendPanoramaMetric(metric: MetricsV2EventItem): void {
this.panorama.sendMetric(metric);
}
sendMetricObject(metric: MetricsLogItem, value: number): void {
this.sendMetric(buildMetricName(metric, theme), value, buildMetricDetail(metric, theme));
}
sendMetricObjectOnce(metric: MetricsLogItem, value: number): void {
const metricKey = jsonStringify(metric);
if (!oneTimeMetrics.has(metricKey)) {
this.sendMetricObject(metric, value);
oneTimeMetrics.add(metricKey);
}
}
/*
* Calls Console Platform's client logging only the first time the provided metricName is used.
* Subsequent calls with the same metricName are ignored.
*/
sendMetricOnce(metricName: string, value: number, detail?: string): void {
if (!oneTimeMetrics.has(metricName)) {
this.sendMetric(metricName, value, detail);
oneTimeMetrics.add(metricName);
}
}
/*
* Reports a metric value 1 to Console Platform's client logging service to indicate that the
* component was used in the page. A component will only be reported as used to client logging
* service once per page view.
*/
logComponentUsed(componentName: string, configuration: ComponentConfiguration) {
this.sendMetricObjectOnce(
{
source: componentName,
action: 'used',
version: this.packageVersion,
configuration,
},
1
);
}
}
export function clearOneTimeMetricsCache(): void {
oneTimeMetrics.clear();
}
export class MetricsTestHelper {
resetOneTimeMetricsCache = clearOneTimeMetricsCache;
}