feat(telemetry): Implement structured telemetry logging and reporting
Adds comprehensive client-side telemetry capabilities for usage, errors, and performance metrics. This includes integrating OpenTelemetry standards into both the frontend (Svelte) and backend (Tauri/Rust) layers to capture events, spans, and system resource utilization. The implementation ensures that all collected data is privacy-filtered by design, explicitly excluding sensitive information like repository paths, credentials, source code, or email addresses from being logged. - Updates README with detailed SigNoz telemetry guide - Adds process metrics collection (CPU/Memory) in Rust backend - Exposes `setTelemetryEnabled` state management to the frontend
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { telemetryLog } from "./telemetry";
|
||||
|
||||
export type AnalyticsEventProperties = Record<string, string | number>;
|
||||
|
||||
export function trackAnalyticsEvent(name: string, props?: AnalyticsEventProperties) {
|
||||
const details = props && Object.keys(props).length > 0 ? ` ${JSON.stringify(props)}` : "";
|
||||
telemetryLog("info", `${name}${details}`, `product.${name}`);
|
||||
void invoke("plugin:aptabase|track_event", {
|
||||
name,
|
||||
props: props && Object.keys(props).length > 0 ? props : undefined,
|
||||
|
||||
@@ -30,10 +30,10 @@
|
||||
</div>
|
||||
<div class="analytics-notice-copy">
|
||||
<p>
|
||||
Gitty can send anonymous usage events to Aptabase so crashes, rough edges, and commonly used workflows are easier to improve.
|
||||
Gitty can send anonymous usage events and technical error logs to Aptabase and the self-hosted SigNoz service so crashes, rough edges, and commonly used workflows are easier to improve.
|
||||
</p>
|
||||
<p>
|
||||
Events do not include repository paths, remote URLs, branch names, commit messages, diffs, file names, credentials, or source code.
|
||||
Telemetry does not include repository paths, remote URLs, branch names, commit messages, diffs, file names, credentials, or source code.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -47,7 +47,7 @@
|
||||
</div>
|
||||
|
||||
<footer class="dialog-footer analytics-notice-footer">
|
||||
<span class="dialog-footer-info">Privacy-friendly, optional, and limited to product usage events.</span>
|
||||
<span class="dialog-footer-info">Privacy-friendly, optional, and limited to product usage and technical errors.</span>
|
||||
<button class="btn-primary" type="button" onclick={() => onContinue(allowAnalytics)}>
|
||||
<Check size={16} aria-hidden="true" />
|
||||
Continue
|
||||
|
||||
@@ -124,7 +124,7 @@
|
||||
<label class="settings-toggle-row">
|
||||
<input type="checkbox" bind:checked={analyticsEnabled} />
|
||||
<span>
|
||||
<strong>{isGerman ? "Anonyme Aptabase-Ereignisse erlauben" : "Allow anonymous Aptabase events"}</strong>
|
||||
<strong>{isGerman ? "Anonyme Analytics und Fehlerberichte erlauben" : "Allow anonymous analytics and error reports"}</strong>
|
||||
<small>{isGerman ? "Es werden keine Repository-Pfade, Remotes, Branches, Commit-Nachrichten, Dateinamen, Diffs, Zugangsdaten oder Code übertragen." : "No repository paths, remotes, branches, commit messages, file names, diffs, credentials, or code are sent."}</small>
|
||||
</span>
|
||||
</label>
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { tracedInvoke as invoke } from "./telemetry";
|
||||
|
||||
import type {
|
||||
AiReviewResult,
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
type TelemetryLevel = "info" | "warn" | "error";
|
||||
|
||||
const MAX_MESSAGE_LENGTH = 2_048;
|
||||
let telemetryEnabled = false;
|
||||
let configuration: Promise<unknown> = Promise.resolve();
|
||||
|
||||
// Telemetry must never contain repository contents or identifying local data.
|
||||
function sanitize(message: string): string {
|
||||
return message
|
||||
.replace(/https?:\/\/\S+/gi, "[url]")
|
||||
.replace(/(?:[A-Za-z]:\\|\/)(?:[^\s<>:"|?*]+[\\/])*[^\s<>:"|?*]*/g, "[path]")
|
||||
.replace(/(token|password|secret|authorization|credential)\s*[:=]\s*\S+/gi, "$1=[redacted]")
|
||||
.replace(/[\w.+-]+@[\w.-]+\.[A-Za-z]{2,}/g, "[email]")
|
||||
.slice(0, MAX_MESSAGE_LENGTH);
|
||||
}
|
||||
|
||||
export function setTelemetryEnabled(enabled: boolean) {
|
||||
telemetryEnabled = enabled;
|
||||
configuration = invoke("set_telemetry_enabled", { enabled }).catch(() => {});
|
||||
}
|
||||
|
||||
export function telemetryLog(level: TelemetryLevel, message: string, eventName?: string) {
|
||||
if (!telemetryEnabled) return;
|
||||
void configuration.then(() => invoke("emit_frontend_log", {
|
||||
log: {
|
||||
level,
|
||||
message: sanitize(message),
|
||||
eventName,
|
||||
},
|
||||
}))
|
||||
.catch(() => {
|
||||
// Observability must never interrupt the Git workflow.
|
||||
});
|
||||
}
|
||||
|
||||
function randomHex(bytes: number): string {
|
||||
const values = new Uint8Array(bytes);
|
||||
crypto.getRandomValues(values);
|
||||
return Array.from(values, (value) => value.toString(16).padStart(2, "0")).join("");
|
||||
}
|
||||
|
||||
export async function tracedInvoke<T>(command: string, args?: Record<string, unknown>): Promise<T> {
|
||||
if (!telemetryEnabled) return invoke<T>(command, args);
|
||||
const startedAtMs = Date.now();
|
||||
const started = performance.now();
|
||||
const traceId = randomHex(16);
|
||||
const spanId = randomHex(8);
|
||||
try {
|
||||
const result = await invoke<T>(command, args);
|
||||
void configuration.then(() => invoke("emit_frontend_span", {
|
||||
span: { name: command, traceId, spanId, startedAtMs, durationMs: performance.now() - started, success: true },
|
||||
})).catch(() => {});
|
||||
return result;
|
||||
} catch (error) {
|
||||
void configuration.then(() => invoke("emit_frontend_span", {
|
||||
span: { name: command, traceId, spanId, startedAtMs, durationMs: performance.now() - started, success: false },
|
||||
})).catch(() => {});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function installGlobalErrorTelemetry() {
|
||||
window.addEventListener("error", (event) => {
|
||||
telemetryLog("error", event.message || "Unhandled frontend error", "frontend.unhandled_error");
|
||||
});
|
||||
window.addEventListener("unhandledrejection", (event) => {
|
||||
const message = event.reason instanceof Error ? event.reason.message : String(event.reason ?? "Unhandled promise rejection");
|
||||
telemetryLog("error", message, "frontend.unhandled_rejection");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user