Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Add template meta helper docs for Django/Fastapi/Starlette/Flask. #7325

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ In this example, tracing information is propagated to the project running at `ht

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

<PlatformSection supported={["python"]} notSupported={["python.django", "python.flask", "python.fastapi", "python.starlette"]}>

### Inject Tracing Information Into Rendered HTML

To propagate tracing information into JavaScript running in rendered HTML you have to inject HTML `meta` tags for `sentry-trace` and `baggage` data into your rendered HTML. Here's an example:
Expand Down Expand Up @@ -79,6 +81,180 @@ render(html)

```

</PlatformSection>

<PlatformSection supported={["python.django"]}>

### Inject Tracing Information Into Rendered HTML

To propagate tracing information into JavaScript running in rendered HTML you have to inject HTML `meta` tags in your rendered HTML. For Django Sentry provides a variable `sentry_trace_meta` to your template context:
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

```html {filename: myproject/templates/mydjangoapp/index.html}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
{{ sentry_trace_meta }}
</head>
<body>
<p>This is a website.</p>
</body>
</html>
```

The rendered template will then look something like this:

```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta
name="sentry-trace"
content="98e5de1c09e444469475855d313e22c3-b995922394e8433d"
/>
<meta
name="baggage"
content="sentry-trace_id=98e5de1c09e444469475855d313e22c3,sentry-environment=prod,sentry-public_key=..."
/>
</head>
<body>
<p>This is a website.</p>
</body>
</html>
```

</PlatformSection>

<PlatformSection supported={["python.flask"]}>

### Inject Tracing Information Into Rendered HTML

To propagate tracing information into JavaScript running in rendered HTML you have to inject HTML `meta` tags in your rendered HTML. For Flask Sentry provides a variable `sentry_trace_meta` to your template context:
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

```html {filename: templates/index.html}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
{{ sentry_trace_meta }}
</head>
<body>
<p>This is a website.</p>
</body>
</html>
```

The rendered template will then look something like this:

```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta
name="sentry-trace"
content="98e5de1c09e444469475855d313e22c3-b995922394e8433d"
/>
<meta
name="baggage"
content="sentry-trace_id=98e5de1c09e444469475855d313e22c3,sentry-environment=prod,sentry-public_key=..."
/>
</head>
<body>
<p>This is a website.</p>
</body>
</html>
```

</PlatformSection>

<PlatformSection supported={["python.fastapi"]}>

### Inject Tracing Information Into Rendered HTML

To propagate tracing information into JavaScript running in rendered HTML you have to inject HTML `meta` tags in your rendered HTML. For FastAPI Sentry provides a variable `sentry_trace_meta` to your Jinja2 template context:
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

```html {filename: templates/index.html}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
{{ sentry_trace_meta }}
</head>
<body>
<p>This is a website.</p>
</body>
</html>
```

The rendered template will then look something like this:

```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta
name="sentry-trace"
content="98e5de1c09e444469475855d313e22c3-b995922394e8433d"
/>
<meta
name="baggage"
content="sentry-trace_id=98e5de1c09e444469475855d313e22c3,sentry-environment=prod,sentry-public_key=..."
/>
</head>
<body>
<p>This is a website.</p>
</body>
</html>
```

</PlatformSection>

<PlatformSection supported={["python.starlette"]}>

### Inject Tracing Information Into Rendered HTML

To propagate tracing information into JavaScript running in rendered HTML you have to inject HTML `meta` tags in your rendered HTML. For Starlette Sentry provides a variable `sentry_trace_meta` to your Jinja2 template context:
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

```html {filename: templates/index.html}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
{{ sentry_trace_meta }}
</head>
<body>
<p>This is a website.</p>
</body>
</html>
```

The rendered template will then look something like this:

```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta
name="sentry-trace"
content="98e5de1c09e444469475855d313e22c3-b995922394e8433d"
/>
<meta
name="baggage"
content="sentry-trace_id=98e5de1c09e444469475855d313e22c3,sentry-environment=prod,sentry-public_key=..."
/>
</head>
<body>
<p>This is a website.</p>
</body>
</html>
```

</PlatformSection>

## Verification

If you make outgoing requests from your project to other services, check if the headers `sentry-trace` and `baggage` are present in the request. If so, distributed tracing is working.