|
| 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 | +package org.apache.skywalking.apm.plugin.armeria; |
| 19 | + |
| 20 | +import com.linecorp.armeria.client.Clients; |
| 21 | +import com.linecorp.armeria.common.HttpMethod; |
| 22 | +import com.linecorp.armeria.common.HttpRequest; |
| 23 | +import com.linecorp.armeria.common.util.SafeCloseable; |
| 24 | +import io.netty.util.AsciiString; |
| 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 | +import java.net.URI; |
| 38 | + |
| 39 | +public abstract class AbstractArmeriaClientInterceptor implements InstanceMethodsAroundInterceptor { |
| 40 | + private static final String KEY_SAFE_CLOSEABLE = "SAFE_CLOSEABLE"; |
| 41 | + |
| 42 | + protected abstract URI getUri(EnhancedInstance objInst); |
| 43 | + |
| 44 | + protected abstract HttpMethod getHttpMethod(Object[] allArguments); |
| 45 | + |
| 46 | + protected abstract String getPath(Object[] allArguments); |
| 47 | + |
| 48 | + protected abstract HttpRequest getHttpRequest(Object[] allArguments); |
| 49 | + |
| 50 | + @Override |
| 51 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) { |
| 52 | + URI uri = getUri(objInst); |
| 53 | + HttpMethod httpMethod = getHttpMethod(allArguments); |
| 54 | + String path = getPath(allArguments); |
| 55 | + |
| 56 | + final ContextCarrier contextCarrier = new ContextCarrier(); |
| 57 | + final String remotePeer = uri.getPort() > 0 ? uri.getHost() + ":" + uri.getPort() : uri.getHost(); |
| 58 | + |
| 59 | + final AbstractSpan exitSpan = ContextManager.createExitSpan(path, contextCarrier, remotePeer); |
| 60 | + |
| 61 | + exitSpan.setComponent(ComponentsDefine.ARMERIA); |
| 62 | + exitSpan.setLayer(SpanLayer.HTTP); |
| 63 | + Tags.HTTP.METHOD.set(exitSpan, httpMethod.name()); |
| 64 | + |
| 65 | + ContextManager.getRuntimeContext().put(KEY_SAFE_CLOSEABLE, Clients.withHeaders(builder -> { |
| 66 | + for (CarrierItem item = contextCarrier.items(); item.hasNext(); ) { |
| 67 | + item = item.next(); |
| 68 | + builder.add(AsciiString.of(item.getHeadKey()), item.getHeadValue()); |
| 69 | + } |
| 70 | + })); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Object afterMethod(final EnhancedInstance objInst, final Method method, final Object[] allArguments, |
| 75 | + final Class<?>[] argumentsTypes, final Object ret) { |
| 76 | + HttpRequest req = getHttpRequest(allArguments); |
| 77 | + if (req != null && ContextManager.isActive()) { |
| 78 | + ContextManager.stopSpan(); |
| 79 | + } |
| 80 | + |
| 81 | + SafeCloseable safeCloseable = ContextManager.getRuntimeContext().get(KEY_SAFE_CLOSEABLE, SafeCloseable.class); |
| 82 | + if (safeCloseable != null) { |
| 83 | + safeCloseable.close(); |
| 84 | + } |
| 85 | + return ret; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void handleMethodException(final EnhancedInstance objInst, final Method method, final Object[] allArguments, |
| 90 | + final Class<?>[] argumentsTypes, final Throwable t) { |
| 91 | + if (ContextManager.isActive()) { |
| 92 | + ContextManager.activeSpan().log(t); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments