|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.activejhttp; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
| 9 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; |
| 10 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType; |
| 11 | +import static io.opentelemetry.javaagent.instrumentation.activejhttp.ActivejHttpServerConnectionSingletons.instrumenter; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.isInterface; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 14 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 15 | +import static net.bytebuddy.matcher.ElementMatchers.not; |
| 16 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 17 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 18 | + |
| 19 | +import io.activej.http.AsyncServlet; |
| 20 | +import io.activej.http.HttpRequest; |
| 21 | +import io.activej.http.HttpResponse; |
| 22 | +import io.activej.promise.Promise; |
| 23 | +import io.opentelemetry.context.Context; |
| 24 | +import io.opentelemetry.context.Scope; |
| 25 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 26 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 27 | +import net.bytebuddy.asm.Advice; |
| 28 | +import net.bytebuddy.description.type.TypeDescription; |
| 29 | +import net.bytebuddy.matcher.ElementMatcher; |
| 30 | + |
| 31 | +public class ActivejHttpServerConnectionInstrumentation implements TypeInstrumentation { |
| 32 | + |
| 33 | + @Override |
| 34 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 35 | + return hasSuperType(named("io.activej.http.AsyncServlet")).and(not(isInterface())); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public ElementMatcher<ClassLoader> classLoaderOptimization() { |
| 40 | + return hasClassesNamed("io.activej.http.AsyncServlet"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void transform(TypeTransformer transformer) { |
| 45 | + transformer.applyAdviceToMethod( |
| 46 | + isMethod() |
| 47 | + .and(named("serve")) |
| 48 | + .and(takesArguments(1).and(takesArgument(0, named("io.activej.http.HttpRequest")))), |
| 49 | + this.getClass().getName() + "$ServeAdvice"); |
| 50 | + } |
| 51 | + |
| 52 | + @SuppressWarnings("unused") |
| 53 | + public static class ServeAdvice { |
| 54 | + |
| 55 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 56 | + public static void methodEnter( |
| 57 | + @Advice.This AsyncServlet asyncServlet, |
| 58 | + @Advice.Argument(0) HttpRequest request, |
| 59 | + @Advice.Local("otelContext") Context context, |
| 60 | + @Advice.Local("otelScope") Scope scope, |
| 61 | + @Advice.Local("httpRequest") HttpRequest httpRequest) { |
| 62 | + Context parentContext = currentContext(); |
| 63 | + httpRequest = request; |
| 64 | + if (!instrumenter().shouldStart(parentContext, request)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + context = instrumenter().start(parentContext, request); |
| 68 | + scope = context.makeCurrent(); |
| 69 | + } |
| 70 | + |
| 71 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 72 | + public static void methodExit( |
| 73 | + @Advice.This AsyncServlet asyncServlet, |
| 74 | + @Advice.Return(readOnly = false) Promise<HttpResponse> responsePromise, |
| 75 | + @Advice.Thrown Throwable throwable, |
| 76 | + @Advice.Local("otelContext") Context context, |
| 77 | + @Advice.Local("otelScope") Scope scope, |
| 78 | + @Advice.Local("httpRequest") HttpRequest httpRequest) { |
| 79 | + if (scope == null) { |
| 80 | + return; |
| 81 | + } |
| 82 | + scope.close(); |
| 83 | + if (throwable != null) { |
| 84 | + instrumenter().end(context, httpRequest, null, throwable); |
| 85 | + } else { |
| 86 | + responsePromise = PromiseWrapper.wrap(responsePromise, httpRequest, context); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments