mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-15 09:21:09 +02:00
Infrastructure: - Add GlitchTip (web + worker) to docker-compose.macmini.yml (port 8020) - Add glitchtip.mana.how to Cloudflare Tunnel config - Add glitchtip database to init-db SQL - Add GLITCHTIP_DSN to .env.development Shared Package (@manacore/shared-error-tracking): - initErrorTracking() - Sentry-compatible init with GlitchTip DSN - captureException(), captureMessage(), setUser(), setTag(), flush() - SentryExceptionFilter for NestJS (captures 5xx errors only) - Graceful no-op when DSN is not configured Integration: - Add instrument.ts to calendar, contacts, todo backends - Import instrument.ts before app bootstrap in all 3 main.ts files - Error tracking auto-initializes when GLITCHTIP_DSN env var is set Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
118 lines
3 KiB
TypeScript
118 lines
3 KiB
TypeScript
/**
|
|
* Shared Error Tracking for ManaCore Apps
|
|
*
|
|
* Uses Sentry SDK with GlitchTip as the self-hosted backend.
|
|
* Compatible with any Sentry-compatible error tracking service.
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* // In instrument.ts (must be imported BEFORE app bootstrap)
|
|
* import { initErrorTracking } from '@manacore/shared-error-tracking';
|
|
*
|
|
* initErrorTracking({
|
|
* serviceName: 'calendar-backend',
|
|
* dsn: process.env.GLITCHTIP_DSN,
|
|
* environment: process.env.NODE_ENV,
|
|
* release: process.env.APP_VERSION,
|
|
* });
|
|
* ```
|
|
*/
|
|
|
|
import * as Sentry from '@sentry/node';
|
|
|
|
export interface ErrorTrackingOptions {
|
|
/** Service/app name (e.g. 'calendar-backend', 'contacts-web') */
|
|
serviceName: string;
|
|
/** GlitchTip/Sentry DSN. If not set, error tracking is disabled. */
|
|
dsn?: string;
|
|
/** Environment (development, staging, production) */
|
|
environment?: string;
|
|
/** Release/version string */
|
|
release?: string;
|
|
/** Sample rate for error events (0.0 to 1.0, default: 1.0) */
|
|
sampleRate?: number;
|
|
/** Sample rate for performance traces (0.0 to 1.0, default: 0.1) */
|
|
tracesSampleRate?: number;
|
|
/** Enable debug mode (default: false) */
|
|
debug?: boolean;
|
|
}
|
|
|
|
let initialized = false;
|
|
|
|
/**
|
|
* Initialize error tracking. Call this BEFORE bootstrapping your app.
|
|
* If no DSN is provided, error tracking is silently disabled.
|
|
*/
|
|
export function initErrorTracking(options: ErrorTrackingOptions): void {
|
|
if (initialized) return;
|
|
|
|
const dsn = options.dsn || process.env.GLITCHTIP_DSN || process.env.SENTRY_DSN;
|
|
|
|
if (!dsn) {
|
|
if (options.debug) {
|
|
console.log(`[ErrorTracking] No DSN configured for ${options.serviceName} - disabled`);
|
|
}
|
|
return;
|
|
}
|
|
|
|
Sentry.init({
|
|
dsn,
|
|
environment: options.environment || process.env.NODE_ENV || 'development',
|
|
release: options.release || process.env.APP_VERSION,
|
|
serverName: options.serviceName,
|
|
sampleRate: options.sampleRate ?? 1.0,
|
|
tracesSampleRate: options.tracesSampleRate ?? 0.1,
|
|
debug: options.debug ?? false,
|
|
});
|
|
|
|
initialized = true;
|
|
|
|
console.log(
|
|
`[ErrorTracking] Initialized for ${options.serviceName} (${options.environment || 'development'})`
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Capture an exception manually
|
|
*/
|
|
export function captureException(error: unknown, context?: Record<string, unknown>): void {
|
|
if (!initialized) return;
|
|
Sentry.captureException(error, { extra: context });
|
|
}
|
|
|
|
/**
|
|
* Capture a message
|
|
*/
|
|
export function captureMessage(
|
|
message: string,
|
|
level: 'fatal' | 'error' | 'warning' | 'info' | 'debug' = 'info'
|
|
): void {
|
|
if (!initialized) return;
|
|
Sentry.captureMessage(message, level);
|
|
}
|
|
|
|
/**
|
|
* Set user context for error reports
|
|
*/
|
|
export function setUser(user: { id: string; email?: string } | null): void {
|
|
if (!initialized) return;
|
|
Sentry.setUser(user);
|
|
}
|
|
|
|
/**
|
|
* Set extra context tags
|
|
*/
|
|
export function setTag(key: string, value: string): void {
|
|
if (!initialized) return;
|
|
Sentry.setTag(key, value);
|
|
}
|
|
|
|
/**
|
|
* Flush pending events (call before process exit)
|
|
*/
|
|
export async function flush(timeout = 2000): Promise<void> {
|
|
if (!initialized) return;
|
|
await Sentry.flush(timeout);
|
|
}
|
|
|
|
export { Sentry };
|