-
Notifications
You must be signed in to change notification settings - Fork 873
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
Set server span name for aborted requests in quarkus resteasy reactive #8891
Merged
laurit
merged 3 commits into
open-telemetry:main
from
laurit:quarkus-resteasy-native-abort
Jul 11, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...rumentation/quarkus/resteasy/reactive/AbstractResteasyReactiveContextInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.quarkus.resteasy.reactive; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext; | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
|
||
public class AbstractResteasyReactiveContextInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("run"), | ||
AbstractResteasyReactiveContextInstrumentation.class.getName() + "$RunAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class RunAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static OtelRequestContext onEnter( | ||
@Advice.This AbstractResteasyReactiveContext<?, ?> requestContext) { | ||
if (requestContext instanceof ResteasyReactiveRequestContext) { | ||
ResteasyReactiveRequestContext context = (ResteasyReactiveRequestContext) requestContext; | ||
return OtelRequestContext.start(context); | ||
} | ||
return null; | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Enter OtelRequestContext context) { | ||
if (context != null) { | ||
context.close(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...opentelemetry/javaagent/instrumentation/quarkus/resteasy/reactive/OtelRequestContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.quarkus.resteasy.reactive; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteSource; | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
|
||
public final class OtelRequestContext { | ||
private static final ThreadLocal<OtelRequestContext> contextThreadLocal = new ThreadLocal<>(); | ||
private boolean firstInvoke = true; | ||
|
||
public static OtelRequestContext start(ResteasyReactiveRequestContext requestContext) { | ||
OtelRequestContext context = new OtelRequestContext(); | ||
contextThreadLocal.set(context); | ||
ResteasyReactiveSpanName.INSTANCE.updateServerSpanName( | ||
requestContext, HttpRouteSource.CONTROLLER); | ||
return context; | ||
} | ||
|
||
public static void onInvoke(ResteasyReactiveRequestContext requestContext) { | ||
OtelRequestContext context = contextThreadLocal.get(); | ||
if (context == null) { | ||
return; | ||
} | ||
// we ignore the first invoke as it uses the same context that we get in start, the second etc. | ||
// invoke will be for sub resource locator that changes the path | ||
if (context.firstInvoke) { | ||
context.firstInvoke = false; | ||
return; | ||
} | ||
ResteasyReactiveSpanName.INSTANCE.updateServerSpanName( | ||
requestContext, HttpRouteSource.NESTED_CONTROLLER); | ||
} | ||
|
||
public void close() { | ||
contextThreadLocal.remove(); | ||
} | ||
|
||
private OtelRequestContext() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...main/java/io/opentelemetry/instrumentation/quarkus/resteasy/reactive/v2_0/TestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.quarkus.resteasy.reactive.v2_0; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
@Provider | ||
public class TestFilter implements ContainerRequestFilter { | ||
|
||
@Override | ||
public void filter(ContainerRequestContext containerRequestContext) { | ||
if (containerRequestContext.getHeaderString("abort") != null) { | ||
containerRequestContext.abortWith( | ||
Response.status(Response.Status.UNAUTHORIZED) | ||
.entity("Aborted") | ||
.type(MediaType.TEXT_PLAIN_TYPE) | ||
.build()); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...main/java/io/opentelemetry/instrumentation/quarkus/resteasy/reactive/v3_0/TestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.quarkus.resteasy.reactive.v3_0; | ||
|
||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.container.ContainerRequestFilter; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
@Provider | ||
public class TestFilter implements ContainerRequestFilter { | ||
|
||
@Override | ||
public void filter(ContainerRequestContext containerRequestContext) { | ||
if (containerRequestContext.getHeaderString("abort") != null) { | ||
containerRequestContext.abortWith( | ||
Response.status(Response.Status.UNAUTHORIZED) | ||
.entity("Aborted") | ||
.type(MediaType.TEXT_PLAIN_TYPE) | ||
.build()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to use OTel
Context
instead of a thread local?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
ThreadLocal
uses slightly less code. I don't have a strong preference, if you want I can rewrite to otel context.