|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.apm.plugin; |
| 20 | + |
| 21 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 23 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 24 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 25 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 26 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 27 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 28 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 29 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 30 | + |
| 31 | +import java.lang.reflect.Method; |
| 32 | +import java.net.URI; |
| 33 | +import java.net.http.HttpRequest; |
| 34 | +import java.net.http.HttpResponse; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.CompletableFuture; |
| 38 | + |
| 39 | +public class HttpClientSendAsyncInterceptor implements InstanceMethodsAroundInterceptor { |
| 40 | + |
| 41 | + @Override |
| 42 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { |
| 43 | + HttpRequest request = (HttpRequest) allArguments[0]; |
| 44 | + URI uri = request.uri(); |
| 45 | + |
| 46 | + ContextCarrier contextCarrier = new ContextCarrier(); |
| 47 | + AbstractSpan span = ContextManager.createExitSpan(buildOperationName(request.method(), uri), contextCarrier, getPeer(uri)); |
| 48 | + |
| 49 | + if (request instanceof EnhancedInstance) { |
| 50 | + ((EnhancedInstance) request).setSkyWalkingDynamicField(contextCarrier); |
| 51 | + } |
| 52 | + |
| 53 | + span.setComponent(ComponentsDefine.JDK_HTTP); |
| 54 | + Tags.HTTP.METHOD.set(span, request.method()); |
| 55 | + Tags.URL.set(span, String.valueOf(uri)); |
| 56 | + SpanLayer.asHttp(span); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { |
| 61 | + |
| 62 | + if (ContextManager.isActive()) { |
| 63 | + AbstractSpan span = ContextManager.activeSpan(); |
| 64 | + span.prepareForAsync(); |
| 65 | + |
| 66 | + if (ret != null) { |
| 67 | + CompletableFuture<?> future = (CompletableFuture<?>) ret; |
| 68 | + ret = future.whenComplete((response, throwable) -> { |
| 69 | + try { |
| 70 | + if (throwable != null) { |
| 71 | + span.errorOccurred(); |
| 72 | + span.log(throwable); |
| 73 | + return; |
| 74 | + } |
| 75 | + if (response instanceof HttpResponse) { |
| 76 | + HttpResponse<?> httpResponse = (HttpResponse<?>) response; |
| 77 | + int statusCode = httpResponse.statusCode(); |
| 78 | + Tags.HTTP_RESPONSE_STATUS_CODE.set(span, statusCode); |
| 79 | + if (statusCode >= 400) { |
| 80 | + span.errorOccurred(); |
| 81 | + } |
| 82 | + } |
| 83 | + } finally { |
| 84 | + span.asyncFinish(); |
| 85 | + } |
| 86 | + }); |
| 87 | + } else { |
| 88 | + Map<String, String> eventMap = new HashMap<String, String>(); |
| 89 | + eventMap.put("error", "No response"); |
| 90 | + span.log(System.currentTimeMillis(), eventMap); |
| 91 | + span.errorOccurred(); |
| 92 | + } |
| 93 | + ContextManager.stopSpan(); |
| 94 | + } |
| 95 | + return ret; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { |
| 100 | + if (ContextManager.isActive()) { |
| 101 | + ContextManager.activeSpan().log(t); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private String buildOperationName(String method, URI uri) { |
| 106 | + String path = uri.getPath(); |
| 107 | + if (path == null || path.isEmpty()) { |
| 108 | + path = "/"; |
| 109 | + } |
| 110 | + return method + ":" + path; |
| 111 | + } |
| 112 | + |
| 113 | + private String getPeer(URI uri) { |
| 114 | + String host = uri.getHost(); |
| 115 | + int port = uri.getPort(); |
| 116 | + |
| 117 | + if (port == -1) { |
| 118 | + port = "https".equalsIgnoreCase(uri.getScheme()) ? 443 : 80; |
| 119 | + } |
| 120 | + |
| 121 | + return host + ":" + port; |
| 122 | + } |
| 123 | +} |
0 commit comments