-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rocketmq-client-java-5.x plugin (#598)
- Loading branch information
Showing
25 changed files
with
1,168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
apm-sniffer/apm-sdk-plugin/rocketMQ-client-java-5.x-plugin/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ 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. | ||
~ | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>apm-sdk-plugin</artifactId> | ||
<groupId>org.apache.skywalking</groupId> | ||
<version>9.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>apm-rocketmq-client-java-5.x-plugin</artifactId> | ||
<name>rocketMQ-client-java-5.x-plugin</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<rocketmq-client-java.version>5.0.5</rocketmq-client-java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.rocketmq</groupId> | ||
<artifactId>rocketmq-client-java</artifactId> | ||
<version>${rocketmq-client-java.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
90 changes: 90 additions & 0 deletions
90
.../org/apache/skywalking/apm/plugin/rocketMQ/client/java/v5/MessageListenerInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.rocketMQ.client.java.v5; | ||
|
||
import org.apache.rocketmq.client.apis.consumer.ConsumeResult; | ||
import org.apache.rocketmq.client.apis.message.MessageView; | ||
import org.apache.skywalking.apm.agent.core.context.CarrierItem; | ||
import org.apache.skywalking.apm.agent.core.context.ContextCarrier; | ||
import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; | ||
import org.apache.skywalking.apm.plugin.rocketMQ.client.java.v5.define.ConsumerEnhanceInfos; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public class MessageListenerInterceptor implements InstanceMethodsAroundInterceptor { | ||
|
||
public static final String CONSUMER_OPERATION_NAME_PREFIX = "RocketMQ/"; | ||
|
||
@Override | ||
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { | ||
MessageView messageView = (MessageView) allArguments[0]; | ||
|
||
ContextCarrier contextCarrier = getContextCarrierFromMessage(messageView); | ||
|
||
AbstractSpan span = ContextManager.createEntrySpan(CONSUMER_OPERATION_NAME_PREFIX + messageView.getTopic() | ||
+ "/Consumer", contextCarrier); | ||
Tags.MQ_TOPIC.set(span, messageView.getTopic()); | ||
|
||
Object skyWalkingDynamicField = objInst.getSkyWalkingDynamicField(); | ||
if (skyWalkingDynamicField != null) { | ||
ConsumerEnhanceInfos consumerEnhanceInfos = (ConsumerEnhanceInfos) skyWalkingDynamicField; | ||
Tags.MQ_BROKER.set(span, consumerEnhanceInfos.getNamesrvAddr()); | ||
span.setPeer(consumerEnhanceInfos.getNamesrvAddr()); | ||
} | ||
|
||
span.setComponent(ComponentsDefine.ROCKET_MQ_CONSUMER); | ||
SpanLayer.asMQ(span); | ||
} | ||
|
||
@Override | ||
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { | ||
ConsumeResult status = (ConsumeResult) ret; | ||
if (ConsumeResult.FAILURE.equals(status)) { | ||
AbstractSpan activeSpan = ContextManager.activeSpan(); | ||
activeSpan.errorOccurred(); | ||
Tags.MQ_STATUS.set(activeSpan, status.name()); | ||
} | ||
ContextManager.stopSpan(); | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { | ||
ContextManager.activeSpan().log(t); | ||
} | ||
|
||
private ContextCarrier getContextCarrierFromMessage(MessageView message) { | ||
ContextCarrier contextCarrier = new ContextCarrier(); | ||
|
||
CarrierItem next = contextCarrier.items(); | ||
while (next.hasNext()) { | ||
next = next.next(); | ||
next.setHeadValue(message.getProperties().get(next.getHeadKey())); | ||
} | ||
|
||
return contextCarrier; | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
...java/org/apache/skywalking/apm/plugin/rocketMQ/client/java/v5/MessageSendInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.rocketMQ.client.java.v5; | ||
|
||
import org.apache.rocketmq.client.apis.message.Message; | ||
import org.apache.rocketmq.client.apis.message.MessageBuilder; | ||
import org.apache.rocketmq.client.apis.producer.SendReceipt; | ||
import org.apache.rocketmq.client.apis.producer.Transaction; | ||
import org.apache.rocketmq.client.java.impl.ClientImpl; | ||
import org.apache.rocketmq.client.java.message.MessageBuilderImpl; | ||
import org.apache.skywalking.apm.agent.core.context.CarrierItem; | ||
import org.apache.skywalking.apm.agent.core.context.ContextCarrier; | ||
import org.apache.skywalking.apm.agent.core.context.ContextManager; | ||
import org.apache.skywalking.apm.agent.core.context.tag.Tags; | ||
import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; | ||
import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; | ||
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; | ||
import org.apache.skywalking.apm.agent.core.util.CollectionUtil; | ||
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; | ||
import org.apache.skywalking.apm.util.StringUtil; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* {@link MessageSendInterceptor} create exit span when the method {@link org.apache.rocketmq.client.java.impl.producer.ProducerImpl#send(Message)} | ||
* and {@link org.apache.rocketmq.client.java.impl.producer.ProducerImpl#send(Message, Transaction)} execute. | ||
*/ | ||
public class MessageSendInterceptor implements InstanceMethodsAroundInterceptor { | ||
|
||
public static final String ASYNC_SEND_OPERATION_NAME_PREFIX = "RocketMQ/"; | ||
|
||
@Override | ||
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { | ||
Message message = (Message) allArguments[0]; | ||
ClientImpl producerImpl = (ClientImpl) objInst; | ||
|
||
ContextCarrier contextCarrier = new ContextCarrier(); | ||
String namingServiceAddress = producerImpl.getClientConfiguration().getEndpoints(); | ||
AbstractSpan span = ContextManager.createExitSpan(buildOperationName(message.getTopic()), contextCarrier, namingServiceAddress); | ||
span.setComponent(ComponentsDefine.ROCKET_MQ_PRODUCER); | ||
Tags.MQ_BROKER.set(span, namingServiceAddress); | ||
Tags.MQ_TOPIC.set(span, message.getTopic()); | ||
Collection<String> keys = message.getKeys(); | ||
if (!CollectionUtil.isEmpty(keys)) { | ||
span.tag(Tags.ofKey("mq.message.keys"), keys.stream().collect(Collectors.joining(","))); | ||
} | ||
Optional<String> tag = message.getTag(); | ||
if (tag.isPresent()) { | ||
span.tag(Tags.ofKey("mq.message.tags"), tag.get()); | ||
} | ||
|
||
contextCarrier.extensionInjector().injectSendingTimestamp(); | ||
SpanLayer.asMQ(span); | ||
|
||
Map<String, String> properties = message.getProperties(); | ||
CarrierItem next = contextCarrier.items(); | ||
while (next.hasNext()) { | ||
next = next.next(); | ||
if (!StringUtil.isEmpty(next.getHeadValue())) { | ||
properties.put(next.getHeadKey(), next.getHeadValue()); | ||
} | ||
} | ||
|
||
MessageBuilder messageBuilder = new MessageBuilderImpl(); | ||
messageBuilder.setTopic(message.getTopic()); | ||
if (message.getTag().isPresent()) { | ||
messageBuilder.setTag(message.getTag().get()); | ||
} | ||
messageBuilder.setKeys(message.getKeys().toArray(new String[0])); | ||
if (message.getMessageGroup().isPresent()) { | ||
messageBuilder.setMessageGroup(message.getMessageGroup().get()); | ||
} | ||
|
||
byte[] body = new byte[message.getBody().limit()]; | ||
message.getBody().get(body); | ||
messageBuilder.setBody(body); | ||
if (message.getDeliveryTimestamp().isPresent()) { | ||
messageBuilder.setDeliveryTimestamp(message.getDeliveryTimestamp().get()); | ||
} | ||
properties.entrySet().forEach(item -> messageBuilder.addProperty(item.getKey(), item.getValue())); | ||
allArguments[0] = messageBuilder.build(); | ||
} | ||
|
||
@Override | ||
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { | ||
SendReceipt sendReceipt = (SendReceipt) ret; | ||
if (sendReceipt != null && sendReceipt.getMessageId() != null) { | ||
AbstractSpan activeSpan = ContextManager.activeSpan(); | ||
activeSpan.tag(Tags.ofKey("mq.message.id"), sendReceipt.getMessageId().toString()); | ||
} | ||
ContextManager.stopSpan(); | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { | ||
ContextManager.activeSpan().log(t); | ||
} | ||
|
||
private String buildOperationName(String topicName) { | ||
return ASYNC_SEND_OPERATION_NAME_PREFIX + topicName + "/Producer"; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...org/apache/skywalking/apm/plugin/rocketMQ/client/java/v5/PushConsumerImplInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.rocketMQ.client.java.v5; | ||
|
||
import org.apache.rocketmq.client.apis.ClientConfiguration; | ||
import org.apache.rocketmq.client.apis.consumer.MessageListener; | ||
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.plugin.rocketMQ.client.java.v5.define.ConsumerEnhanceInfos; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* {@link PushConsumerImplInterceptor} create exit span when the method {@link org.apache.rocketmq.client.java.impl.consumer.PushConsumerImpl#PushConsumerImpl(ClientConfiguration, String, Map, MessageListener, int, int, int)} execute. | ||
*/ | ||
public class PushConsumerImplInterceptor implements InstanceConstructorInterceptor { | ||
|
||
@Override | ||
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { | ||
ClientConfiguration clientConfiguration = (ClientConfiguration) allArguments[0]; | ||
String namesrvAddr = clientConfiguration.getEndpoints(); | ||
ConsumerEnhanceInfos consumerEnhanceInfos = new ConsumerEnhanceInfos(namesrvAddr); | ||
|
||
if (allArguments[3] instanceof EnhancedInstance) { | ||
EnhancedInstance enhancedMessageListener = (EnhancedInstance) allArguments[3]; | ||
enhancedMessageListener.setSkyWalkingDynamicField(consumerEnhanceInfos); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...org/apache/skywalking/apm/plugin/rocketMQ/client/java/v5/define/ConsumerEnhanceInfos.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.apm.plugin.rocketMQ.client.java.v5.define; | ||
|
||
public class ConsumerEnhanceInfos { | ||
|
||
private String namesrvAddr; | ||
|
||
public ConsumerEnhanceInfos(String namesrvAddr) { | ||
this.namesrvAddr = namesrvAddr; | ||
} | ||
|
||
public String getNamesrvAddr() { | ||
return namesrvAddr; | ||
} | ||
} |
Oops, something went wrong.