Skip to content

Commit ca980c3

Browse files
adinauershanamatthewsmarkushi
authored
Tracing without Performance Java (#7313)
Co-authored-by: Shana Matthews <[email protected]> Co-authored-by: Markus Hintersteiner <[email protected]>
1 parent 8e41ca4 commit ca980c3

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
On this page you will learn how to manually propagate trace information into and out of your Android application. Please note, that you do not need to do this manually, if you [use one of our supported frameworks](/platforms/android/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 tracing information and to attach it again when making an outgoing request within your application.
4+
5+
Your Android application is likely going to be the first in a chain of requests which means there probably won't be any tracing information to extract but instead your application starts a new trace and sends out tracing information so other applications in the chain can keep the trace going.
6+
7+
## Step 1) Extract Incoming Tracing Information
8+
9+
Incoming tracing information has to be extracted and stored in memory for later use. Sentry provides the `continueTrace()` function to help you with this.
10+
11+
Here's an example of how to extract and store incoming tracing information using `continueTrace()`:
12+
13+
```java
14+
import io.sentry.BaggageHeader;
15+
import io.sentry.ITransaction;
16+
import io.sentry.Sentry;
17+
import io.sentry.SentryTraceHeader;
18+
import io.sentry.TransactionContext;
19+
20+
final String sentryTraceHeader = httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER);
21+
final List<String> baggageHeader = Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER));
22+
23+
final TransactionContext transactionContext = Sentry.continueTrace(sentryTraceHeader, baggageHeader);
24+
if (transactionContext != null) {
25+
final ITransaction transaction = Sentry.startTransaction(transactionContext);
26+
}
27+
```
28+
29+
In this example, `getHeader()` returns a single `String` or `null` and `getHeaders()` returns an enumeration of `Strings` which may be empty.
30+
31+
If you pass these headers to Sentry's `continueTrace()` function it will store them in memory for later use.
32+
33+
## Step 2) Inject Tracing Information to Outgoing Requests
34+
35+
For distributed tracing to work, the two headers `sentry-trace` and `baggage`, must now also be added to outgoing requests.
36+
37+
If you are sending outgoing HTTP requests with <PlatformLink to="/configuration/integrations/okhttp/">OkHttp</PlatformLink>, <PlatformLink to="/configuration/integrations/apollo/">Apollo</PlatformLink> or <PlatformLink to="/configuration/integrations/apollo3/">Apollo 3</PlatformLink> 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.
38+
39+
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:
40+
41+
```java
42+
import io.sentry.BaggageHeader;
43+
import io.sentry.Sentry;
44+
import io.sentry.SentryTraceHeader;
45+
46+
final SentryTraceHeader traceparent = Sentry.getTraceparent();
47+
if (traceparent != null) {
48+
httpHeaders.add(traceparent.getName(), traceparent.getValue());
49+
}
50+
final BaggageHeader baggage = Sentry.getBaggage();
51+
if (baggage != null) {
52+
httpHeaders.add(baggage.getName(), baggage.getValue());
53+
}
54+
55+
performRequest("https://example.com", httpHeaders);
56+
```
57+
58+
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.
59+
60+
The two services are now connected with your custom distributed tracing implementation.
61+
62+
## Verification
63+
64+
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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.
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 the Android SDK, follow the <PlatformLink to="/usage/distributed-tracing/custom-instrumentation/">Custom Instrumentation</PlatformLink> set up steps.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
If you use the current version of our Java SDK, distributed tracing works automatically with the following frameworks:
2+
3+
- [Spring](/platforms/java/guides/spring/usage/distributed-tracing/#how-to-use-distributed-tracing)
4+
- [Spring Boot](/platforms/java/guides/spring-boot/usage/distributed-tracing/#how-to-use-distributed-tracing)
5+
6+
If you use SDK version `6.24.x` or below, you need to <PlatformLink to="/performance/">Set Up Performance</PlatformLink> monitoring for distributed tracing to work. If you do not want to use performance monitoring, you can instead set up <PlatformLink to="/usage/distributed-tracing/custom-instrumentation/">Custom Instrumentation</PlatformLink> for distributed tracing.
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 Java SDK, distributed tracing works automatically without additional configuration.
2+
3+
If you use SDK version `6.24.x` or below, you need to <PlatformLink to="/performance/">Set Up Performance</PlatformLink> monitoring for distributed tracing to work. If you do not want to use performance monitoring, you can instead set up <PlatformLink to="/usage/distributed-tracing/custom-instrumentation/">Custom Instrumentation</PlatformLink> for distributed tracing.
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 Java SDK, distributed tracing works automatically without additional configuration.
2+
3+
If you use SDK version `6.24.x` or below, you need to <PlatformLink to="/performance/">Set Up Performance</PlatformLink> monitoring for distributed tracing to work. If you do not want to use performance monitoring, you can instead set up <PlatformLink to="/usage/distributed-tracing/custom-instrumentation/">Custom Instrumentation</PlatformLink> for distributed tracing.

src/platforms/common/usage/distributed-tracing/custom-instrumentation.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ description: ""
55
supported:
66
- python
77
- php
8+
- java
9+
- android
810
---
911

1012
<PlatformContent includePath="distributed-tracing/custom-instrumentation/" />

0 commit comments

Comments
 (0)