Skip to content

Commit f074fb3

Browse files
committed
improve breadcrumb descriptions by adding wrapper element ids
1 parent c946b78 commit f074fb3

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

instrumentation-client.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,48 @@ import * as Sentry from "@sentry/nextjs"
22

33
const environment = process.env.NEXT_PUBLIC_CONTEXT || "development"
44

5-
if (environment === "production") {
6-
Sentry.init({
7-
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
8-
tracesSampleRate: 0.1,
9-
debug: false,
10-
environment,
11-
})
5+
/**
6+
* Finds the closest element (including the element itself) that has an id attribute
7+
* @param element - The starting element to search from
8+
* @param maxDepth - Maximum number of parent levels to search (default: 3)
9+
* @returns The first found attribute value in priority order, null otherwise
10+
*/
11+
function findClosestElementId(
12+
element: Element | null | undefined,
13+
maxDepth: number = 3
14+
): string | null {
15+
if (!element || maxDepth < 0) return null
16+
17+
const sentryId = element.getAttribute("data-testid")
18+
if (sentryId) return sentryId
19+
20+
const ariaLabel = element.getAttribute("aria-label")
21+
if (ariaLabel) return ariaLabel
22+
23+
const id = element.getAttribute("id")
24+
if (id) return id
25+
26+
// Recursively check parent elements up to maxDepth
27+
return findClosestElementId(element.parentElement, maxDepth - 1)
1228
}
1329

30+
Sentry.init({
31+
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
32+
tracesSampleRate: 0.1,
33+
environment,
34+
enabled: environment === "production",
35+
beforeBreadcrumb(breadcrumb, hint) {
36+
if (breadcrumb.category === "ui.click") {
37+
const element = hint?.event?.target
38+
39+
const id = findClosestElementId(element)
40+
if (id) {
41+
breadcrumb.message = id + " (" + breadcrumb.message + ")"
42+
}
43+
}
44+
45+
return breadcrumb
46+
},
47+
})
48+
1449
export const onRouterTransitionStart = Sentry.captureRouterTransitionStart

next.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,19 @@ module.exports = (phase, { defaultConfig }) => {
137137
},
138138
}
139139

140+
nextConfig = {
141+
...nextConfig,
142+
experimental: {
143+
...experimental,
144+
instrumentationHook: true,
145+
},
146+
}
147+
140148
if (phase !== PHASE_DEVELOPMENT_SERVER) {
141149
nextConfig = {
142150
...nextConfig,
143151
experimental: {
144-
...experimental,
152+
...nextConfig.experimental,
145153
outputFileTracingExcludes: {
146154
"*": [
147155
/**

0 commit comments

Comments
 (0)