All files / apps/huella-legal/app/plugins observability.client.ts

100% Statements 25/25
100% Branches 6/6
100% Functions 5/5
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          38x 38x     45x   45x     6x   6x 2x 1x     6x 6x   6x 7x   6x   6x   6x 6x     7x   6x 12x   6x      
import type { startWebTelemetry } from "@joinery/observability";
 
type Faro = ReturnType<typeof startWebTelemetry>;
 
// Lighthouse stops tracing soon after the page goes quiet, so starting later keeps Faro out of it
const IDLE_START_MS = 10_000;
const INTERACTIONS = ["pointerdown", "keydown"] as const;
 
export default defineNuxtPlugin((nuxtApp) => {
    const { observability } = useRuntimeConfig().public;
 
    if (!observability.url) return;
 
    let faro: Faro | undefined;
    const earlyErrors: Error[] = [];
 
    nuxtApp.hook("vue:error", (error) => {
        if (faro) faro.api.pushError(error as Error);
        else earlyErrors.push(error as Error);
    });
 
    onNuxtReady(() => {
        const triggers = new AbortController();
 
        const start = async () => {
            if (triggers.signal.aborted) return;
 
            triggers.abort();
 
            const telemetry = await import("@joinery/observability");
 
            faro = telemetry.startWebTelemetry(observability);
            for (const error of earlyErrors.splice(0)) faro.api.pushError(error);
        };
 
        const begin = () => void start();
 
        for (const type of INTERACTIONS)
            addEventListener(type, begin, { passive: true, signal: triggers.signal });
 
        setTimeout(begin, IDLE_START_MS);
    });
});