|
| 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.sofarpc; |
| 20 | + |
| 21 | +import com.alipay.sofa.rpc.client.ProviderInfo; |
| 22 | +import com.alipay.sofa.rpc.context.RpcInternalContext; |
| 23 | +import com.alipay.sofa.rpc.core.request.SofaRequest; |
| 24 | +import com.alipay.sofa.rpc.core.response.SofaResponse; |
| 25 | +import org.apache.skywalking.apm.agent.core.context.CarrierItem; |
| 26 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 27 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 28 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 29 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 30 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 31 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 32 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 33 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 34 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 35 | + |
| 36 | +import java.lang.reflect.Method; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author leizhiyuan |
| 40 | + */ |
| 41 | +public class SofaRpcConsumerInterceptor implements InstanceMethodsAroundInterceptor { |
| 42 | + |
| 43 | + public static final String SKYWALKING_PREFIX = "skywalking."; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 47 | + Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { |
| 48 | + SofaRequest sofaRequest = (SofaRequest) allArguments[0]; |
| 49 | + RpcInternalContext rpcContext = RpcInternalContext.getContext(); |
| 50 | + |
| 51 | + ProviderInfo providerInfo = rpcContext.getProviderInfo(); |
| 52 | + |
| 53 | + AbstractSpan span = null; |
| 54 | + |
| 55 | + final String host = providerInfo.getHost(); |
| 56 | + final int port = providerInfo.getPort(); |
| 57 | + final ContextCarrier contextCarrier = new ContextCarrier(); |
| 58 | + final String operationName = generateOperationName(providerInfo, sofaRequest); |
| 59 | + span = ContextManager.createExitSpan(operationName, contextCarrier, host + ":" + port); |
| 60 | + CarrierItem next = contextCarrier.items(); |
| 61 | + while (next.hasNext()) { |
| 62 | + next = next.next(); |
| 63 | + String key = next.getHeadKey(); |
| 64 | + String skyWalkingKey = SKYWALKING_PREFIX + key; |
| 65 | + sofaRequest.addRequestProp(skyWalkingKey, next.getHeadValue()); |
| 66 | + } |
| 67 | + |
| 68 | + Tags.URL.set(span, generateRequestURL(providerInfo, sofaRequest)); |
| 69 | + span.setComponent(ComponentsDefine.SOFARPC); |
| 70 | + SpanLayer.asRPCFramework(span); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 75 | + Class<?>[] argumentsTypes, Object ret) throws Throwable { |
| 76 | + SofaResponse result = (SofaResponse) ret; |
| 77 | + if (result != null && result.isError()) { |
| 78 | + dealException((Throwable) result.getAppResponse()); |
| 79 | + } |
| 80 | + |
| 81 | + ContextManager.stopSpan(); |
| 82 | + return ret; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 87 | + Class<?>[] argumentsTypes, Throwable t) { |
| 88 | + dealException(t); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Log the throwable, which occurs in Dubbo RPC service. |
| 93 | + */ |
| 94 | + private void dealException(Throwable throwable) { |
| 95 | + AbstractSpan span = ContextManager.activeSpan(); |
| 96 | + span.errorOccurred(); |
| 97 | + span.log(throwable); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Format operation name. e.g. org.apache.skywalking.apm.plugin.test.Test.test(String) |
| 102 | + * |
| 103 | + * @return operation name. |
| 104 | + */ |
| 105 | + private String generateOperationName(ProviderInfo providerInfo, SofaRequest sofaRequest) { |
| 106 | + StringBuilder operationName = new StringBuilder(); |
| 107 | + operationName.append(sofaRequest.getInterfaceName()); |
| 108 | + operationName.append("." + sofaRequest.getMethodName() + "("); |
| 109 | + for (String arg : sofaRequest.getMethodArgSigs()) { |
| 110 | + operationName.append(arg + ","); |
| 111 | + } |
| 112 | + |
| 113 | + if (sofaRequest.getMethodArgs().length > 0) { |
| 114 | + operationName.delete(operationName.length() - 1, operationName.length()); |
| 115 | + } |
| 116 | + |
| 117 | + operationName.append(")"); |
| 118 | + |
| 119 | + return operationName.toString(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Format request url. |
| 124 | + * e.g. bolt://127.0.0.1:20880/org.apache.skywalking.apm.plugin.test.Test.test(String). |
| 125 | + * |
| 126 | + * @return request url. |
| 127 | + */ |
| 128 | + private String generateRequestURL(ProviderInfo providerInfo, SofaRequest sofaRequest) { |
| 129 | + StringBuilder requestURL = new StringBuilder(); |
| 130 | + requestURL.append(providerInfo.getProtocolType() + "://"); |
| 131 | + requestURL.append(providerInfo.getHost()); |
| 132 | + requestURL.append(":" + providerInfo.getPort() + "/"); |
| 133 | + requestURL.append(generateOperationName(providerInfo, sofaRequest)); |
| 134 | + return requestURL.toString(); |
| 135 | + } |
| 136 | +} |
0 commit comments