|
| 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.ContextManager; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; |
| 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.plugin.interceptor.enhance.EnhancedInstance; |
| 26 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 27 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 28 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 29 | + |
| 30 | +import java.lang.reflect.Method; |
| 31 | + |
| 32 | +public class ThreadPerTaskExecutorRunInterceptor implements InstanceMethodsAroundInterceptor { |
| 33 | + |
| 34 | + @Override |
| 35 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { |
| 36 | + Object skyWalkingDynamicField = objInst.getSkyWalkingDynamicField(); |
| 37 | + if (skyWalkingDynamicField == null) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (!(skyWalkingDynamicField instanceof ContextSnapshot)) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + AbstractSpan span = ContextManager.createLocalSpan(getOperationName(objInst, method)); |
| 46 | + span.setComponent(ComponentsDefine.THREAD_PER_TASK_EXECUTOR); |
| 47 | + setCarrierThread(span); |
| 48 | + |
| 49 | + ContextSnapshot contextSnapshot = (ContextSnapshot) skyWalkingDynamicField; |
| 50 | + ContextManager.continued(contextSnapshot); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { |
| 55 | + if (ContextManager.isActive()) { |
| 56 | + ContextManager.stopSpan(); |
| 57 | + } |
| 58 | + return ret; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { |
| 63 | + if (ContextManager.isActive()) { |
| 64 | + ContextManager.activeSpan().log(t); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private String getOperationName(EnhancedInstance objInst, Method method) { |
| 69 | + return objInst.getClass().getName() + "." + method.getName(); |
| 70 | + } |
| 71 | + |
| 72 | + private void setCarrierThread(AbstractSpan span) { |
| 73 | + String threadInfo = Thread.currentThread().toString(); |
| 74 | + if (threadInfo.startsWith("VirtualThread")) { |
| 75 | + String[] parts = threadInfo.split("@"); |
| 76 | + if (parts.length >= 1) { |
| 77 | + Tags.THREAD_CARRIER.set(span, parts[1]); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments