|
| 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.caffeine.v3; |
| 20 | + |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 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.context.trace.SpanLayer; |
| 26 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 27 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 28 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 29 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 30 | + |
| 31 | +import static org.apache.skywalking.apm.plugin.caffeine.v3.CaffeineOperationTransform.transformOperation; |
| 32 | + |
| 33 | +abstract public class AbstractCaffeineInterceptor implements InstanceMethodsAroundInterceptor { |
| 34 | + |
| 35 | + protected AbstractSpan generateSpanInfo(String methodName) { |
| 36 | + AbstractSpan span = ContextManager.createLocalSpan("Caffeine/" + methodName); |
| 37 | + span.setComponent(ComponentsDefine.CAFFEINE); |
| 38 | + Tags.CACHE_TYPE.set(span, ComponentsDefine.CAFFEINE.getName()); |
| 39 | + Tags.CACHE_CMD.set(span, methodName); |
| 40 | + transformOperation(methodName).ifPresent(op -> Tags.CACHE_OP.set(span, op)); |
| 41 | + SpanLayer.asCache(span); |
| 42 | + return span; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void beforeMethod(final EnhancedInstance objInst, |
| 47 | + final Method method, |
| 48 | + final Object[] allArguments, |
| 49 | + final Class<?>[] argumentsTypes, |
| 50 | + final MethodInterceptResult result) throws Throwable { |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Object afterMethod(final EnhancedInstance objInst, |
| 55 | + final Method method, |
| 56 | + final Object[] allArguments, |
| 57 | + final Class<?>[] argumentsTypes, |
| 58 | + final Object ret) throws Throwable { |
| 59 | + ContextManager.stopSpan(); |
| 60 | + return ret; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void handleMethodException(final EnhancedInstance objInst, |
| 65 | + final Method method, |
| 66 | + final Object[] allArguments, |
| 67 | + final Class<?>[] argumentsTypes, |
| 68 | + final Throwable t) { |
| 69 | + ContextManager.activeSpan().log(t); |
| 70 | + } |
| 71 | +} |
0 commit comments