Skywalking-02:如何寫一個Skywalking trace外掛

switchvov發表於2021-07-31

如何寫一個Skywalking trace外掛

javaagent 原理

美團技術團隊-Java 動態除錯技術原理及實踐

類圖

file

實現

ConsumeMessageConcurrentlyInstrumentation

public class ConsumeMessageConcurrentlyInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
    // 需要增強的類
    private static final String ENHANCE_CLASS = "com.aliyun.openservices.shade.com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently";
    // 需要增強的方法
    private static final String CONSUMER_MESSAGE_METHOD = "consumeMessage";
    // 增加的方法對應的攔截器
    private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.ons.v1.MessageConcurrentlyConsumeInterceptor";

    // 構造器不需要攔截
    @Override
    public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
        return new ConstructorInterceptPoint[0];
    }

    @Override
    public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[] {
            // 新增一個攔截器
            new InstanceMethodsInterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    // 方法匹配
                    return named(CONSUMER_MESSAGE_METHOD);
                }

                @Override
                public String getMethodsInterceptor() {
                    return INTERCEPTOR_CLASS;
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            }
        };
    }

    @Override
    protected ClassMatch enhanceClass() {
        // 需要增強的類
        return HierarchyMatch.byHierarchyMatch(new String[] {ENHANCE_CLASS});
    }
}

AbstractMessageConsumeInterceptor

public abstract class AbstractMessageConsumeInterceptor implements InstanceMethodsAroundInterceptor {

    public static final String CONSUMER_OPERATION_NAME_PREFIX = "OnsRocketMQ/";

    // 在方法前增強
    @Override
    public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments,
        Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
        // 拿到方法引數,轉換成訊息列表
        List<MessageExt> msgs = (List<MessageExt>) allArguments[0];

        // 從訊息中中獲取TraceId等Context資訊
        ContextCarrier contextCarrier = getContextCarrierFromMessage(msgs.get(0));
        
        // 建立一個entry span
        AbstractSpan span = ContextManager.createEntrySpan(CONSUMER_OPERATION_NAME_PREFIX + msgs.get(0)
                                                                                                .getTopic() + "/Consumer", contextCarrier);
        span.setComponent(ComponentsDefine.ROCKET_MQ_CONSUMER);
        SpanLayer.asMQ(span);
        for (int i = 1; i < msgs.size(); i++) {
            ContextManager.extract(getContextCarrierFromMessage(msgs.get(i)));
        }

    }

    // 異常處理
    @Override
    public final void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
        Class<?>[] argumentsTypes, Throwable t) {
        ContextManager.activeSpan().log(t);
    }

    private ContextCarrier getContextCarrierFromMessage(MessageExt message) {
        ContextCarrier contextCarrier = new ContextCarrier();

        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            next.setHeadValue(message.getUserProperty(next.getHeadKey()));
        }

        return contextCarrier;
    }
}

MessageConcurrentlyConsumeInterceptor

public class MessageConcurrentlyConsumeInterceptor extends AbstractMessageConsumeInterceptor {
    // 在方法後處理
    @Override
    public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
        Object ret) throws Throwable {
        // 獲取消費狀態
        ConsumeConcurrentlyStatus status = (ConsumeConcurrentlyStatus) ret;
        if (status == ConsumeConcurrentlyStatus.RECONSUME_LATER) {
			// 消費狀態為重試,則設定span出現錯誤
            AbstractSpan activeSpan = ContextManager.activeSpan();
            activeSpan.errorOccurred();
            Tags.MQ_STATUS.set(activeSpan, status.name());
        }
        // 停止span
        ContextManager.stopSpan();
        return ret;
    }
}

專案:apm-ons-1.x-plugin

參考文件

  1. apm-ons-1.x-plugin
  2. 美團技術團隊-Java 動態除錯技術原理及實踐

分享並記錄所學所見

相關文章