Add sentry.ts with initSentry + withSentryTransaction, wrap all tool call handlers with transaction tracing and error capture. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
/**
|
|
* Sentry SDK integration for confluence-mcp.
|
|
*
|
|
* Provides error tracking and performance monitoring for MCP tool calls
|
|
* while filtering out normal error responses (isError: true).
|
|
*/
|
|
|
|
import * as Sentry from "@sentry/node";
|
|
|
|
export function initSentry(environment: string = "development"): void {
|
|
const dsn = process.env.SENTRY_DSN || "";
|
|
|
|
if (!dsn) {
|
|
console.error("[sentry] SENTRY_DSN not set, Sentry disabled");
|
|
return;
|
|
}
|
|
|
|
Sentry.init({
|
|
dsn,
|
|
environment,
|
|
tracesSampleRate: parseFloat(process.env.SENTRY_TRACE_SAMPLE_RATE || "0.1"),
|
|
integrations: [
|
|
Sentry.httpIntegration(),
|
|
],
|
|
beforeSend(event: Sentry.ErrorEvent, hint: Sentry.EventHint) {
|
|
const originalException = hint.originalException as any;
|
|
if (originalException?.isError === true) {
|
|
return null;
|
|
}
|
|
|
|
if (event.request?.headers) {
|
|
const headers = { ...event.request.headers };
|
|
delete headers.Authorization;
|
|
delete headers.Cookie;
|
|
delete headers["X-API-Key"];
|
|
event.request.headers = headers;
|
|
}
|
|
|
|
if (event.request?.data) {
|
|
let dataStr = JSON.stringify(event.request.data);
|
|
dataStr = dataStr.replace(
|
|
/(api[_-]?key|token|secret)["']?\s*[:=]\s*["']?[\w-]+/gi,
|
|
"$1=REDACTED"
|
|
);
|
|
try {
|
|
event.request.data = JSON.parse(dataStr);
|
|
} catch {
|
|
event.request.data = dataStr;
|
|
}
|
|
}
|
|
|
|
return event;
|
|
},
|
|
maxBreadcrumbs: 30,
|
|
attachStacktrace: true,
|
|
release: process.env.APP_VERSION || "unknown",
|
|
sendDefaultPii: false,
|
|
});
|
|
|
|
console.error(
|
|
`[sentry] Initialized: ${environment} - ${process.env.APP_VERSION || "unknown"}`
|
|
);
|
|
}
|
|
|
|
export async function withSentryTransaction<T>(
|
|
toolName: string,
|
|
handler: () => Promise<T>
|
|
): Promise<T> {
|
|
return Sentry.startSpan(
|
|
{ name: `tool_${toolName}`, op: "mcp.tool" },
|
|
async (span: Sentry.Span) => {
|
|
try {
|
|
const result = await handler();
|
|
span.setStatus({ code: 1, message: "ok" });
|
|
return result;
|
|
} catch (error: unknown) {
|
|
const err = error as { isError?: boolean };
|
|
if (!err.isError) {
|
|
Sentry.captureException(error);
|
|
}
|
|
span.setStatus({ code: 2, message: "error" });
|
|
throw error;
|
|
}
|
|
}
|
|
);
|
|
}
|