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

broaden the requestTrace attachment accessor #1297

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1297.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: broaden the requestTrace attachment accessor
links:
- https://github.com/palantir/tracing-java/pull/1297
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,23 @@ public final class TracingAttachments {
*/
static final AttachmentKey<DetachedSpan> REQUEST_SPAN = AttachmentKey.create(DetachedSpan.class);

/**
* The {@link Detached} trace state which represents the top-level request being processed. This may
* be used to apply thread state to code executing outside traced handlers, exchange completion
* listeners, for example.
*/
@SuppressWarnings("unchecked")
public static final AttachmentKey<Detached> REQUEST_DETACHED_TRACE =
(AttachmentKey<Detached>) (AttachmentKey<?>) REQUEST_SPAN;
Comment on lines +46 to +48
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't love exposing this directly since it allows the attachment to be removed, and we don't really expect anyone to do that. The risk is small, and this unblocks applying tracing to some additional libraries without consuming that functionality directly into tracing-undertow. In particular WebSocketHttpExchange wraps an HttpServerExchange and exposes its attachments, but doesn't implement Attachable or expose the underlying exchange directly.


/**
* Gets the {@link Detached} trace state which represents the top-level request being processed. This may
* be used to apply thread state to code executing outside traced handlers, exchange completion
* listeners, for example.
*/
@Nullable
public static Detached requestTrace(HttpServerExchange exchange) {
return exchange.getAttachment(REQUEST_SPAN);
return exchange.getAttachment(REQUEST_DETACHED_TRACE);
}

private TracingAttachments() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ public void whenSamplingDecisionHasNotBeenMade_callsSampler() throws Exception {
public void setsDetachedTrace() throws Exception {
handler.handleRequest(exchange);
assertThat(TracingAttachments.requestTrace(exchange)).isNotNull();
assertThat(exchange.getAttachment(TracingAttachments.REQUEST_DETACHED_TRACE))
.isNotNull();
}

@Test
Expand Down