-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.js
More file actions
63 lines (54 loc) · 1.65 KB
/
telemetry.js
File metadata and controls
63 lines (54 loc) · 1.65 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
// lokiPush.js
export default class Telemetry {
telemetryEnabled = true;
loggingURL = "https://apoh.alexkarpinski.com/loki/api/v1/push";
constructor() {
this.telemetryToggle = document.createElement("button");
this.telemetryToggle.id = "telemetry";
this.telemetryToggle.innerText = "Telemetry On";
this.telemetryToggle.addEventListener("click", () => {
this.toggleTelemetry();
});
}
toggleTelemetry() {
this.telemetryEnabled = !this.telemetryEnabled;
this.telemetryToggle.innerText = this.telemetryEnabled
? "Telemetry On"
: "Telemetry Off";
}
attachControls(parent) {
parent.appendChild(this.telemetryToggle);
}
async log(message, tags = {}) {
if (!this.telemetryEnabled) {
return;
}
var payload = {
streams: [
{
stream: tags,
values: [[(Date.now() * 1000000).toString(), message]],
},
],
};
try {
const response = await fetch(this.loggingURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response;
} catch (error) {
console.error(
"There was a problem with your fetch operation:",
error
);
throw error;
}
}
}