Custom Instrumentation
On this page you will learn how to manually propagate trace information into and out of your Go application.
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.
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:
- In a web environment it will be sent via HTTP headers, for example, by another Sentry SDK used in your frontend project.
- You also can pick up tracing information from environment variables.
Here's an example of how to extract and store incoming tracing information using sentry.ContinueTrace()
:
hub := sentry.CurrentHub()
sentry.ContinueTrace(hub, r.Header.Get(sentry.SentryTraceHeader), r.Header.Get(sentry.SentryBaggageHeader)),
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.
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.
You can generate this tracing information with the Sentry SDK's hub.GetTraceparent()
and hub.GetBaggage()
functions. Here's an example using Guzzle:
hub := sentry.CurrentHub()
req, _ := http.NewRequest("GET", "http://exmaple.com", nil)
req.Header.Add(sentry.SentryTraceHeader, hub.GetTraceparent())
req.Header.Add(sentry.SentryBaggageHeader, hub.GetBaggage())
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.
The two services are now connected with your custom distributed tracing implementation.
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.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").