|
| 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.shenyu.v24x; |
| 20 | + |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +import org.apache.skywalking.apm.agent.core.context.CarrierItem; |
| 27 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 28 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 29 | +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; |
| 30 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 31 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags.HTTP; |
| 32 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 33 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 34 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 35 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 36 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 37 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 38 | +import org.springframework.http.HttpHeaders; |
| 39 | +import org.springframework.web.server.ServerWebExchange; |
| 40 | +import org.springframework.web.server.ServerWebExchangeDecorator; |
| 41 | +import org.springframework.web.server.adapter.DefaultServerWebExchange; |
| 42 | + |
| 43 | +import reactor.core.publisher.Mono; |
| 44 | + |
| 45 | +/** |
| 46 | + * Apache shenyu global-plugin interceptor. |
| 47 | + */ |
| 48 | +public class GlobalPluginExecuteMethodInterceptor implements InstanceMethodsAroundInterceptor { |
| 49 | + |
| 50 | + public static final String SHENYU_AGENT_TRACE_ID = "shenyu-agent-trace-id"; |
| 51 | + public static final String SKYWALKING_CONTEXT_SNAPSHOT = "SKYWALKING_CONTEXT_SNAPSHOT"; |
| 52 | + public static final String SKYWALKING_SPAN = "SKYWALKING_SPAN"; |
| 53 | + |
| 54 | + @Override |
| 55 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 56 | + MethodInterceptResult result) throws Throwable { |
| 57 | + ServerWebExchange exchange = (ServerWebExchange) allArguments[0]; |
| 58 | + |
| 59 | + ContextCarrier carrier = new ContextCarrier(); |
| 60 | + CarrierItem next = carrier.items(); |
| 61 | + HttpHeaders headers = exchange.getRequest().getHeaders(); |
| 62 | + while (next.hasNext()) { |
| 63 | + next = next.next(); |
| 64 | + List<String> header = headers.get(next.getHeadKey()); |
| 65 | + if (header != null && header.size() > 0) { |
| 66 | + next.setHeadValue(header.get(0)); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + AbstractSpan span = ContextManager.createEntrySpan(exchange.getRequest().getURI().getPath(), carrier); |
| 71 | + span.setComponent(ComponentsDefine.APACHE_SHENYU); |
| 72 | + SpanLayer.asHttp(span); |
| 73 | + Tags.URL.set(span, exchange.getRequest().getURI().toString()); |
| 74 | + HTTP.METHOD.set(span, exchange.getRequest().getMethodValue()); |
| 75 | + |
| 76 | + ContextSnapshot snapshot = ContextManager.capture(); |
| 77 | + exchange.getAttributes().put(SHENYU_AGENT_TRACE_ID, snapshot.getTraceId().getId()); |
| 78 | + EnhancedInstance instance = getInstance(allArguments[0]); |
| 79 | + instance.setSkyWalkingDynamicField(snapshot); |
| 80 | + span.prepareForAsync(); |
| 81 | + ContextManager.stopSpan(span); |
| 82 | + |
| 83 | + exchange.getAttributes().put(SKYWALKING_SPAN, span); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 88 | + Object ret) throws Throwable { |
| 89 | + |
| 90 | + ServerWebExchange exchange = (ServerWebExchange) allArguments[0]; |
| 91 | + |
| 92 | + AbstractSpan span = (AbstractSpan) exchange.getAttributes().get(SKYWALKING_SPAN); |
| 93 | + if (Objects.isNull(span)) { |
| 94 | + return ret; |
| 95 | + } |
| 96 | + Mono<Void> monoReturn = (Mono<Void>) ret; |
| 97 | + |
| 98 | + // add skywalking context snapshot to reactor context. webclient plugin need to use SKYWALKING_CONTEXT_SNAPSHOT |
| 99 | + EnhancedInstance instance = getInstance(allArguments[0]); |
| 100 | + if (instance != null && instance.getSkyWalkingDynamicField() != null) { |
| 101 | + monoReturn = monoReturn.subscriberContext( |
| 102 | + c -> c.put(SKYWALKING_CONTEXT_SNAPSHOT, instance.getSkyWalkingDynamicField())); |
| 103 | + } |
| 104 | + |
| 105 | + return monoReturn |
| 106 | + .doOnError(throwable -> span.errorOccurred().log(throwable)) |
| 107 | + .doFinally(s -> { |
| 108 | + try { |
| 109 | + Optional.ofNullable(exchange.getResponse().getStatusCode()).ifPresent(httpStatus -> { |
| 110 | + Tags.HTTP_RESPONSE_STATUS_CODE.set(span, httpStatus.value()); |
| 111 | + if (httpStatus.isError()) { |
| 112 | + span.errorOccurred(); |
| 113 | + } |
| 114 | + }); |
| 115 | + } finally { |
| 116 | + span.asyncFinish(); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 123 | + Class<?>[] argumentsTypes, Throwable t) { |
| 124 | + } |
| 125 | + |
| 126 | + public static EnhancedInstance getInstance(Object o) { |
| 127 | + EnhancedInstance instance = null; |
| 128 | + if (o instanceof DefaultServerWebExchange) { |
| 129 | + instance = (EnhancedInstance) o; |
| 130 | + } else if (o instanceof ServerWebExchangeDecorator) { |
| 131 | + ServerWebExchange delegate = ((ServerWebExchangeDecorator) o).getDelegate(); |
| 132 | + return getInstance(delegate); |
| 133 | + } |
| 134 | + return instance; |
| 135 | + } |
| 136 | +} |
0 commit comments