mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-07 16:11:46 +02:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Paperclip reports server and browser errors through optional Sentry monitoring > - One environment variable sends both error types to one Sentry project > - Operators need separate control for browser and server error data > - This pull request adds specific variables and keeps the existing variable as a fallback > - The benefit is separate monitoring without breaking current deployments ## Linked Issues or Issue Description **What existing behavior does this improve?** The Sentry configuration for server and browser monitoring uses one environment variable. **Subsystem affected** Cross-cutting (multiple of the above) **Current behavior** `SENTRY_DSN` supplies the server and browser clients. Both clients therefore report to the same Sentry project. **Proposed behavior** `SENTRY_DSN_FRONTEND` supplies the browser client. `SENTRY_DSN_BACKEND` supplies the server process. `SENTRY_DSN` remains a fallback for either component. **Reason and benefit** Operators can send browser and server errors to separate Sentry projects. Operators can also activate only one component. **Breaking changes** None. Existing deployments can continue to use `SENTRY_DSN`. ## What Changed - Add `resolveSentryDsns(env)` and use it in the server and browser configuration paths. - Add precedence, empty-string, fallback, and route tests. - Update the README, observability guide, and stale code comments. - Log one warning when the server uses the legacy fallback without exposing a DSN value. ## Verification - `pnpm vitest run --project server sentry-dsn` — 8 tests pass. - `pnpm vitest run --project server auth-routes` — 21 tests pass. - The earlier run of the three targeted suites passed 40 tests. - `tsc --noEmit` passes for the files in this diff. - All required GitHub Actions checks pass, including the full continuous-integration suite. ## Risks The main risk is an incorrect environment variable precedence rule. Unit tests cover specific values, empty strings, and legacy fallback behavior. The existing `SENTRY_DSN` path remains compatible. ## Model Used OpenAI Codex — GPT-5, current runtime, tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
216 lines
8.7 KiB
TypeScript
216 lines
8.7 KiB
TypeScript
// Optional Sentry error monitoring for the server process.
|
|
//
|
|
// Activated only when the backend DSN resolves to a value — see
|
|
// `resolveSentryDsns` in `sentry-dsn.ts` for the precedence between
|
|
// `SENTRY_DSN_BACKEND` and the legacy `SENTRY_DSN` fallback. When it
|
|
// resolves to `null`, no Sentry package is loaded at all.
|
|
//
|
|
// The import is dynamic and the package is an optional runtime dependency —
|
|
// operators who want server-side error monitoring install `@sentry/node`
|
|
// themselves. That keeps Sentry off the default dependency graph and avoids
|
|
// forcing a lockfile bump for an opt-in feature. This gate mirrors the
|
|
// OpenTelemetry gate in `instrumentation.ts`.
|
|
//
|
|
// OpenTelemetry keeps ownership of trace setup: the initializer passes
|
|
// `skipOpenTelemetrySetup: true` and `tracesSampleRate: 0`, so this module
|
|
// adds error monitoring only and starts no span or trace behavior of its
|
|
// own.
|
|
//
|
|
// Default-integration privacy note: `sendDefaultPii: false` filters values
|
|
// by name, inside the `RequestData` integration only. Three other default
|
|
// integrations copy raw values past that filter, so the initializer removes
|
|
// or narrows them with built-in Sentry options — no custom filter code:
|
|
// - `Console` turns a `console.*` call into a breadcrumb with the raw
|
|
// arguments. The initializer drops it.
|
|
// - `ContextLines` reads local source lines around each stack frame off
|
|
// the host disk. The initializer drops it.
|
|
// - `Http` records a breadcrumb for each outbound request, with its URL
|
|
// and query string. The initializer keeps the integration (`RequestData`
|
|
// and request isolation need it) and turns the breadcrumb off with the
|
|
// integration's own `breadcrumbs` option.
|
|
//
|
|
// `onUnhandledRejectionIntegration` defaults to `mode: "warn"`, which
|
|
// registers a `process.on("unhandledRejection")` listener. Node cancels its
|
|
// own crash-on-unhandled-rejection behavior when any listener is registered.
|
|
// The server relies on that crash today, so the initializer passes
|
|
// `mode: "strict"`: Sentry still captures the event, then exits the process,
|
|
// so the existing crash-and-restart behavior stays.
|
|
//
|
|
// Before it imports the package, the bootstrap checks the installed
|
|
// `@sentry/node` version against the exact version this manifest's
|
|
// `peerDependencies` declares — the same audited version documented in
|
|
// `doc/observability.md`. A missing or a mismatched version logs one
|
|
// diagnostic and leaves the server running without error monitoring; it
|
|
// never throws. This gate mirrors the OpenTelemetry gate in
|
|
// `instrumentation.ts`.
|
|
|
|
import { checkExactPeerVersions } from "./peer-version-check.js";
|
|
import { resolveSentryDsns } from "./sentry-dsn.js";
|
|
|
|
const { backend: dsn, legacyFallbackUsed } = resolveSentryDsns();
|
|
|
|
if (legacyFallbackUsed) {
|
|
// eslint-disable-next-line no-console
|
|
console.warn(
|
|
"[paperclip] SENTRY_DSN_FRONTEND or SENTRY_DSN_BACKEND is not set. " +
|
|
"The server uses the legacy SENTRY_DSN value for the affected " +
|
|
"component. Set SENTRY_DSN_FRONTEND and SENTRY_DSN_BACKEND to send " +
|
|
"each component to its own Sentry project.",
|
|
);
|
|
}
|
|
|
|
/** The subset of the `@sentry/node` client surface this gate calls. */
|
|
interface SentryHandle {
|
|
captureException(error: unknown): string;
|
|
close(timeout?: number): Promise<boolean>;
|
|
}
|
|
|
|
let sentryHandle: SentryHandle | null = null;
|
|
let shutdownPromise: Promise<void> | null = null;
|
|
|
|
/**
|
|
* Resolves once the Sentry SDK has started, or once bootstrap has failed and
|
|
* logged, or at once when the backend DSN resolves to `null`. No caller
|
|
* needs to await this before calling `captureException` — it is a no-op
|
|
* until ready — but `index.ts` awaits it at startup so the first real error
|
|
* has a live client.
|
|
*/
|
|
export const sentryReady: Promise<void> = dsn ? bootstrapSentry(dsn) : Promise.resolve();
|
|
|
|
/**
|
|
* Report an error to Sentry. A no-op before the gate opens, when the gate
|
|
* never opens (the backend DSN resolves to `null`), or when bootstrap
|
|
* failed. Never throws — observability must not change control flow.
|
|
*/
|
|
export function captureException(error: unknown): void {
|
|
if (!sentryHandle) return;
|
|
try {
|
|
sentryHandle.captureException(error);
|
|
} catch (err) {
|
|
// eslint-disable-next-line no-console
|
|
console.error("[paperclip] Sentry captureException failed", err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Flush buffered events and close the Sentry client. Idempotent — concurrent
|
|
* callers share one shutdown. A no-op when monitoring is off or bootstrap
|
|
* failed.
|
|
*/
|
|
export function shutdownSentry(): Promise<void> {
|
|
shutdownPromise ??= (async () => {
|
|
await sentryReady;
|
|
if (!sentryHandle) return;
|
|
try {
|
|
// Awaiting matters: the client flushes buffered events to Sentry
|
|
// during close; exiting before it settles silently drops them.
|
|
await sentryHandle.close(5_000);
|
|
} catch (err) {
|
|
// eslint-disable-next-line no-console
|
|
console.error("[paperclip] Sentry shutdown failed", err);
|
|
}
|
|
})();
|
|
return shutdownPromise;
|
|
}
|
|
|
|
/**
|
|
* The subset of the `@sentry/node` module surface the initializer needs to
|
|
* build its options object. A structural type, not the real Sentry type —
|
|
* the real type is unavailable at compile time because the package is an
|
|
* optional runtime dependency (see the module comment above).
|
|
*/
|
|
interface SentryModuleLike {
|
|
httpIntegration(options: { breadcrumbs: boolean }): { name: string };
|
|
onUnhandledRejectionIntegration(options: { mode: string }): { name: string };
|
|
}
|
|
|
|
/** The `Sentry.init` options this gate builds. */
|
|
export interface SentryInitOptions {
|
|
dsn: string;
|
|
skipOpenTelemetrySetup: boolean;
|
|
tracesSampleRate: number;
|
|
sendDefaultPii: boolean;
|
|
integrations: (defaults: Array<{ name: string }>) => Array<{ name: string }>;
|
|
}
|
|
|
|
/**
|
|
* Build the `Sentry.init` options object. A pure function, split out from
|
|
* `bootstrapSentry` so a test can call it with a real `@sentry/node` module
|
|
* and assert the resolved integration list and the captured-event shape
|
|
* against the true SDK, not a stand-in.
|
|
*/
|
|
export function buildSentryInitOptions(
|
|
dsn: string,
|
|
Sentry: SentryModuleLike,
|
|
): SentryInitOptions {
|
|
return {
|
|
dsn,
|
|
skipOpenTelemetrySetup: true,
|
|
tracesSampleRate: 0,
|
|
sendDefaultPii: false,
|
|
integrations: (defaults: Array<{ name: string }>) => {
|
|
const kept = defaults.filter(
|
|
(integration) =>
|
|
integration.name !== "Console" &&
|
|
integration.name !== "ContextLines" &&
|
|
integration.name !== "Http" &&
|
|
integration.name !== "OnUnhandledRejection",
|
|
);
|
|
return [
|
|
...kept,
|
|
// Keep the rest of the Http integration — RequestData and request
|
|
// isolation need it — but turn the outbound breadcrumb off.
|
|
Sentry.httpIntegration({ breadcrumbs: false }),
|
|
// Keep today's crash-on-unhandled-rejection behavior. See the
|
|
// module comment above for why the default mode cannot stay.
|
|
Sentry.onUnhandledRejectionIntegration({ mode: "strict" }),
|
|
];
|
|
},
|
|
};
|
|
}
|
|
|
|
async function bootstrapSentry(dsn: string): Promise<void> {
|
|
// Gate on the exact peer version before touching the dynamic import: a
|
|
// package installed at the wrong version can still load and start, which
|
|
// would silently invalidate the privacy audit `doc/observability.md`
|
|
// records against one exact version. Checking first turns that into one
|
|
// precise, fail-open diagnostic.
|
|
const versionCheck = checkExactPeerVersions(["@sentry/node"]);
|
|
if (!versionCheck.ok) {
|
|
// eslint-disable-next-line no-console
|
|
console.warn(
|
|
"[paperclip] The backend Sentry DSN is set, but the @sentry/node " +
|
|
"package is not installed, or is installed at an unsupported " +
|
|
"version. Install the declared version of @sentry/node to enable " +
|
|
"server error monitoring. Continuing without it.",
|
|
versionCheck.detail,
|
|
);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
// Dynamic import so type-resolution doesn't require the package to be
|
|
// installed unless the operator actually opts in.
|
|
// @ts-ignore optional peer dep
|
|
const Sentry = await import("@sentry/node");
|
|
|
|
Sentry.init(buildSentryInitOptions(dsn, Sentry));
|
|
|
|
sentryHandle = {
|
|
captureException: (error) => Sentry.captureException(error),
|
|
close: (timeout) => Sentry.close(timeout),
|
|
};
|
|
} catch (err) {
|
|
// The exact-version gate above already confirmed @sentry/node is
|
|
// installed at the declared version, so only a load or init failure
|
|
// after that point reaches this block.
|
|
// eslint-disable-next-line no-console
|
|
console.warn(
|
|
"[paperclip] The backend Sentry DSN is set, and @sentry/node passed " +
|
|
"the version check, but it failed to load or initialize. " +
|
|
"Continuing without error monitoring.",
|
|
err,
|
|
);
|
|
}
|
|
}
|