Openstatus www.openstatus.dev

claude: fix oom (#1729)

authored by

Thibault Le Ouay and committed by
GitHub
d62a189e cc233520

+16 -10
+16 -10
apps/server/src/libs/middlewares/track.ts
··· 1 1 import type { Variables } from "@/types"; 2 + import { getLogger } from "@logtape/logtape"; 2 3 import { 3 4 type EventProps, 4 5 parseInputToProps, 5 6 setupAnalytics, 6 7 } from "@openstatus/analytics"; 7 8 import type { Context, Next } from "hono"; 9 + 10 + const logger = getLogger("api-server"); 8 11 9 12 export function trackMiddleware(event: EventProps, eventProps?: string[]) { 10 13 return async (c: Context<{ Variables: Variables }, "/*">, next: Next) => { ··· 26 29 const additionalProps = parseInputToProps(json, eventProps); 27 30 const workspace = c.get("workspace"); 28 31 29 - // REMINDER: use setTimeout to avoid blocking the response 30 - setTimeout(async () => { 31 - const analytics = await setupAnalytics({ 32 - userId: `api_${workspace.id}`, 33 - workspaceId: `${workspace.id}`, 34 - plan: workspace.plan, 35 - location: c.req.raw.headers.get("x-forwarded-for") ?? undefined, 36 - userAgent: c.req.raw.headers.get("user-agent") ?? undefined, 32 + setupAnalytics({ 33 + userId: `api_${workspace.id}`, 34 + workspaceId: `${workspace.id}`, 35 + plan: workspace.plan, 36 + location: c.req.raw.headers.get("x-forwarded-for") ?? undefined, 37 + userAgent: c.req.raw.headers.get("user-agent") ?? undefined, 38 + }) 39 + .then((analytics) => analytics.track({ ...event, additionalProps })) 40 + .catch(() => { 41 + logger.warn( 42 + "Failed to send analytics event {event} for workspace {workspaceId}", 43 + { event: event.name, workspaceId: workspace.id }, 44 + ); 37 45 }); 38 - await analytics.track({ ...event, additionalProps }); 39 - }, 0); 40 46 } 41 47 }; 42 48 }