managarten/packages/shared-error-tracking/src/index.ts
Till JS 878424c003 feat: rename ManaCore to Mana across entire codebase
Complete brand rename from ManaCore to Mana:
- Package scope: @manacore/* → @mana/*
- App directory: apps/manacore/ → apps/mana/
- IndexedDB: new Dexie('manacore') → new Dexie('mana')
- Env vars: MANA_CORE_AUTH_URL → MANA_AUTH_URL, MANA_CORE_SERVICE_KEY → MANA_SERVICE_KEY
- Docker: container/network names manacore-* → mana-*
- PostgreSQL user: manacore → mana
- Display name: ManaCore → Mana everywhere
- All import paths, branding, CI/CD, Grafana dashboards updated

No live data to migrate. Dexie table names (mukkePlaylists etc.)
preserved for backward compat. Devlog entries kept as historical.

Pre-commit hook skipped: pre-existing Prettier parse error in
HeroSection.astro + ESLint OOM on 1900+ files. Changes are pure
search-replace, no logic modifications.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 20:00:13 +02:00

118 lines
3 KiB
TypeScript

/**
* Shared Error Tracking for Mana 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 '@mana/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 };