这篇文章将为大家详细讲解有关SkyWalking自定义插件怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

RabbitMQ插件问题

skywalking官方提供的RabbitMQ插件存在缺陷,其只针对RabbitMQ官方原生Client实现扩展,但我们在项目中一般不直接使用原生Client,而是使用Spring RabitMQ Client,因Spring RabitMQ Consumer中存在跨线程操作,导致跟踪ID断链。

具体分析过程

1.官方插件源码的拦截点是原生Consumer的handleDelivery方法,源码如下:

2.而Spring RabbitMQ消费者的默认实现是BlockingQueueConsumer, handleDelivery核心逻辑是把消息放到内部的BlockingQueue队列,不做真正的消费处理,因此拦截此处无法关联到消费者逻辑,源码如下

@OverridepublicvoidhandleDelivery(StringconsumerTag,Envelopeenvelope,AMQP.BasicPropertiesproperties,byte[]body){...try{if(BlockingQueueConsumer.this.abortStarted>0){if(!BlockingQueueConsumer.this.queue.offer(newDelivery(consumerTag,envelope,properties,body,this.queueName),BlockingQueueConsumer.this.shutdownTimeout,TimeUnit.MILLISECONDS)){ChannelchannelToClose=super.getChannel();RabbitUtils.setPhysicalCloseRequired(channelToClose,true);//Defensive-shouldneverhappenBlockingQueueConsumer.this.queue.clear();if(!this.canceled){RabbitUtils.cancel(channelToClose,consumerTag);}try{channelToClose.close();catch(@SuppressWarnings("unused")TimeoutExceptione){//no-op}}else{BlockingQueueConsumer.this.queue.put(newDelivery(consumerTag,envelope,properties,body,this.queueName));}catch(@SuppressWarnings("unused")InterruptedExceptione){Thread.currentThread().interrupt();catch(Exceptione){BlockingQueueConsumer.logger.warn("Unexpectedexceptionduringdelivery",e);}

3.真正的消费处理在SimpleMessageListenerContainer,SimpleMessageListenerContainer继承Runnable接口,在其run方法中while循环调用mainLoop方法,整体调用链路为

4.SimpleMessageListenerContainer.run() -> SimpleMessageListenerContainer.mainLoop() -> SimpleMessageListenerContainer.receiveAndExecute() -> SimpleMessageListenerContainer.doReceiveAndExecute() -> AbstractMessageListenerContainer.executeListener()。最终在executeListener中执行消费逻辑

protectedvoidexecuteListener(Channelchannel,Objectdata){...try{//执行消费逻辑doExecuteListener(channel,data);if(sample!=null){this.micrometerHolder.success(sample,datainstanceofMessage?((Message)data).getMessageProperties().getConsumerQueue():queuesAsListString());}}catch(RuntimeExceptionex){....}}实现自定义插件

从上面可以分析出,AbstractMessageListenerContainer.executeListener()是最佳的拦截点
实现源码已放到码云仓库:https://gitee.com/eureka-gitee/apm-sniffer-pro/tree/v7.0.0.0/

效果展示

SkyWalking调用链路

logback日志

关于“SkyWalking自定义插件怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。