Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to continue a trace using baggage header #5617

Merged
merged 3 commits into from
Oct 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions src/platform-includes/performance/distributed-tracing/java.mdx
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
To obtain a trace header from the span, use `ISpan#toSentryTrace()` method. Then pass it to the downstream service. If the communication happens over HTTP, it is recommended that you set the value to the `sentry-trace` (also available as a constant `SentryTraceHeader.SENTRY_TRACE_HEADER`) HTTP header.
To obtain a trace header from the span, use `ISpan#toSentryTrace()` method. Then pass it to the downstream service. If the communication happens over HTTP, it's recommended that you set the value to the `sentry-trace` (also available as a constant `SentryTraceHeader.SENTRY_TRACE_HEADER`) HTTP header, as well as attaching the `baggage` header (`BaggageHeader.BAGGAGE_HEADER`) which can be retrieved from `ISpan#toBaggageHeader(List<String>)`. You should also pass in any existing `baggage` header entries that are present on the outgoing request so we can handle duplicate Sentry entries and limits for you.

To create a span as a continuation of the trace retrieved from the upstream service, pass the `sentry-trace` header value to the transaction context:
To create a span as a continuation of the trace retrieved from the upstream service, pass the `sentry-trace` and `baggage` header values to the transaction context:

```java
import io.sentry.Baggage;
import io.sentry.BaggageHeader;
import io.sentry.ITransaction;
import io.sentry.Sentry;
import io.sentry.SentryTraceHeader;
import io.sentry.TransactionContext;
import io.sentry.exception.InvalidSentryTraceHeaderException;

String sentryTrace = request.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER);
List<String> baggageHeaders = Collections.list(request.getHeaders(BaggageHeader.BAGGAGE_HEADER);
ITransaction transaction = null;
try {
transaction = Sentry.startTransaction(TransactionContext.fromSentryTrace("name", "op", new SentryTraceHeader(sentryTrace)));
transaction = Sentry.startTransaction(
TransactionContext.fromSentryTrace(
"name",
TransactionNameSource.CUSTOM,
"op",
new SentryTraceHeader(sentryTrace),
Baggage.fromHeader(baggageHeaders)
)
);
} catch (InvalidSentryTraceHeaderException e) {
// handle invalid trace header
}
```

```kotlin
import io.sentry.Sentry
import io.sentry.SentryTraceHeader
import io.sentry.TransactionContext
import io.sentry.exception.InvalidSentryTraceHeaderException
import io.sentry.Baggage;
import io.sentry.BaggageHeader;
import io.sentry.ITransaction;
import io.sentry.Sentry;
import io.sentry.SentryTraceHeader;
import io.sentry.TransactionContext;
import io.sentry.exception.InvalidSentryTraceHeaderException;

val sentryTrace = request.getHeader(SentryTraceHeader.SENTRY_TRACE_HEADER)
val baggageHeaders = request.getHeaders(BaggageHeader.BAGGAGE_HEADER)
val transaction = try {
Sentry.startTransaction(TransactionContext.fromSentryTrace("name", "op", SentryTraceHeader(sentryTrace)))
Sentry.startTransaction(
TransactionContext.fromSentryTrace(
"name",
TransactionNameSource.CUSTOM,
"op",
SentryTraceHeader(sentryTrace),
Baggage.fromHeader(baggageHeaders)
)
)
} catch (e: InvalidSentryTraceHeaderException) {
// handle invalid trace header
}
Expand Down