import { debug, getActiveSpan, getRootSpan, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from "@sentry/core"; function getMatchedRoutePath(event) { const matchedRoute = event.context.matchedRoute; return matchedRoute?.path ?? matchedRoute?.route; } export function updateRouteBeforeResponse(event) { if (!event.context.matchedRoute) { return; } const matchedRoutePath = getMatchedRoutePath(event); const requestPath = event.path ?? event._path; if (matchedRoutePath && matchedRoutePath !== requestPath) { if (matchedRoutePath === "/**") { return; } const activeSpan = getActiveSpan(); if (!activeSpan) { return; } const rootSpan = getRootSpan(activeSpan); if (!rootSpan) { return; } rootSpan.setAttributes({ [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: "route", "http.route": matchedRoutePath }); const params = event.context?.params; if (params && typeof params === "object") { Object.entries(params).forEach(([key, value]) => { rootSpan.setAttributes({ [`url.path.parameter.${key}`]: String(value), [`params.${key}`]: String(value) }); }); } debug.log(`Updated transaction name for parametrized route: ${matchedRoutePath}`); } }