Custom Instrumentation
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, or you have our performance monitoring feature turned on.
To set it up manually, all you have to do is to make sure your application extracts incoming
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.
Step 1) Extract Incoming Tracing Information
Incoming
continueTrace()
function to help you with this.Here's an example of how to extract and store incoming tracing information using continueTrace()
:
import io.sentry.BaggageHeader;
import io.sentry.ITransaction;
import io.sentry.Sentry;
import io.sentry.SentryTraceHeader;
import io.sentry.TransactionContext;
final String sentryTraceHeader = httpRequest.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER);
final List<String> baggageHeader = Collections.list(httpRequest.getHeaders(BaggageHeader.BAGGAGE_HEADER));
final TransactionContext transactionContext = Sentry.continueTrace(sentryTraceHeader, baggageHeader);
if (transactionContext != null) {
final ITransaction transaction = Sentry.startTransaction(transactionContext);
}
In this example, getHeader()
returns a single String
or null
and getHeaders()
returns an enumeration of Strings
which may be empty.
If you pass these headers to Sentry's continueTrace()
function it will store them in memory for later use.
Step 2) Inject Tracing Information to Outgoing Requests
For distributed
sentry-trace
and baggage
, must now also be added to outgoing requests.If you are sending outgoing HTTP requests with OkHttp, Apollo or Apollo 3 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.
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:
import io.sentry.BaggageHeader;
import io.sentry.Sentry;
import io.sentry.SentryTraceHeader;
final SentryTraceHeader traceparent = Sentry.getTraceparent();
if (traceparent != null) {
httpHeaders.add(traceparent.getName(), traceparent.getValue());
}
final BaggageHeader baggage = Sentry.getBaggage();
if (baggage != null) {
httpHeaders.add(baggage.getName(), baggage.getValue());
}
performRequest("https://example.com", httpHeaders);
In this example, tracing information is propagated to the
https://example.com
. If this project uses the Sentry Java SDK, it will extract and save the tracing information for later use.The two services are now connected with your custom distributed tracing implementation.
Verification
If you make outgoing requests from your
sentry-trace
and baggage
are present in the request. If so, distributed Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- maven:io.sentry:sentry-android
- Version:
- 6.32.0
- Repository:
- https://github.com/getsentry/sentry-java