Skip to content

Commit a6ef44c

Browse files
Tracing without performance documentation (#7176)
Add documentation for the built-in distributed tracing support (without the need to turn on performance monitoring). Merge docs with existing docs on "Performance Monitoring > Connecting Services".
1 parent dfc1477 commit a6ef44c

File tree

37 files changed

+364
-76
lines changed

37 files changed

+364
-76
lines changed

src/docs/contributing/approach/sdk-docs/write-performance.mdx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ If any of the above files are not applicable to your platform, find the `<Platfo
6666

6767
Then, determine whether to provide a code sample illustrating how to connect errors and spans. If so, add the SDK to the `Supported` list in the appropriate `<PlatformSection>`, then add a code sample `/src/platform-includes/performance/connect-errors-spans/`.
6868

69-
If the platform supports distributed tracing using custom instrumentation, add the SDK to the appropriate `<PlatformSection>` and create an appropriate file at `src/platform-includes/performance/distributed-tracing`.
70-
7169
### Automatic Instrumentation
7270

7371
This page does not use common content. It covers what instrumentation is automatic when tracing is enabled for this SDK.
@@ -82,10 +80,6 @@ This file is `troubleshooting-performance.mdx`. It explains corner cases and how
8280

8381
2. Add an example to `src/platform-includes/performance/control-data-truncation/`
8482

85-
### Connecting Services
86-
87-
This page does not currently use common content. It covers an example of how to connect frontend and backend services. In future, we may determine that it's useful to expand the content beyond the JS-specific focus.
88-
8983
### Sampling
9084

9185
Guidelines TBD.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Configuration
2+
3+
By default, trace information (`sentry-trace` and `baggage` headers) will be added to outgoing XHR/fetch requests that contain `localhost` in their URL and requests where the URL starts with `'/'`, for example, `/api/v1/users`.
4+
5+
To configure which URLs have trace information added, set the `tracePropagationTargets` option:
6+
7+
```javascript
8+
Sentry.init({
9+
// ...
10+
integrations: [
11+
new Sentry.BrowserTracing({
12+
tracePropagationTargets: ["localhost", /^https:\/\/api\.example\.com/],
13+
}),
14+
],
15+
});
16+
```
17+
18+
In this example, trace information will be added to outgoing XHR/fetch requests to `localhost` and URLs starting with `https://api.example.com`.
19+
20+
See <PlatformLink to="/configuration/options/#trace-propagation-targets">Tracing Options</PlatformLink> docs for more information about the `trace_propagation_targets` option.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Configuration
2+
3+
By default, trace information (`sentry-trace` and `baggage` headers) will be added to all outgoing HTTP requests. If you want to limit to the URLs where trace information is added, you can specify a `trace_propagation_targets` option:
4+
5+
```python
6+
import sentry_sdk
7+
8+
sentry_sdk.init(
9+
dsn="___PUBLIC_DSN___",
10+
trace_propagation_targets=[
11+
"https://myproject.org",
12+
r"https://.*\.otherservice.org/.*",
13+
],
14+
# ...
15+
)
16+
```
17+
18+
In the example above, trace information will be added to all requests to URLs that contain `"https://myproject.org"` and to all URLs of all sub domains of `otherservice.org` like `https://api.otherservice.org/something/` or `https://payment.otherservice.org/something/`.
19+
20+
See <PlatformLink to="/configuration/options/#trace-propagation-targets">Tracing Options</PlatformLink> docs for more information about the `trace_propagation_targets` option.

src/platform-includes/distributed-tracing/TODO-INTEGRATE-instrumentation/automatic-instrumentation-intro/_default.mdx

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
To enable distributed tracing, add the `BrowserTracing` integration to your `Sentry.init` options, as described in the Performance <PlatformLink to="/performance/instrumentation/automatic-instrumentation/"> Automatic Instrumentation</PlatformLink> docs. The [Next.js](/platforms/javascript/guides/nextjs/) and [SvelteKit](/platforms/javascript/guides/sveltekit/) SDKs include this integration by default.
2+
3+
Tracing information sent from the backend via the `sentry-tracing` and `baggage` HTML `meta` tags will be extracted and stored in a transaction that is created at page load.
4+
5+
The `sentry-trace` and `baggage` headers will be added to all your outgoing XHR/fetch requests.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Configuration
2+
3+
To enable distributed tracing, add the `BrowserTracing` integration to your `Sentry.init` options, as described in the Performance <PlatformLink to="/performance/instrumentation/automatic-instrumentation/">Automatic Instrumentation</PlatformLink> docs. Sentry's [Next.js](/platforms/javascript/guides/nextjs/) and [SvelteKit](/platforms/javascript/guides/sveltekit/) SDKs include this integration by default.
4+
5+
See the instructions at <PlatformLink to="/platforms/javascript/performance/">Set Up Performance</PlatformLink> for more information on how to configure tracing.
6+
7+
Additionally, if your backend is hosted on a different domain, you may need to <PlatformLink to="/distributed-tracing/instrumentation/automatic-instrumentation/#configure-backend-cors-headers">Configure Backend CORS Headers</PlatformLink> to ensure requests aren't blocked.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
On this page you will learn how to manually propagate trace information into and out of your JavaScript application. Please note, that you do not need to do this manually, if you [use one of our supported frameworks](/platforms/javascript/usage/distributed-tracing/#how-to-use-distributed-tracing), or you have our <PlatformLink to="/performance/">performance monitoring feature</PlatformLink> turned on.
2+
3+
To set it up manually, all you have to do is to make sure your application extracts incoming headers and to set those headers again when making an outgoing request within your application.
4+
5+
To enable distributed tracing, add the `BrowserTracing` integration to your `Sentry.init` options, as described in the Performance <PlatformLink to="/performance/instrumentation/automatic-instrumentation/"> Automatic Instrumentation</PlatformLink> docs. The [Next.js](/platforms/javascript/guides/nextjs/) and [SvelteKit](/platforms/javascript/guides/sveltekit/) SDKs include this integration by default.
6+
7+
If you are using one of the frameworks listed above, see the <PlatformLink to="/distributed-tracing/instrumentation/automatic-instrumentation/">Automatic Instrumentation</PlatformLink>. page on how to configure distributed tracing.
8+
9+
If you prefer to implement custom distributed tracing, you must:
10+
11+
- Extract and store incoming tracing information from HTML `meta` tags when loading the page
12+
- Inject tracing information to the outgoing request when sending outgoing requests.
13+
14+
To learn more, see our <PlatformLink to="/distributed-tracing/">Distributed Tracing</PlatformLink> docs.
15+
16+
## Extract Tracing Information From HTML `meta` Tags
17+
18+
Assuming the backend that renders the HTML has injected tracing information into the HTML as `meta` tags, you can extract that tracing information on `pageload` and use it to create new transactions connected to that incoming trace.
19+
20+
See the docs on how to [Inject Tracing Information Into Rendered HTML](/platforms/python/distributed-tracing/instrumentation/custom-instrumentation/#inject-tracing-information-into-rendered-html) to learn more about how to configure your backend to inject this information.
21+
22+
For example, on `pageload` you could do the following:
23+
24+
```javascript
25+
// Read meta tag values
26+
const sentryTraceMetaTagValue = getMetaContent("sentry-trace");
27+
const baggageMetaTagValue = getMetaContent("baggage");
28+
29+
// Extract Sentry trace information
30+
const traceParentData = extractTraceparentData(sentryTraceMetaTagValue);
31+
32+
// Create Dynamic Sampling Context
33+
const dynamicSamplingContext =
34+
baggageHeaderToDynamicSamplingContext(baggageMetaTagValue);
35+
36+
// Create transaction context
37+
const transactionContext = {
38+
...traceParentData,
39+
metadata: {
40+
dynamicSamplingContext: dynamicSamplingContext,
41+
},
42+
};
43+
44+
// Start transaction with tracing information of meta tags
45+
const pageLoadTransaction = startTransaction(hub, transactionContext);
46+
```
47+
48+
In this example, we create a new transaction that is attached to the trace specified in the `sentry-trace` and `baggage` HTML `meta` tags.
49+
50+
## Inject Tracing Information to Outgoing Requests
51+
52+
For distributed tracing to work, the two headers that you extracted and stored in the `pageLoadTransaction`, `sentry-trace` and `baggage`, must be added to outgoing XHR/fetch requests.
53+
54+
Here's an example of how to collect and inject this tracing information to outgoing requests:
55+
56+
```javascript
57+
// Create `sentry-trace` header
58+
const sentryTraceHeader = pageLoadTransaction.toTraceparent();
59+
60+
// Create `baggage` header
61+
const dynamicSamplingContext = pageLoadTransaction.getDynamicSamplingContext();
62+
const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(
63+
dynamicSamplingContext
64+
);
65+
66+
// Make outgoing request
67+
fetch("https://example.com", {
68+
method: "GET",
69+
headers: {
70+
baggage: sentryBaggageHeader,
71+
"sentry-trace": sentryTraceHeader,
72+
},
73+
}).then((response) => {
74+
// ...
75+
});
76+
```
77+
78+
In this example, tracing information is propagated to the project running at `https://example.com`. If this project uses a Sentry SDK, it will extract and save the tracing information for later use.
79+
80+
The two services are now connected with your custom distributed tracing implementation.
81+
82+
## Verification
83+
84+
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.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
On this page you will learn how to manually propagate trace information into and out of your Python application. Please note, that you do not need to do this manually, if you [use one of our supported frameworks](/platforms/python/usage/distributed-tracing/#how-to-use-distributed-tracing), or you have our <PlatformLink to="/performance/">performance monitoring feature</PlatformLink> turned on.
2+
3+
To set it up manually, all you have to do is to make sure your application extracts incoming headers and to set those headers again when making an outgoing request within your application.
4+
5+
## Step 1) Extract Incoming Tracing Information
6+
7+
Incoming tracing information has to be extracted and stored in memory for later use. Sentry provides the `continue_trace()` function to help you with this. Incoming tracing information can come from different places:
8+
9+
- In a web environment it will be sent with HTTP headers, for example, by another Sentry SDK used in your frontend project.
10+
- In a job queue, like Celery, it can be retrieved from meta or header variables.
11+
- You also can pick up tracing information from environment variables.
12+
13+
Here's an example of how to extract and store incoming tracing information using `continue_trace()`:
14+
15+
```python
16+
import sentry_sdk
17+
from my_project import get_incoming_headers_as_dict
18+
19+
headers = get_incoming_headers_as_dict()
20+
21+
sentry_sdk.continue_trace(headers)
22+
```
23+
24+
In this example, `get_incoming_headers_as_dict()` returns a dictionary that contains tracing information from HTTP headers, environment variables, or any other mechanism your project uses to communicate with the outside world.
25+
26+
Sentry's `continue_trace()` function will extract the given headers, try to find the `sentry-trace` and `baggage` headers, and store them in memory for later use.
27+
28+
## Step 2) Inject Tracing Information to Outgoing Requests
29+
30+
For distributed tracing to work, the two headers `sentry-trace` and `baggage`, must now also be added to outgoing requests. If you pregenerate HTML on the server-side, you might want to take a look at option 2 as well, which describes how to pass on tracing information through HTML meta tags.
31+
32+
### Inject Tracing Information Into HTTP Requests
33+
34+
If you are sending outgoing HTTP requests with [Requests](https://requests.readthedocs.io/en/latest/), [AIOHTTP](https://docs.aiohttp.org/en/stable/), the low level [http.client](https://docs.python.org/3/library/http.client.html), or [httplib](https://docs.python.org/2/library/httplib.html) on Python 2, this tracing information is automatically added to outgoing requests.
35+
36+
If your using none of the above, you can generate this tracing information with the Sentry SDK's `get_traceparent()` and `get_baggage()` functions. Here's an example:
37+
38+
```python
39+
import sentry_sdk
40+
from my_project import make_an_outgoing_request
41+
42+
headers = {}
43+
headers["sentry-trace"] = sentry_sdk.get_traceparent()
44+
headers["baggage"] = sentry_sdk.get_baggage()
45+
46+
make_an_outgoing_request(to="https://example.com", headers=headers)
47+
```
48+
49+
In this example, tracing information is propagated to the project running at `https://example.com`. If this project uses the Sentry Python SDK, it will extract and save the tracing information for later use.
50+
51+
The two services are now connected with your custom distributed tracing implementation.
52+
53+
### Inject Tracing Information Into Rendered HTML
54+
55+
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:
56+
57+
```python
58+
import sentry_sdk
59+
from my_project import render
60+
61+
meta = ""
62+
meta += '<meta name="sentry-trace" content="%s">' % sentry_sdk.get_traceparent()
63+
meta += '<meta name="baggage" content="%s">' % sentry_sdk.get_baggage()
64+
65+
html = """
66+
<!DOCTYPE html>
67+
<html>
68+
<head>
69+
<meta charset="UTF-8">
70+
{additional_meta}
71+
</head>
72+
<body>
73+
<p>This is a website.</p>
74+
</body>
75+
</html>
76+
""".format(additional_meta=meta)
77+
78+
render(html)
79+
80+
```
81+
82+
## Verification
83+
84+
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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In order to use distributed tracing with this SDK, you have to <PlatformLink to="/performance/">activate performance monitoring</PlatformLink>.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
If you use the current version of our Python SDK, distributed tracing just works out of the box. No additional configuration is required.
2+
3+
If you however use version `1.25.x` or below, you need to have our <PlatformLink to="/performance/">performance monitoring feature enabled</PlatformLink> for distributed tracing to work.

0 commit comments

Comments
 (0)