Repository navigation
Expand file tree
/
Copy pathrum.html
More file actions
191 lines (176 loc) · 6.31 KB
/
Copy pathrum.html
File metadata and controls
191 lines (176 loc) · 6.31 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!--
CML Platform Observability: Real User Monitoring (RUM) & Web Vitals
Add this script to your site's <head> or before </body>.
Zero dependencies. Captures Core Web Vitals and unhandled errors,
sending standard OTLP/HTTP logs to the central OpenTelemetry collector.
-->
<script>
(function() {
const ENDPOINT = window.__OTEL_ENDPOINT__ || "";
const PROJECT = window.__OTEL_PROJECT__ || "my-web-app";
const SERVICE = window.__OTEL_SERVICE__ || PROJECT;
const ENV = window.__OTEL_ENV__ || "prod";
const TOKEN = window.__OTEL_TOKEN__ || "";
let isSending = false;
function sendLog(body, severityText, attributes) {
if (!ENDPOINT || isSending) return;
isSending = true;
try {
const timeUnixNano = String(Date.now()) + "000000";
const attrs = attributes || {};
const logRecord = {
timeUnixNano: timeUnixNano,
observedTimeUnixNano: timeUnixNano,
severityNumber: severityText === "ERROR" ? 17 : 9,
severityText: severityText,
body: { stringValue: body },
attributes: Object.keys(attrs).map(function(key) {
const val = attrs[key];
return {
key: key,
value: typeof val === "number" ? { doubleValue: val } : { stringValue: String(val) }
};
})
};
const pagePath = window.location.origin + window.location.pathname;
const payload = {
resourceLogs: [{
resource: {
attributes: [
{ key: "service.name", value: { stringValue: SERVICE } },
{ key: "project", value: { stringValue: PROJECT } },
{ key: "env", value: { stringValue: ENV } },
{ key: "telemetry.sdk.name", value: { stringValue: "cml-rum" } },
{ key: "telemetry.sdk.language", value: { stringValue: "web-js" } },
{ key: "user_agent", value: { stringValue: navigator.userAgent } },
{ key: "page.url", value: { stringValue: pagePath } }
]
},
scopeLogs: [{
scope: { name: "cml-rum", version: "1.0.0" },
logRecords: [logRecord]
}]
}]
};
const headers = { "Content-Type": "application/json" };
if (TOKEN) headers["Authorization"] = "Bearer " + TOKEN;
fetch(ENDPOINT.replace(/\/+$/, "") + "/v1/logs", {
method: "POST",
headers: headers,
body: JSON.stringify(payload),
keepalive: true,
referrerPolicy: "no-referrer",
credentials: "omit",
mode: "cors"
}).catch(function() {});
} catch (e) {
// Never let telemetry errors escape to window.onerror
} finally {
isSending = false;
}
}
// --- Core Web Vitals ---
if ("PerformanceObserver" in window) {
let lcpObserver;
let latestLcp;
let lcpFinalized = document.visibilityState === "hidden";
let firstHiddenTime = lcpFinalized ? 0 : Infinity;
let clsObserver;
let sessionStart = 0;
let lastShiftTime = 0;
let sessionScore = 0;
let clsScore = 0;
let reportedCls = -1;
function updateLcp(entries) {
if (lcpFinalized) return;
for (const entry of entries) {
if (entry.startTime < firstHiddenTime) latestLcp = entry;
}
}
function finalizeLcp() {
if (lcpFinalized) return;
if (lcpObserver) {
updateLcp(lcpObserver.takeRecords());
lcpObserver.disconnect();
}
lcpFinalized = true;
if (latestLcp) {
sendLog("WebVital: LCP", "INFO", {
vital_name: "LCP",
duration_ms: Math.round(latestLcp.startTime),
element: latestLcp.element ? latestLcp.element.tagName || "element" : "unknown"
});
}
}
function updateCls(entries) {
for (const entry of entries) {
if (entry.hadRecentInput) continue;
// A session ends at a one-second gap or a five-second total span.
if (sessionScore > 0 && entry.startTime - lastShiftTime < 1000 && entry.startTime - sessionStart < 5000) {
sessionScore += entry.value;
} else {
sessionStart = entry.startTime;
sessionScore = entry.value;
}
lastShiftTime = entry.startTime;
clsScore = Math.max(clsScore, sessionScore);
}
}
try {
if (!lcpFinalized) {
lcpObserver = new PerformanceObserver((list) => updateLcp(list.getEntries()));
lcpObserver.observe({ type: "largest-contentful-paint", buffered: true });
}
} catch {
// Some browsers do not support this performance entry type.
}
try {
if (PerformanceObserver.supportedEntryTypes?.includes("layout-shift")) {
clsObserver = new PerformanceObserver((list) => updateCls(list.getEntries()));
clsObserver.observe({ type: "layout-shift", buffered: true });
}
} catch {
clsObserver = undefined;
// Some browsers do not support this performance entry type.
}
for (const event of ["pointerdown", "touchstart", "keydown", "scroll"]) {
window.addEventListener(event, finalizeLcp, { once: true, capture: true, passive: true });
}
document.addEventListener("visibilitychange", (event) => {
if (document.visibilityState !== "hidden") return;
firstHiddenTime = Math.min(firstHiddenTime, event.timeStamp);
finalizeLcp();
if (clsObserver) updateCls(clsObserver.takeRecords());
if (clsObserver && clsScore > reportedCls) {
sendLog("WebVital: CLS", "INFO", {
vital_name: "CLS",
cls_score: Math.round(clsScore * 1000) / 1000
});
reportedCls = clsScore;
}
});
}
// --- Uncaught Errors ---
function scriptLocation(filename) {
try {
const url = new URL(filename, window.location.origin);
return url.protocol === "https:" || url.protocol === "http:" ? url.origin + url.pathname : "unknown";
} catch (_) {
return "unknown";
}
}
window.addEventListener("error", function(evt) {
sendLog("Unhandled JavaScript error", "ERROR", {
error_type: "uncaught_exception",
filename: evt.filename ? scriptLocation(evt.filename) : "unknown",
lineno: evt.lineno || 0,
colno: evt.colno || 0
});
});
window.addEventListener("unhandledrejection", function() {
sendLog("Unhandled promise rejection", "ERROR", {
error_type: "unhandled_promise_rejection"
});
});
})();
</script>