fix: sentry tracing

This commit is contained in:
devthejo 2025-06-24 08:51:54 +02:00
parent 4a0f3ab7ef
commit e6924ac9ff
2 changed files with 37 additions and 46 deletions

View file

@ -104,7 +104,8 @@ const HeadlessTask = async (event) => {
}); });
}, 60000); // 60 second timeout }, 60000); // 60 second timeout
let transaction; // Simple performance tracking without deprecated APIs
const taskStartTime = Date.now();
try { try {
// Validate event structure // Validate event structure
@ -118,15 +119,6 @@ const HeadlessTask = async (event) => {
throw new Error("Invalid event name received"); throw new Error("Invalid event name received");
} }
// Start Sentry transaction for the entire HeadlessTask
transaction = Sentry.startTransaction({
name: "headless-task",
op: "background-task",
data: { eventName: name },
});
Sentry.getCurrentScope().setSpan(transaction);
// Add initial breadcrumb // Add initial breadcrumb
Sentry.addBreadcrumb({ Sentry.addBreadcrumb({
message: "HeadlessTask started", message: "HeadlessTask started",
@ -181,14 +173,10 @@ const HeadlessTask = async (event) => {
}, },
}); });
// Get current position // Get current position with performance tracking
const locationSpan = transaction.startChild({ const locationStartTime = Date.now();
op: "get-current-position",
description: "Getting current position",
});
const location = await getCurrentPosition(); const location = await getCurrentPosition();
locationSpan.finish(); const locationDuration = Date.now() - locationStartTime;
const isLocationError = location && location.code !== undefined; const isLocationError = location && location.code !== undefined;
@ -237,11 +225,7 @@ const HeadlessTask = async (event) => {
}); });
// Change pace to ensure location updates with timeout // Change pace to ensure location updates with timeout
const paceSpan = transaction.startChild({ const paceStartTime = Date.now();
op: "change-pace",
description: "Changing pace to true",
});
await Promise.race([ await Promise.race([
BackgroundGeolocation.changePace(true), BackgroundGeolocation.changePace(true),
new Promise((_, reject) => new Promise((_, reject) =>
@ -251,27 +235,24 @@ const HeadlessTask = async (event) => {
), ),
), ),
]); ]);
paceSpan.finish(); const paceDuration = Date.now() - paceStartTime;
Sentry.addBreadcrumb({ Sentry.addBreadcrumb({
message: "changePace completed", message: "changePace completed",
category: "headless-task", category: "headless-task",
level: "info", level: "info",
data: { duration: paceDuration },
}); });
// Perform sync with timeout // Perform sync with timeout
const syncSpan = transaction.startChild({ const syncStartTime = Date.now();
op: "sync-locations",
description: "Syncing locations",
});
const syncResult = await Promise.race([ const syncResult = await Promise.race([
BackgroundGeolocation.sync(), BackgroundGeolocation.sync(),
new Promise((_, reject) => new Promise((_, reject) =>
setTimeout(() => reject(new Error("sync timeout")), 20000), setTimeout(() => reject(new Error("sync timeout")), 20000),
), ),
]); ]);
syncSpan.finish(); const syncDuration = Date.now() - syncStartTime;
Sentry.addBreadcrumb({ Sentry.addBreadcrumb({
message: "Sync completed successfully", message: "Sync completed successfully",
@ -413,35 +394,46 @@ const HeadlessTask = async (event) => {
}); });
} }
// Finish transaction successfully // Task completed successfully
if (transaction) { const taskDuration = Date.now() - taskStartTime;
transaction.setStatus("ok");
} Sentry.addBreadcrumb({
message: "HeadlessTask completed successfully",
category: "headless-task",
level: "info",
data: {
eventName: name,
duration: taskDuration,
},
});
} catch (error) { } catch (error) {
const taskDuration = Date.now() - taskStartTime;
// Capture any unexpected errors // Capture any unexpected errors
Sentry.captureException(error, { Sentry.captureException(error, {
tags: { tags: {
module: "headless-task", module: "headless-task",
eventName: event?.name || "unknown", eventName: event?.name || "unknown",
}, },
extra: {
duration: taskDuration,
},
}); });
geolocBgLogger.error("HeadlessTask error", { error, event }); geolocBgLogger.error("HeadlessTask error", {
error,
// Mark transaction as failed event,
if (transaction) { duration: taskDuration,
transaction.setStatus("internal_error"); });
}
} finally { } finally {
// Clear the timeout // Clear the timeout
clearTimeout(taskTimeout); clearTimeout(taskTimeout);
// Always finish the transaction const finalDuration = Date.now() - taskStartTime;
if (transaction) { geolocBgLogger.debug("HeadlessTask completed", {
transaction.finish(); event: event?.name,
} duration: finalDuration,
});
geolocBgLogger.debug("HeadlessTask completed", { event: event?.name });
} }
}; };

View file

@ -1,5 +1,4 @@
import * as Sentry from "@sentry/react-native"; import * as Sentry from "@sentry/react-native";
import "@sentry/tracing";
import { Platform } from "react-native"; import { Platform } from "react-native";
import env from "~/env"; import env from "~/env";