|
| 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.spring.webflux.v6.webclient; |
| 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.ContextSnapshot; |
| 24 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 25 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 26 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 27 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 28 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.InstanceMethodsAroundInterceptorV2; |
| 29 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.MethodInvocationContext; |
| 30 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 31 | +import org.springframework.http.HttpStatusCode; |
| 32 | +import org.springframework.web.reactive.function.client.ClientRequest; |
| 33 | +import org.springframework.web.reactive.function.client.ClientResponse; |
| 34 | +import reactor.core.publisher.Mono; |
| 35 | + |
| 36 | +import java.lang.reflect.Method; |
| 37 | +import java.net.URI; |
| 38 | +import java.util.Optional; |
| 39 | + |
| 40 | +public class WebFluxWebClientInterceptor implements InstanceMethodsAroundInterceptorV2 { |
| 41 | + |
| 42 | + @Override |
| 43 | + public void beforeMethod(EnhancedInstance objInst, |
| 44 | + Method method, |
| 45 | + Object[] allArguments, |
| 46 | + Class<?>[] argumentsTypes, |
| 47 | + MethodInvocationContext context) throws Throwable { |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Object afterMethod(EnhancedInstance objInst, |
| 53 | + Method method, |
| 54 | + Object[] allArguments, |
| 55 | + Class<?>[] argumentsTypes, |
| 56 | + Object ret, |
| 57 | + MethodInvocationContext context) throws Throwable { |
| 58 | + // fix the problem that allArgument[0] may be null |
| 59 | + if (allArguments[0] == null) { |
| 60 | + return ret; |
| 61 | + } |
| 62 | + Mono<ClientResponse> ret1 = (Mono<ClientResponse>) ret; |
| 63 | + return Mono.deferContextual(ctx -> { |
| 64 | + |
| 65 | + ClientRequest request = (ClientRequest) allArguments[0]; |
| 66 | + URI uri = request.url(); |
| 67 | + final String operationName = getRequestURIString(uri); |
| 68 | + final String remotePeer = getIPAndPort(uri); |
| 69 | + AbstractSpan span = ContextManager.createExitSpan(operationName, remotePeer); |
| 70 | + |
| 71 | + // get ContextSnapshot from reactor context, the snapshot is set to reactor context by any other plugin |
| 72 | + // such as DispatcherHandlerHandleMethodInterceptor in spring-webflux-5.x-plugin |
| 73 | + final Optional<Object> optional = ctx.getOrEmpty("SKYWALKING_CONTEXT_SNAPSHOT"); |
| 74 | + optional.ifPresent(snapshot -> ContextManager.continued((ContextSnapshot) snapshot)); |
| 75 | + |
| 76 | + //set components name |
| 77 | + span.setComponent(ComponentsDefine.SPRING_WEBCLIENT); |
| 78 | + Tags.URL.set(span, uri.toString()); |
| 79 | + Tags.HTTP.METHOD.set(span, request.method().toString()); |
| 80 | + SpanLayer.asHttp(span); |
| 81 | + |
| 82 | + final ContextCarrier contextCarrier = new ContextCarrier(); |
| 83 | + ContextManager.inject(contextCarrier); |
| 84 | + if (request instanceof EnhancedInstance) { |
| 85 | + ((EnhancedInstance) request).setSkyWalkingDynamicField(contextCarrier); |
| 86 | + } |
| 87 | + |
| 88 | + //user async interface |
| 89 | + span.prepareForAsync(); |
| 90 | + ContextManager.stopSpan(); |
| 91 | + return ret1.doOnSuccess(clientResponse -> { |
| 92 | + HttpStatusCode httpStatus = clientResponse.statusCode(); |
| 93 | + if (httpStatus != null) { |
| 94 | + Tags.HTTP_RESPONSE_STATUS_CODE.set(span, httpStatus.value()); |
| 95 | + if (httpStatus.isError()) { |
| 96 | + span.errorOccurred(); |
| 97 | + } |
| 98 | + } |
| 99 | + }).doOnError(span::log).doFinally(s -> { |
| 100 | + span.asyncFinish(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void handleMethodException(EnhancedInstance objInst, |
| 107 | + Method method, |
| 108 | + Object[] allArguments, |
| 109 | + Class<?>[] argumentsTypes, |
| 110 | + Throwable t, |
| 111 | + MethodInvocationContext context) { |
| 112 | + AbstractSpan activeSpan = ContextManager.activeSpan(); |
| 113 | + activeSpan.errorOccurred(); |
| 114 | + activeSpan.log(t); |
| 115 | + } |
| 116 | + |
| 117 | + private String getRequestURIString(URI uri) { |
| 118 | + String requestPath = uri.getPath(); |
| 119 | + return requestPath != null && requestPath.length() > 0 ? requestPath : "/"; |
| 120 | + } |
| 121 | + |
| 122 | + // return ip:port |
| 123 | + private String getIPAndPort(URI uri) { |
| 124 | + return uri.getHost() + ":" + uri.getPort(); |
| 125 | + } |
| 126 | +} |
0 commit comments