|
| 1 | +On this page you will learn how to manually propagate trace information into and out of your Java application. Please note, that you do not need to do this manually, if you [use one of our supported frameworks](/platforms/java/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 `continueTrace()` function to help you with this. Incoming tracing information can come from HTTP headers, for example, by another Sentry SDK used in your frontend project. |
| 8 | + |
| 9 | +Here's an example of how to extract and store incoming tracing information using `continueTrace()`: |
| 10 | + |
| 11 | +```java |
| 12 | +import io.sentry.BaggageHeader; |
| 13 | +import io.sentry.ITransaction; |
| 14 | +import io.sentry.Sentry; |
| 15 | +import io.sentry.SentryTraceHeader; |
| 16 | +import io.sentry.TransactionContext; |
| 17 | + |
| 18 | +final String sentryTraceHeader = httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER); |
| 19 | +final List<String> baggageHeader = Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER)); |
| 20 | + |
| 21 | +final TransactionContext transactionContext = Sentry.continueTrace(sentryTraceHeader, baggageHeader); |
| 22 | +if (transactionContext != null) { |
| 23 | + final ITransaction transaction = Sentry.startTransaction(transactionContext); |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +In this example, `getHeader()` returns a single `String` or `null` and `getHeaders()` returns an enumeration of `Strings` which may be empty. |
| 28 | + |
| 29 | +If you pass these headers to Sentry's `continueTrace()` function it will store them in memory for later use. |
| 30 | + |
| 31 | +## Step 2) Inject Tracing Information to Outgoing Requests |
| 32 | + |
| 33 | +For distributed tracing to work, the two headers `sentry-trace` and `baggage`, must now also be added to outgoing requests. |
| 34 | + |
| 35 | +If you are sending outgoing HTTP requests with [RestTemplate](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html), [WebClient](https://docs.spring.io/spring-framework/reference/web/webflux-webclient.html), Apollo, Apollo 3 or OpenFeign and have our integration for it enabled, this tracing information is automatically added to outgoing requests. You do not have to enable Performance for distributed tracing to work. |
| 36 | + |
| 37 | +If you're using none of the above, you can generate this tracing information with the Sentry SDK's `getTraceparent()` and `getBaggage()` functions. Here's an example: |
| 38 | + |
| 39 | +```java |
| 40 | +import io.sentry.BaggageHeader; |
| 41 | +import io.sentry.Sentry; |
| 42 | +import io.sentry.SentryTraceHeader; |
| 43 | + |
| 44 | +final SentryTraceHeader traceparent = Sentry.getTraceparent(); |
| 45 | +if (traceparent != null) { |
| 46 | + httpHeaders.add(traceparent.getName(), traceparent.getValue()); |
| 47 | +} |
| 48 | +final BaggageHeader baggage = Sentry.getBaggage(); |
| 49 | +if (baggage != null) { |
| 50 | + httpHeaders.add(baggage.getName(), baggage.getValue()); |
| 51 | +} |
| 52 | + |
| 53 | +performRequest("https://example.com", httpHeaders); |
| 54 | +``` |
| 55 | + |
| 56 | +In this example, tracing information is propagated to the project running at `https://example.com`. If this project uses the Sentry Java SDK, it will extract and save the tracing information for later use. |
| 57 | + |
| 58 | +The two services are now connected with your custom distributed tracing implementation. |
| 59 | + |
| 60 | +## Verification |
| 61 | + |
| 62 | +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