EnqueueInterceptor.java 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

L
liqiangz 已提交
19
package org.apache.skywalking.apm.plugin.okhttp.common;
20 21 22 23 24 25 26 27

import okhttp3.Request;
import org.apache.skywalking.apm.agent.core.context.ContextManager;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;

L
liqiangz 已提交
28 29
import java.lang.reflect.Method;

A
ascrutae 已提交
30 31 32 33 34
/**
 * {@link EnqueueInterceptor} create a local span and the prefix of the span operation name is start with `Async` when
 * the `enqueue` method called and also put the `ContextSnapshot` and `RealCall` instance into the
 * `SkyWalkingDynamicField`.
 */
35 36 37 38
public class EnqueueInterceptor implements InstanceMethodsAroundInterceptor, InstanceConstructorInterceptor {
    @Override
    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
        MethodInterceptResult result) throws Throwable {
39 40
        EnhancedInstance callbackInstance = (EnhancedInstance) allArguments[0];
        Request request = (Request) objInst.getSkyWalkingDynamicField();
41
        ContextManager.createLocalSpan("Async" + request.url().uri().getPath());
42

43
        /**
A
ascrutae 已提交
44
         * Here is the process about how to trace the async function.
45 46 47
         *
         * 1. Storage `Request` object into `RealCall` instance when the constructor of `RealCall` called.
         * 2. Put the `RealCall` instance to `CallBack` instance
A
ascrutae 已提交
48 49 50
         * 3. Get the `RealCall` instance from `CallBack` and then Put the `RealCall` into `AsyncCall` instance
         *    since the constructor of `RealCall` called.
         * 5. Create the exit span by using the `RealCall` instance when `AsyncCall` method called.
51
         */
52

53
        callbackInstance.setSkyWalkingDynamicField(new EnhanceRequiredInfo(objInst, ContextManager.capture()));
54 55 56 57 58 59 60 61 62
    }

    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
        Object ret) throws Throwable {
        ContextManager.stopSpan();
        return ret;
    }

63 64
    @Override
    public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
65
        Class<?>[] argumentsTypes, Throwable t) {
E
Evan 已提交
66
        ContextManager.activeSpan().log(t);
67 68
    }

69 70
    @Override
    public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
71 72 73
        objInst.setSkyWalkingDynamicField(allArguments[1]);
    }
}