Skip to content

Commit d62beaf

Browse files
Lms24lizokmmydea
authored
feat(javascript): Add documentation for injecting Html <meta> tags (#10926)
Add documentation for new JS APIs `getTraceMetaTags` and `getTraceData` --------- Co-authored-by: Liza Mock <[email protected]> Co-authored-by: Francesco Novy <[email protected]>
1 parent 2b04df7 commit d62beaf

File tree

1 file changed

+47
-2
lines changed
  • platform-includes/distributed-tracing/custom-instrumentation

1 file changed

+47
-2
lines changed

platform-includes/distributed-tracing/custom-instrumentation/javascript.mdx

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ In this example, we create a new transaction that is attached to the trace speci
169169
170170
</PlatformCategorySection>
171171
172-
### Inject Tracing Information to Outgoing Requests
172+
### Inject Tracing Information into Outgoing Requests
173173
174174
For distributed tracing to work, the two headers that you extracted and stored in the active root span, `sentry-trace` and `baggage`, must be added to outgoing HTTP requests.
175175
@@ -205,6 +205,51 @@ In this example, tracing information is propagated to the project running at `ht
205205

206206
The two services are now connected with your custom distributed tracing implementation.
207207

208+
<PlatformCategorySection notSupported={['browser']}>
209+
210+
### Injecting Tracing Information into HTML
211+
212+
If you're server-side rendering HTML and you use a Sentry SDK in your browser application, you can connect the backend and frontend traces by injecting your server's tracing information as `<meta>` tags into the HTML that's initially served to the browser. When the frontend SDK is initialized, it will automatically pick up the tracing information from the `<meta>` tags and continue the trace. Note, that your browser SDK needs to register `browserTracingIntegration` for this to work.
213+
214+
The easiest and recommended way to do this is to use the `Sentry.getTraceMetaTags()`:
215+
216+
```javascript {5} {filename:index.js}
217+
function renderHtml() {
218+
return `
219+
<html>
220+
<head>
221+
${Sentry.getTraceMetaTags()}
222+
</head>
223+
<body>
224+
<!-- Your HTML content -->
225+
</body>
226+
</html>
227+
`;
228+
}
229+
```
230+
231+
Alternatively, if you need more control over how meta tags are generated, you can use `Sentry.getTraceData()` to get only the meta tag values and generate the meta tags yourself:
232+
233+
```javascript {2, 7-8} {filename:index.js}
234+
function renderHtml() {
235+
const metaTagValues = Sentry.getTraceData();
236+
237+
return `
238+
<html>
239+
<head>
240+
<meta name="sentry-trace" content="${metaTagValues["sentry-trace"]}">
241+
<meta name="baggage" content="${metaTagValues.baggage}">
242+
</head>
243+
<body>
244+
<!-- Your HTML content -->
245+
</body>
246+
</html>
247+
`;
248+
}
249+
```
250+
251+
</PlatformCategorySection>
252+
208253
### Starting a New Trace
209254
210255
Available since SDK version `8.5.0`.
@@ -214,7 +259,7 @@ This means that spans or errors collected by the SDK during this new trace will
214259

215260
To start a new trace that remains valid throughout the duration of a callback, use `startNewTrace`:
216261

217-
```javascript {2-6}
262+
```javascript {2-9}
218263
myButton.addEventListener("click", async () => {
219264
Sentry.startNewTrace(() => {
220265
Sentry.startSpan(

0 commit comments

Comments
 (0)