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 1 commit
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 is 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>)`. Please 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.
adinauer marked this conversation as resolved.
Show resolved Hide resolved

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, hub.getOptions().getLogger())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to expose a method fromHeader that doesn't take a logger.
Sentry.getCurrentHub() is @ApiStatus.Internal so people cannot use this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or make it nullable, but I'd rather expose a new overload.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about passing in the options instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does not help either, there's no public API to retrieve the options.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm so we're down to calling Sentry.getCurrentHub().getOptions().getLogger() inside then to hide it from the user but still be able to log problems I presume.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, that's what HubAdapter.getInstance() stands for, at least to make it testable when calling internally.
I'd make a PR asap because otherwise, it's not usable, I don't expect people using Baggage.fromHeader themselves any way, so it's not a p0 but create an issue, and don't merge this before doing it, this would just raise more questions around "how do I get the logger?".

)
);
} 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, hub.options.logger)
)
)
} catch (e: InvalidSentryTraceHeaderException) {
// handle invalid trace header
}
Expand Down