You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
📝 Add docstrings to codex/implement-web-vitals-tracking-and-api-jt9zi9
Docstrings generation was requested by @shayancoin.
* #125 (comment)
The following files were modified:
* `frontend/src/app/api/otel/webvital/route.ts`
* `frontend/src/app/providers.tsx`
* `frontend/src/app/web-vitals.ts`
* Generate a base64-encoded cryptographic random identifier.
43
+
*
44
+
* @param bytes - Number of random bytes to generate
45
+
* @returns A base64-encoded string containing `bytes` cryptographically secure random bytes
46
+
*/
35
47
functionrandomId(bytes: number){
36
48
returnrandomBytes(bytes).toString('base64');
37
49
}
38
50
51
+
/**
52
+
* Determines the OTLP collector endpoint URL from environment variables.
53
+
*
54
+
* Prefers `OTEL_EXPORTER_OTLP_ENDPOINT` or `OTEL_COLLECTOR_URL` when present. If those are not set, builds a URL from `OTEL_COLLECTOR_HOST` with optional `OTEL_COLLECTOR_PROTOCOL` (default `http`), `OTEL_COLLECTOR_PORT`, and `OTEL_COLLECTOR_PATH` (default `/v1/traces`).
55
+
*
56
+
* @returns The collector endpoint URL as a string, or `undefined` if no configuration is available.
* Create an OTLP span object representing a single web-vital event.
80
+
*
81
+
* @param event - Web-vital event data whose fields are mapped to span attributes (e.g., `webvital.value`, `webvital.delta`, `webvital.rating`, `webvital.id`, `webvital.type`, `webvital.navigation_type`, `navigation.*`, and truncated `webvital.attribution`)
82
+
* @param fallbackTime - Fallback timestamp in milliseconds since epoch used to compute the span's start and end times when precise timing is not provided by the event
83
+
* @returns An OTLP-compatible span object containing `traceId`, `spanId`, `name`, `kind`, `startTimeUnixNano`, `endTimeUnixNano`, `traceState`, `flags`, and an `attributes` array describing the web-vital and navigation details
@@ -150,6 +176,17 @@ function createSpan(event: IncomingEvent, fallbackTime: number) {
150
176
};
151
177
}
152
178
179
+
/**
180
+
* HTTP POST handler that ingests a JSON payload of web-vital events and exports them as OTLP spans to a configured collector.
181
+
*
182
+
* Processes an incoming payload of events, transforms each event into an OTLP span, sends the batch to the resolved collector endpoint, and returns a JSON response describing the result.
183
+
*
184
+
* @returns A JSON response:
185
+
* - `{ accepted: number }` when spans were successfully exported (number is the count of accepted spans).
186
+
* - `{ error: 'Invalid JSON payload', details: string }` with HTTP status 400 if the request body is invalid JSON.
187
+
* - `{ error: 'Collector endpoint is not configured' }` with HTTP status 500 if no collector endpoint is available.
188
+
* - `{ error: 'Failed to export web vitals', status: number, body: string }` with HTTP status 502 if the collector responds with a non-OK status.
189
+
*/
153
190
exportasyncfunctionPOST(request: Request){
154
191
letpayload: IncomingPayload;
155
192
try{
@@ -227,4 +264,3 @@ export async function POST(request: Request) {
@@ -152,6 +185,11 @@ function metricToPayload(metric: Metric): WebVitalEvent {
152
185
returnpayload;
153
186
}
154
187
188
+
/**
189
+
* Starts observing core web-vital metrics and enqueues each metric as a telemetry payload.
190
+
*
191
+
* Registers observers for CLS, LCP, INP, FCP, and TTFB; each observed metric is converted to a payload and added to the batching queue for delivery.
192
+
*/
155
193
functionregisterWebVitalObservers(){
156
194
consthandleMetric=(metric: Metric)=>{
157
195
enqueue(metricToPayload(metric));
@@ -164,6 +202,11 @@ function registerWebVitalObservers() {
164
202
onTTFB(handleMetric);
165
203
}
166
204
205
+
/**
206
+
* Register page lifecycle listeners that trigger an immediate flush of the web-vitals queue.
207
+
*
208
+
* Adds handlers for `beforeunload` and `pagehide`, and for `visibilitychange` when the document becomes `hidden`, causing the queued events to be sent before the page is unloaded or hidden.
209
+
*/
167
210
functionsetupLifecycleFlushes(){
168
211
constimmediateFlush=()=>{
169
212
voidflushQueue();
@@ -178,6 +221,12 @@ function setupLifecycleFlushes() {
178
221
});
179
222
}
180
223
224
+
/**
225
+
* Initializes collection and reporting of web-vitals and navigation timing data.
226
+
*
227
+
* This function is idempotent: calling it multiple times has no additional effect.
228
+
* It is a no-op outside of a browser environment.
229
+
*/
181
230
exportfunctioninitWebVitals(){
182
231
if(initialized||typeofwindow==='undefined'){
183
232
return;
@@ -189,4 +238,3 @@ export function initWebVitals() {
0 commit comments