Skip to content

Commit b36541b

Browse files
authored
Go: Add custom trace propagation (#11284)
1 parent 0d6b4bf commit b36541b

File tree

1 file changed

+42
-0
lines changed
  • platform-includes/distributed-tracing/custom-instrumentation

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
On this page you will learn how to manually propagate trace information into and out of your Go application.
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 `sentry.ContinueTrace()` function to help you with this. Incoming tracing information can come from different places:
8+
9+
- In a web environment it will be sent via HTTP headers, for example, by another Sentry SDK used in your frontend project.
10+
- You also can pick up tracing information from environment variables.
11+
12+
Here's an example of how to extract and store incoming tracing information using `sentry.ContinueTrace()`:
13+
14+
```go
15+
hub := sentry.CurrentHub()
16+
sentry.ContinueTrace(hub, r.Header.Get(sentry.SentryTraceHeader), r.Header.Get(sentry.SentryBaggageHeader)),
17+
```
18+
19+
Sentry's `sentry.ContinueTrace()` function will extract the given headers, try to find the `sentry-trace` and `baggage` headers, and store them in memory for later use.
20+
21+
## Step 2) Inject Tracing Information to Outgoing Requests
22+
23+
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.
24+
25+
### Inject Tracing Information Into HTTP Requests
26+
27+
You can generate this tracing information with the Sentry SDK's `hub.GetTraceparent()` and `hub.GetBaggage()` functions. Here's an example using Guzzle:
28+
29+
```go
30+
hub := sentry.CurrentHub()
31+
req, _ := http.NewRequest("GET", "http://exmaple.com", nil)
32+
req.Header.Add(sentry.SentryTraceHeader, hub.GetTraceparent())
33+
req.Header.Add(sentry.SentryBaggageHeader, hub.GetBaggage())
34+
```
35+
36+
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.
37+
38+
The two services are now connected with your custom distributed tracing implementation.
39+
40+
## Verification
41+
42+
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.

0 commit comments

Comments
 (0)