Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions packages/api/src/entries/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,26 @@ const workerHandler = {

// Wrap with Sentry using withSentry pattern
export default Sentry.withSentry((env: Env) => {
console.log("🔍 [SENTRY DEBUG] withSentry callback called");
console.log("🔍 [SENTRY DEBUG] env.SENTRY_DSN exists:", !!env.SENTRY_DSN);
console.log(
"🔍 [SENTRY DEBUG] env.SENTRY_DSN length:",
env.SENTRY_DSN?.length || 0
);

const config = getSentryConfig(env);

// If no DSN provided, Sentry will be disabled
if (!config) {
console.warn(
"⚠️ Sentry not initialized: SENTRY_DSN not configured. Set SENTRY_DSN in wrangler.toml or Cloudflare secrets."
console.error(
"❌ [SENTRY DEBUG] Sentry not initialized: SENTRY_DSN not configured. Set SENTRY_DSN in wrangler.toml or Cloudflare secrets."
);
console.error("❌ [SENTRY DEBUG] Available env keys:", Object.keys(env));
return { dsn: undefined };
}

console.log("✅ [SENTRY DEBUG] getSentryConfig returned config with DSN");

// Add version metadata if available
const versionId = env?.CF_VERSION_METADATA?.id;
if (versionId && typeof versionId === "string") {
Expand All @@ -132,21 +142,23 @@ export default Sentry.withSentry((env: Env) => {
Sentry.honoIntegration(),
];

// Log Sentry initialization in development
// Log Sentry initialization (ALWAYS log, not just in dev)
const environment = (env.SENTRY_ENVIRONMENT ||
env.NODE_ENV ||
"development") as string;
if (environment === "development") {
console.log("✅ Sentry initialized for backend:", {
environment,
release: config.release,
hasDsn: !!config.dsn,
aiTracking: true,
consoleLogging: true,
httpTracing: true,
trpcTracing: true,
});
}

console.log("✅ [SENTRY DEBUG] Sentry config prepared:", {
environment,
release: config.release,
hasDsn: !!config.dsn,
dsnLength: typeof config.dsn === "string" ? config.dsn.length : 0,
tracesSampleRate: config.tracesSampleRate,
enableLogs: config.enableLogs,
enableMetrics: config.enableMetrics,
integrations: Array.isArray(config.integrations)
? config.integrations.length
: 0,
});

return config;
}, workerHandler);
86 changes: 79 additions & 7 deletions packages/api/src/hono/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,32 @@ export function createHonoApp(config: HonoAppConfig) {
const Sentry = c.get("sentry");
const env = c.get("env");

console.log(
"🔍 [SENTRY DEBUG] HTTP middleware called for:",
c.req.method,
c.req.path
);
console.log("🔍 [SENTRY DEBUG] Sentry exists:", !!Sentry);
console.log("🔍 [SENTRY DEBUG] env.SENTRY_DSN exists:", !!env.SENTRY_DSN);
console.log(
"🔍 [SENTRY DEBUG] Sentry.startSpan exists:",
!!(Sentry && typeof Sentry.startSpan === "function")
);

// Only create spans if Sentry is configured
if (!Sentry || !env.SENTRY_DSN) {
console.warn(
"⚠️ [SENTRY DEBUG] Skipping span creation - Sentry not configured"
);
return await next();
}

// Create transaction name from method and path
const method = c.req.method;
const path = c.req.path;

console.log("✅ [SENTRY DEBUG] Creating span for:", method, path);

// Use Sentry.startSpan to create a trace for this HTTP request
return await Sentry.startSpan(
{
Expand Down Expand Up @@ -175,22 +192,77 @@ export function createHonoApp(config: HonoAppConfig) {
return c.json({ status: "ok", runtime: c.get("runtime") });
});

// Debug Sentry
// Debug Sentry - comprehensive diagnostics
app.get("/debug-sentry", (c) => {
const env = c.get("env");
const sentry = c.get("sentry");
const runtime = c.get("runtime");

console.log("🔍 [SENTRY DEBUG] /debug-sentry called");

const diagnostics = {
runtime,
sentryConfigured: !!env.SENTRY_DSN,
sentryDsnLength: env.SENTRY_DSN?.length || 0,
sentryDsnPrefix: env.SENTRY_DSN?.substring(0, 20) || "not set",
sentryExists: !!sentry,
sentryMethods: sentry
? Object.keys(sentry)
.filter(
(k) =>
typeof (sentry as Record<string, unknown>)[k] === "function"
)
.slice(0, 10)
: [],
environment: env.SENTRY_ENVIRONMENT || env.NODE_ENV || "unknown",
allEnvKeys: Object.keys(env).filter(
(k) => !k.includes("SECRET") && !k.includes("KEY")
),
};

console.log("🔍 [SENTRY DEBUG] Diagnostics:", diagnostics);

if (!env.SENTRY_DSN) {
return c.json({ message: "Sentry not configured" });
return c.json({
status: "error",
message: "Sentry DSN not configured",
diagnostics,
});
}

const testError = new Error("Test Sentry error!");
const eventId = sentry.captureException(testError, {
tags: { test: "debug-sentry", runtime },
});
// Try to capture an exception
try {
const testError = new Error("Test Sentry error from debug endpoint!");
const eventId = sentry.captureException(testError, {
tags: { test: "debug-sentry", runtime },
});

console.log(
"✅ [SENTRY DEBUG] Test exception captured, eventId:",
eventId
);

return c.json({ error: "Test error", eventId, runtime }, 500);
return c.json(
{
status: "success",
message: "Test error and log sent to Sentry",
eventId,
diagnostics,
},
500
);
} catch (error) {
console.error("❌ [SENTRY DEBUG] Failed to send test error:", error);
return c.json(
{
status: "error",
message: "Failed to send test error",
error: error instanceof Error ? error.message : String(error),
diagnostics,
},
500
);
}
});

// BetterAuth routes
Expand Down
75 changes: 50 additions & 25 deletions packages/app/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,42 @@ declare module "@tanstack/react-router" {

// Initialize Sentry AFTER router is created (so we can include router integration)
const dsn = import.meta.env.VITE_SENTRY_DSN;

// ALWAYS log Sentry status (not just in dev)
console.log("🔍 [SENTRY DEBUG FRONTEND] Checking Sentry DSN...");
console.log("🔍 [SENTRY DEBUG FRONTEND] DSN exists:", !!dsn);
console.log("🔍 [SENTRY DEBUG FRONTEND] DSN type:", typeof dsn);
console.log("🔍 [SENTRY DEBUG FRONTEND] DSN length:", dsn?.length || 0);
console.log(
"🔍 [SENTRY DEBUG FRONTEND] DSN trimmed length:",
dsn?.trim?.()?.length || 0
);
console.log(
"🔍 [SENTRY DEBUG FRONTEND] All VITE env keys:",
Object.keys(import.meta.env).filter((k) => k.startsWith("VITE_"))
);

if (dsn && typeof dsn === "string" && dsn.trim().length > 0) {
console.log(
"✅ [SENTRY DEBUG FRONTEND] DSN validation passed, initializing Sentry..."
);

const environment =
import.meta.env.VITE_SENTRY_ENVIRONMENT ||
import.meta.env.MODE ||
"development";
const release = import.meta.env.VITE_APP_VERSION;

// Debug logging in development
if (import.meta.env.DEV) {
console.log("🔧 Sentry Configuration:", {
dsn: dsn.substring(0, 20) + "...", // Log partial DSN for debugging
environment,
release,
hasApiUrl: !!import.meta.env.VITE_API_URL,
});
}
// ALWAYS log configuration (not just in dev)
console.log("🔧 [SENTRY DEBUG FRONTEND] Sentry Configuration:", {
dsn: dsn.substring(0, 20) + "...", // Log partial DSN for debugging
dsnFull: dsn, // TEMPORARY: log full DSN to debug
environment,
release,
mode: import.meta.env.MODE,
dev: import.meta.env.DEV,
hasApiUrl: !!import.meta.env.VITE_API_URL,
});

Sentry.init({
dsn,
Expand Down Expand Up @@ -178,26 +198,31 @@ if (dsn && typeof dsn === "string" && dsn.trim().length > 0) {
},
});

// Debug logging in development
if (import.meta.env.DEV) {
console.log("✅ Sentry initialized for frontend");
}
// ALWAYS log initialization status (not just in dev)
console.log("✅ [SENTRY DEBUG FRONTEND] Sentry.init() called successfully");

// Test Sentry is working (only in development)
if (import.meta.env.DEV) {
try {
Sentry.captureMessage(
"Sentry test message - initialization complete",
"info"
);
} catch {
// Sentry not available - silently ignore
}
// Test Sentry is working (ALWAYS, not just in development)
try {
console.log("🔍 [SENTRY DEBUG FRONTEND] Sending test message...");
const eventId = Sentry.captureMessage(
"Sentry test message - initialization complete",
"info"
);
console.log(
"✅ [SENTRY DEBUG FRONTEND] Test message sent, eventId:",
eventId
);
} catch (error) {
console.error(
"❌ [SENTRY DEBUG FRONTEND] Failed to send test message:",
error
);
}
} else {
console.warn(
"⚠️ Sentry DSN not configured. Set VITE_SENTRY_DSN to enable error tracking."
console.error(
"❌ [SENTRY DEBUG FRONTEND] Sentry DSN not configured or invalid. Set VITE_SENTRY_DSN to enable error tracking."
);
console.error("❌ [SENTRY DEBUG FRONTEND] Received DSN value:", dsn);
}

// Register PWA service worker
Expand Down
Loading