feat: Add Sentry support

This commit is contained in:
Jesse Wierzbinski 2024-07-24 18:10:29 +02:00
parent 0679971cc0
commit 5061735da7
No known key found for this signature in database
8 changed files with 57 additions and 0 deletions

View file

@ -51,6 +51,7 @@
- [x] Advanced Roles and Permissions API.
- [x] HTTP proxy support
- [x] Tor hidden service support
- [x] Sentry logging support
- [x] Ability to change the domain name in a single config change, without any database edits
## Screenshots

2
app.ts
View file

@ -1,4 +1,5 @@
import { errorResponse, jsonResponse, response } from "@/response";
import { sentry } from "@/sentry";
import { Hono } from "@hono/hono";
import { getLogger } from "@logtape/logtape";
import { config } from "config-manager";
@ -96,6 +97,7 @@ export const appFactory = async () => {
app.onError((error) => {
const serverLogger = getLogger("server");
serverLogger.error`${error}`;
sentry?.captureException(error);
return jsonResponse(
{
error: "A server error occured",

BIN
bun.lockb

Binary file not shown.

View file

@ -389,6 +389,18 @@ log_ip = false
# Log all filtered objects
log_filters = true
[logging.sentry]
# Whether to enable https://sentry.io error logging
enabled = false
# Sentry DSN for error logging
dsn = ""
debug = false
sample_rate = 1.0
traces_sample_rate = 1.0
max_breadcrumbs = 100
# environment = "production"
[logging.storage]
# Path to logfile for requests
requests = "logs/requests.log"

View file

@ -1,4 +1,5 @@
import { configureLoggers } from "@/loggers";
import { sentry } from "@/sentry";
import { createServer } from "@/server";
import { config } from "config-manager";
import { appFactory } from "~/app";
@ -6,6 +7,7 @@ import { setupDatabase } from "./drizzle/db";
if (import.meta.main) {
await import("./setup");
sentry?.captureMessage("Server started");
}
await setupDatabase();

View file

@ -106,6 +106,7 @@
"@lysand-org/client": "^0.2.5",
"@lysand-org/federation": "2.1.8",
"@oclif/core": "^4.0.14",
"@sentry/bun": "^8.19.0",
"@tufjs/canonical-json": "^2.0.0",
"altcha-lib": "^0.4.1",
"blurhash": "^2.0.5",

View file

@ -568,6 +568,20 @@ export const configValidator = z.object({
.default("info"),
log_ip: z.boolean().default(false),
log_filters: z.boolean().default(true),
sentry: z
.object({
enabled: z.boolean().default(false),
dsn: z.string().url().or(z.literal("")).optional(),
debug: z.boolean().default(false),
sample_rate: z.number().min(0).max(1.0).default(1.0),
traces_sample_rate: z.number().min(0).max(1.0).default(1.0),
max_breadcrumbs: z.number().default(100),
environment: z.string().optional(),
})
.refine(
(arg) => (arg.enabled ? !!arg.dsn : true),
"When sentry is enabled, DSN must be set",
),
storage: z
.object({
requests: z.string().default("logs/requests.log"),
@ -582,6 +596,13 @@ export const configValidator = z.object({
log_level: "info",
log_ip: false,
log_filters: true,
sentry: {
enabled: false,
debug: false,
sample_rate: 1.0,
traces_sample_rate: 1.0,
max_breadcrumbs: 100,
},
storage: {
requests: "logs/requests.log",
},

18
utils/sentry.ts Normal file
View file

@ -0,0 +1,18 @@
import * as Sentry from "@sentry/bun";
import { config } from "config-manager";
import pkg from "~/package.json";
const sentryInstance =
config.logging.sentry.enabled &&
Sentry.init({
dsn: config.logging.sentry.dsn,
debug: config.logging.sentry.debug,
sampleRate: config.logging.sentry.sample_rate,
maxBreadcrumbs: config.logging.sentry.max_breadcrumbs,
tracesSampleRate: config.logging.sentry.traces_sample_rate,
environment: config.logging.sentry.environment,
tracePropagationTargets: [config.http.bind],
release: pkg.version,
});
export const sentry = sentryInstance || undefined;