fix: sentry tracing

This commit is contained in:
devthejo 2025-06-23 00:07:49 +02:00
parent 8ba4056187
commit 4a0f3ab7ef
2 changed files with 217 additions and 215 deletions

View file

@ -346,11 +346,12 @@ export default async function trackLocation() {
if (count > 0) { if (count > 0) {
locationLogger.info(`Found ${count} pending records, forcing sync`); locationLogger.info(`Found ${count} pending records, forcing sync`);
const transaction = Sentry.startTransaction({ await Sentry.startSpan(
{
name: "force-sync-pending-records", name: "force-sync-pending-records",
op: "geolocation-sync", op: "geolocation-sync",
}); },
async (span) => {
try { try {
const { userToken } = getAuthState(); const { userToken } = getAuthState();
const state = await BackgroundGeolocation.getState(); const state = await BackgroundGeolocation.getState();
@ -371,7 +372,7 @@ export default async function trackLocation() {
}, },
}); });
transaction.setStatus("ok"); span.setStatus("ok");
} else { } else {
Sentry.addBreadcrumb({ Sentry.addBreadcrumb({
message: "Forced sync skipped", message: "Forced sync skipped",
@ -383,7 +384,7 @@ export default async function trackLocation() {
}, },
}); });
transaction.setStatus("cancelled"); span.setStatus("cancelled");
} }
} catch (error) { } catch (error) {
locationLogger.error("Forced sync failed", { locationLogger.error("Forced sync failed", {
@ -401,10 +402,11 @@ export default async function trackLocation() {
}, },
}); });
transaction.setStatus("internal_error"); span.setStatus("internal_error");
} finally { throw error; // Re-throw to ensure span captures the error
transaction.finish();
} }
},
);
} }
} catch (error) { } catch (error) {
locationLogger.error("Failed to get pending records count", { locationLogger.error("Failed to get pending records count", {

View file

@ -12,13 +12,12 @@ const logger = createLogger({
// Background task to cancel expired notifications // Background task to cancel expired notifications
const backgroundTask = async () => { const backgroundTask = async () => {
const transaction = Sentry.startTransaction({ await Sentry.startSpan(
{
name: "auto-cancel-expired-notifications", name: "auto-cancel-expired-notifications",
op: "background-task", op: "background-task",
}); },
async (span) => {
Sentry.getCurrentScope().setSpan(transaction);
try { try {
logger.info("Starting auto-cancel expired notifications task"); logger.info("Starting auto-cancel expired notifications task");
@ -29,12 +28,13 @@ const backgroundTask = async () => {
}); });
// Get displayed notifications with timeout protection // Get displayed notifications with timeout protection
const getNotificationsSpan = transaction.startChild({ let notifications;
await Sentry.startSpan(
{
op: "get-displayed-notifications", op: "get-displayed-notifications",
description: "Getting displayed notifications", description: "Getting displayed notifications",
}); },
async (getNotificationsSpan) => {
let notifications;
try { try {
// Add timeout protection for the API call // Add timeout protection for the API call
notifications = await Promise.race([ notifications = await Promise.race([
@ -50,9 +50,9 @@ const backgroundTask = async () => {
} catch (error) { } catch (error) {
getNotificationsSpan.setStatus("internal_error"); getNotificationsSpan.setStatus("internal_error");
throw error; throw error;
} finally {
getNotificationsSpan.finish();
} }
},
);
if (!Array.isArray(notifications)) { if (!Array.isArray(notifications)) {
logger.warn("No notifications array received", { notifications }); logger.warn("No notifications array received", { notifications });
@ -166,7 +166,7 @@ const backgroundTask = async () => {
}, },
}); });
transaction.setStatus("ok"); span.setStatus("ok");
} catch (error) { } catch (error) {
logger.error("Auto-cancel task failed", { error }); logger.error("Auto-cancel task failed", { error });
@ -177,11 +177,11 @@ const backgroundTask = async () => {
}, },
}); });
transaction.setStatus("internal_error"); span.setStatus("internal_error");
throw error; // Re-throw to be handled by caller throw error; // Re-throw to be handled by caller
} finally {
transaction.finish();
} }
},
);
}; };
export const useAutoCancelExpired = () => { export const useAutoCancelExpired = () => {