-
Notifications
You must be signed in to change notification settings - Fork 945
Fix vert.x route containing dupicate segments when RoutingContext.next is used #12260
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
46 changes: 46 additions & 0 deletions
46
...javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/RouteHolder.java
This file contains hidden or 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,46 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.vertx; | ||
|
||
import static io.opentelemetry.context.ContextKey.named; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import io.opentelemetry.context.ImplicitContextKeyed; | ||
|
||
public final class RouteHolder implements ImplicitContextKeyed { | ||
private static final ContextKey<RouteHolder> KEY = named("opentelemetry-vertx-route"); | ||
|
||
private String route; | ||
|
||
private RouteHolder(String route) { | ||
this.route = route; | ||
} | ||
|
||
public static Context init(Context context, String route) { | ||
if (context.get(KEY) != null) { | ||
return context; | ||
} | ||
return context.with(new RouteHolder(route)); | ||
} | ||
|
||
public static String get(Context context) { | ||
RouteHolder holder = context.get(KEY); | ||
return holder != null ? holder.route : null; | ||
} | ||
|
||
public static void set(Context context, String route) { | ||
RouteHolder holder = context.get(KEY); | ||
if (holder != null) { | ||
holder.route = route; | ||
} | ||
} | ||
|
||
@Override | ||
public Context storeInContext(Context context) { | ||
return context.with(KEY, this); | ||
} | ||
} |
This file contains hidden or 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
53 changes: 53 additions & 0 deletions
53
.../java/io/opentelemetry/javaagent/instrumentation/vertx/RoutingContextInstrumentation.java
This file contains hidden or 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,53 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.vertx; | ||
|
||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; | ||
|
||
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import io.vertx.ext.web.RoutingContext; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class RoutingContextInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderOptimization() { | ||
return hasClassesNamed("io.vertx.ext.web.RoutingContext"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return implementsInterface(named("io.vertx.ext.web.RoutingContext")); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isPublic().and(named("next")).and(takesNoArguments()), | ||
this.getClass().getName() + "$NextAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class NextAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void next(@Advice.This RoutingContext routingContext) { | ||
// calling next tells router to move to the next matching route | ||
// restore remembered route to remove currently matched route from it | ||
String previousRoute = RoutingContextUtil.getRoute(routingContext); | ||
if (previousRoute != null) { | ||
RouteHolder.set(Java8BytecodeBridge.currentContext(), previousRoute); | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...nt/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/RoutingContextUtil.java
This file contains hidden or 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,24 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.vertx; | ||
|
||
import io.opentelemetry.instrumentation.api.util.VirtualField; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public final class RoutingContextUtil { | ||
private static final VirtualField<RoutingContext, String> routeField = | ||
VirtualField.find(RoutingContext.class, String.class); | ||
|
||
public static void setRoute(RoutingContext routingContext, String route) { | ||
routeField.set(routingContext, route); | ||
} | ||
|
||
public static String getRoute(RoutingContext routingContext) { | ||
return routeField.get(routingContext); | ||
} | ||
|
||
private RoutingContextUtil() {} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
maybe rename to
updateIfExists
?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.
There are similar
set
methods in otherImplicitContextKeyed
implementations. If we are going to rename then we should rename all the similar usages and probably theget
method also. I think theholder != null
check is just defensive coding. It really should not be null unless the instrumented code has changed in an unexpected way, so theIfExists
suffix isn't really necessary in my opinion.