|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.opentelemetry.instrumentation.auto.httpclient; |
| 18 | + |
| 19 | +import static io.opentelemetry.instrumentation.auto.httpclient.JdkHttpClientTracer.TRACER; |
| 20 | +import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed; |
| 21 | +import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.extendsClass; |
| 22 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 23 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 24 | +import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; |
| 25 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 26 | +import static net.bytebuddy.matcher.ElementMatchers.not; |
| 27 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 28 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 29 | + |
| 30 | +import com.google.auto.service.AutoService; |
| 31 | +import io.opentelemetry.context.Scope; |
| 32 | +import io.opentelemetry.instrumentation.auto.api.CallDepthThreadLocalMap.Depth; |
| 33 | +import io.opentelemetry.javaagent.tooling.Instrumenter; |
| 34 | +import io.opentelemetry.trace.Span; |
| 35 | +import java.net.http.HttpRequest; |
| 36 | +import java.net.http.HttpResponse; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.concurrent.CompletableFuture; |
| 40 | +import net.bytebuddy.asm.Advice; |
| 41 | +import net.bytebuddy.description.method.MethodDescription; |
| 42 | +import net.bytebuddy.description.type.TypeDescription; |
| 43 | +import net.bytebuddy.matcher.ElementMatcher; |
| 44 | + |
| 45 | +@AutoService(Instrumenter.class) |
| 46 | +public class HttpClientInstrumentation extends Instrumenter.Default { |
| 47 | + |
| 48 | + public HttpClientInstrumentation() { |
| 49 | + super("httpclient"); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public ElementMatcher<ClassLoader> classLoaderMatcher() { |
| 54 | + // Optimization for expensive typeMatcher. |
| 55 | + return hasClassesNamed("java.net.http.HttpClient"); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 60 | + return nameStartsWith("java.net.") |
| 61 | + .or(nameStartsWith("jdk.internal.")) |
| 62 | + .and(not(named("jdk.internal.net.http.HttpClientFacade"))) |
| 63 | + .and(extendsClass(named("java.net.http.HttpClient"))); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String[] helperClassNames() { |
| 68 | + return new String[] { |
| 69 | + packageName + ".HttpHeadersInjectAdapter", |
| 70 | + packageName + ".JdkHttpClientTracer", |
| 71 | + packageName + ".ResponseConsumer" |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() { |
| 77 | + Map<ElementMatcher<? super MethodDescription>, String> transformers = new HashMap<>(); |
| 78 | + |
| 79 | + transformers.put( |
| 80 | + isMethod() |
| 81 | + .and(named("send")) |
| 82 | + .and(isPublic()) |
| 83 | + .and(takesArguments(2)) |
| 84 | + .and(takesArgument(0, named("java.net.http.HttpRequest"))), |
| 85 | + HttpClientInstrumentation.class.getName() + "$SendAdvice"); |
| 86 | + |
| 87 | + transformers.put( |
| 88 | + isMethod() |
| 89 | + .and(named("sendAsync")) |
| 90 | + .and(isPublic()) |
| 91 | + .and(takesArgument(0, named("java.net.http.HttpRequest"))), |
| 92 | + HttpClientInstrumentation.class.getName() + "$SendAsyncAdvice"); |
| 93 | + |
| 94 | + return transformers; |
| 95 | + } |
| 96 | + |
| 97 | + public static class SendAdvice { |
| 98 | + |
| 99 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 100 | + public static void methodEnter( |
| 101 | + @Advice.Argument(value = 0) HttpRequest httpRequest, |
| 102 | + @Advice.Local("otelSpan") Span span, |
| 103 | + @Advice.Local("otelScope") Scope scope, |
| 104 | + @Advice.Local("otelCallDepth") Depth callDepth) { |
| 105 | + |
| 106 | + callDepth = TRACER.getCallDepth(); |
| 107 | + if (callDepth.getAndIncrement() == 0) { |
| 108 | + span = TRACER.startSpan(httpRequest); |
| 109 | + if (span.getContext().isValid()) { |
| 110 | + scope = TRACER.startScope(span, httpRequest); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 116 | + public static void methodExit( |
| 117 | + @Advice.Return HttpResponse result, |
| 118 | + @Advice.Thrown Throwable throwable, |
| 119 | + @Advice.Local("otelSpan") Span span, |
| 120 | + @Advice.Local("otelScope") Scope scope, |
| 121 | + @Advice.Local("otelCallDepth") Depth callDepth) { |
| 122 | + |
| 123 | + if (callDepth.decrementAndGet() == 0 && scope != null) { |
| 124 | + scope.close(); |
| 125 | + if (throwable == null) { |
| 126 | + TRACER.end(span, result); |
| 127 | + } else { |
| 128 | + TRACER.endExceptionally(span, result, throwable); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public static class SendAsyncAdvice { |
| 135 | + |
| 136 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 137 | + public static void methodEnter( |
| 138 | + @Advice.Argument(value = 0) HttpRequest httpRequest, |
| 139 | + @Advice.Local("otelSpan") Span span, |
| 140 | + @Advice.Local("otelScope") Scope scope, |
| 141 | + @Advice.Local("otelCallDepth") Depth callDepth) { |
| 142 | + |
| 143 | + callDepth = TRACER.getCallDepth(); |
| 144 | + if (callDepth.getAndIncrement() == 0) { |
| 145 | + span = TRACER.startSpan(httpRequest); |
| 146 | + if (span.getContext().isValid()) { |
| 147 | + scope = TRACER.startScope(span, httpRequest); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 153 | + public static void methodExit( |
| 154 | + @Advice.Return(readOnly = false) CompletableFuture<HttpResponse<?>> future, |
| 155 | + @Advice.Thrown Throwable throwable, |
| 156 | + @Advice.Local("otelSpan") Span span, |
| 157 | + @Advice.Local("otelScope") Scope scope, |
| 158 | + @Advice.Local("otelCallDepth") Depth callDepth) { |
| 159 | + |
| 160 | + if (callDepth.decrementAndGet() == 0 && scope != null) { |
| 161 | + scope.close(); |
| 162 | + if (throwable != null) { |
| 163 | + TRACER.endExceptionally(span, null, throwable); |
| 164 | + } else { |
| 165 | + future = future.whenComplete(new ResponseConsumer(span)); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments