postiz-app/libraries/nestjs-libraries/src/sentry/initialize.sentry.ts
2026-03-21 22:18:28 +01:00

59 lines
1.7 KiB
TypeScript

import * as Sentry from '@sentry/nestjs';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import { capitalize } from 'lodash';
export const initializeSentry = (appName: string, allowLogs = false) => {
if (!process.env.NEXT_PUBLIC_SENTRY_DSN) {
return null;
}
try {
Sentry.init({
initialScope: {
tags: {
service: appName,
component: 'nestjs',
},
contexts: {
app: {
name: `Postiz ${capitalize(appName)}`,
},
},
},
environment: process.env.NODE_ENV || 'development',
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
spotlight: process.env.SENTRY_SPOTLIGHT === '1',
integrations: [
// Add our Profiling integration
nodeProfilingIntegration(),
Sentry.consoleLoggingIntegration({ levels: ['log', 'info', 'warn', 'error', 'debug', 'assert', 'trace'] }),
Sentry.openAIIntegration({
recordInputs: true,
recordOutputs: true,
}),
],
tracesSampleRate: 1.0,
enableLogs: true,
// Profiling
profileSessionSampleRate: process.env.NODE_ENV === 'development' ? 1.0 : 0.45,
profileLifecycle: 'trace',
});
} catch (err) {
console.log(err);
}
try {
process.on('unhandledRejection', (reason) => {
try {
Sentry.metrics.count('app.unhandled_errors', 1, { tags: { service: appName, route: 'unhandledRejection' } } as any);
} catch (e) {}
});
process.on('uncaughtException', (err) => {
try {
Sentry.metrics.count('app.unhandled_errors', 1, { tags: { service: appName, route: 'uncaughtException' } } as any);
} catch (e) {}
});
} catch (e) {}
return true;
};