|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 11 | + |
| 12 | +import io.opentelemetry.instrumentation.api.util.VirtualField; |
| 13 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 14 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 15 | +import net.bytebuddy.asm.Advice; |
| 16 | +import net.bytebuddy.description.type.TypeDescription; |
| 17 | +import net.bytebuddy.matcher.ElementMatcher; |
| 18 | +import org.apache.pekko.http.scaladsl.model.Uri; |
| 19 | +import org.apache.pekko.http.scaladsl.server.PathMatcher; |
| 20 | +import org.apache.pekko.http.scaladsl.server.PathMatchers; |
| 21 | +import org.apache.pekko.http.scaladsl.server.PathMatchers$; |
| 22 | + |
| 23 | +public class PathMatcherStaticInstrumentation implements TypeInstrumentation { |
| 24 | + @Override |
| 25 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 26 | + return extendsClass(named("org.apache.pekko.http.scaladsl.server.PathMatcher")); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void transform(TypeTransformer transformer) { |
| 31 | + transformer.applyAdviceToMethod( |
| 32 | + named("apply") |
| 33 | + .and(takesArgument(0, named("org.apache.pekko.http.scaladsl.model.Uri$Path"))), |
| 34 | + this.getClass().getName() + "$ApplyAdvice"); |
| 35 | + } |
| 36 | + |
| 37 | + @SuppressWarnings("unused") |
| 38 | + public static class ApplyAdvice { |
| 39 | + |
| 40 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 41 | + public static void onExit( |
| 42 | + @Advice.This PathMatcher<?> pathMatcher, |
| 43 | + @Advice.Argument(0) Uri.Path path, |
| 44 | + @Advice.Return PathMatcher.Matching<?> result) { |
| 45 | + // result is either matched or unmatched, we only care about the matches |
| 46 | + if (result.getClass() == PathMatcher.Matched.class) { |
| 47 | + if (PathMatchers$.PathEnd$.class == pathMatcher.getClass()) { |
| 48 | + PekkoRouteHolder.endMatched(); |
| 49 | + return; |
| 50 | + } |
| 51 | + // if present use the matched path that was remembered in PathMatcherInstrumentation, |
| 52 | + // otherwise just use a * |
| 53 | + String prefix = VirtualField.find(PathMatcher.class, String.class).get(pathMatcher); |
| 54 | + if (prefix == null) { |
| 55 | + if (PathMatchers.Slash$.class == pathMatcher.getClass()) { |
| 56 | + prefix = "/"; |
| 57 | + } else { |
| 58 | + prefix = "*"; |
| 59 | + } |
| 60 | + } |
| 61 | + if (prefix != null) { |
| 62 | + PekkoRouteHolder.push(prefix); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments