Skip to content

Commit e24c21a

Browse files
author
Luca Forstner
authored
Add instructions of how debug ids need to be injected (#11352)
1 parent 2f2fed8 commit e24c21a

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: Debug IDs
3+
description: Requirements for the JS SDK to pick up Debug IDs
4+
sidebar_order: 12
5+
---
6+
7+
## Requirements
8+
9+
This section outlines everything needed for Sentry and the Sentry SDK to pick up Debug IDs to unminify/symbolicate JavaScript files.
10+
It explains the necessary modifications to generated artifacts for source mapping to work.
11+
12+
We follow our [TC39 Debug ID Proposal](https://github.com/tc39/source-map/blob/main/proposals/debug-id.md). However, because Debug IDs are not yet a standard, we need to employ a workaround.
13+
14+
### The Shape of Debug IDs
15+
16+
Debug IDs should be UUIDs, e.g., `85314830-023f-4cf1-a267-535f4e37bb17`. The UUID version does not matter.
17+
18+
### Debug ID References
19+
20+
Embed Debug IDs in JavaScript files and their source map for Sentry indexing.
21+
22+
JavaScript files should have a `//# debugId=...` comment at the end:
23+
24+
```js
25+
"use strict";
26+
console.log("Hello world!");
27+
//# debugId=85314830-023f-4cf1-a267-535f4e37bb17
28+
```
29+
30+
If a JS file has a source map reference, append the `debugId` comment after the `sourceMappingURL` comment:
31+
32+
```js
33+
"use strict";
34+
console.log("Hello world!");
35+
//# sourceMappingURL=index.js.map
36+
//# debugId=85314830-023f-4cf1-a267-535f4e37bb17
37+
```
38+
39+
The corresponding source map should embed the same Debug ID using both `debug_id` and `debugId` fields for compatibility:
40+
41+
```json
42+
{
43+
"version": 3,
44+
"file": "index.js",
45+
"sources": [],
46+
"sourcesContent": [],
47+
"names": [],
48+
"mappings": "...",
49+
"debug_id": "85314830-023f-4cf1-a267-535f4e37bb17",
50+
"debugId": "85314830-023f-4cf1-a267-535f4e37bb17"
51+
}
52+
```
53+
54+
Since JS runtimes do not provide access to the `//# debugId=...` comment, inform the Sentry SDK about the Debug IDs for all loaded JavaScript files via a global variable.
55+
56+
Prepend each JavaScript file with a snippet to write file information and Debug IDs into a global variable. **Ensure that any changes to a JavaScript file also update the `mappings` in the corresponding source map.**
57+
58+
The snippet should look as follows:
59+
60+
```js
61+
{
62+
var globalObject =
63+
typeof window !== "undefined"
64+
? window
65+
: typeof global !== "undefined"
66+
? global
67+
: typeof self !== "undefined"
68+
? self
69+
: {};
70+
71+
var stack = new globalObject.Error().stack;
72+
73+
if (stack) {
74+
globalObject._sentryDebugIds = globalObject._sentryDebugIds || {};
75+
globalObject._sentryDebugIds[stack] =
76+
"85314830-023f-4cf1-a267-535f4e37bb17";
77+
}
78+
}
79+
```
80+
81+
This snippet creates an error to capture the stack trace, which includes the file name/path. The stack trace is used as a key to map to the Debug ID for that file inside a global `_sentryDebugIds` object. The Sentry SDK parses out the Debug IDs and file names from this object when an error is captured.
82+
83+
You can minify this snippet into one line, which will simplify updating the source map `mappings`.

0 commit comments

Comments
 (0)