|
| 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.rocketMQ.client.java.v5; |
| 20 | + |
| 21 | +import org.apache.rocketmq.client.apis.message.Message; |
| 22 | +import org.apache.rocketmq.client.apis.message.MessageBuilder; |
| 23 | +import org.apache.rocketmq.client.apis.producer.SendReceipt; |
| 24 | +import org.apache.rocketmq.client.apis.producer.Transaction; |
| 25 | +import org.apache.rocketmq.client.java.impl.ClientImpl; |
| 26 | +import org.apache.rocketmq.client.java.message.MessageBuilderImpl; |
| 27 | +import org.apache.skywalking.apm.agent.core.context.CarrierItem; |
| 28 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 29 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 30 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 31 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 32 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 33 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 34 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 35 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 36 | +import org.apache.skywalking.apm.agent.core.util.CollectionUtil; |
| 37 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 38 | +import org.apache.skywalking.apm.util.StringUtil; |
| 39 | + |
| 40 | +import java.lang.reflect.Method; |
| 41 | +import java.util.Collection; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.Optional; |
| 44 | +import java.util.stream.Collectors; |
| 45 | + |
| 46 | +/** |
| 47 | + * {@link MessageSendInterceptor} create exit span when the method {@link org.apache.rocketmq.client.java.impl.producer.ProducerImpl#send(Message)} |
| 48 | + * and {@link org.apache.rocketmq.client.java.impl.producer.ProducerImpl#send(Message, Transaction)} execute. |
| 49 | + */ |
| 50 | +public class MessageSendInterceptor implements InstanceMethodsAroundInterceptor { |
| 51 | + |
| 52 | + public static final String ASYNC_SEND_OPERATION_NAME_PREFIX = "RocketMQ/"; |
| 53 | + |
| 54 | + @Override |
| 55 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { |
| 56 | + Message message = (Message) allArguments[0]; |
| 57 | + ClientImpl producerImpl = (ClientImpl) objInst; |
| 58 | + |
| 59 | + ContextCarrier contextCarrier = new ContextCarrier(); |
| 60 | + String namingServiceAddress = producerImpl.getClientConfiguration().getEndpoints(); |
| 61 | + AbstractSpan span = ContextManager.createExitSpan(buildOperationName(message.getTopic()), contextCarrier, namingServiceAddress); |
| 62 | + span.setComponent(ComponentsDefine.ROCKET_MQ_PRODUCER); |
| 63 | + Tags.MQ_BROKER.set(span, namingServiceAddress); |
| 64 | + Tags.MQ_TOPIC.set(span, message.getTopic()); |
| 65 | + Collection<String> keys = message.getKeys(); |
| 66 | + if (!CollectionUtil.isEmpty(keys)) { |
| 67 | + span.tag(Tags.ofKey("mq.message.keys"), keys.stream().collect(Collectors.joining(","))); |
| 68 | + } |
| 69 | + Optional<String> tag = message.getTag(); |
| 70 | + if (tag.isPresent()) { |
| 71 | + span.tag(Tags.ofKey("mq.message.tags"), tag.get()); |
| 72 | + } |
| 73 | + |
| 74 | + contextCarrier.extensionInjector().injectSendingTimestamp(); |
| 75 | + SpanLayer.asMQ(span); |
| 76 | + |
| 77 | + Map<String, String> properties = message.getProperties(); |
| 78 | + CarrierItem next = contextCarrier.items(); |
| 79 | + while (next.hasNext()) { |
| 80 | + next = next.next(); |
| 81 | + if (!StringUtil.isEmpty(next.getHeadValue())) { |
| 82 | + properties.put(next.getHeadKey(), next.getHeadValue()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + MessageBuilder messageBuilder = new MessageBuilderImpl(); |
| 87 | + messageBuilder.setTopic(message.getTopic()); |
| 88 | + if (message.getTag().isPresent()) { |
| 89 | + messageBuilder.setTag(message.getTag().get()); |
| 90 | + } |
| 91 | + messageBuilder.setKeys(message.getKeys().toArray(new String[0])); |
| 92 | + if (message.getMessageGroup().isPresent()) { |
| 93 | + messageBuilder.setMessageGroup(message.getMessageGroup().get()); |
| 94 | + } |
| 95 | + |
| 96 | + byte[] body = new byte[message.getBody().limit()]; |
| 97 | + message.getBody().get(body); |
| 98 | + messageBuilder.setBody(body); |
| 99 | + if (message.getDeliveryTimestamp().isPresent()) { |
| 100 | + messageBuilder.setDeliveryTimestamp(message.getDeliveryTimestamp().get()); |
| 101 | + } |
| 102 | + properties.entrySet().forEach(item -> messageBuilder.addProperty(item.getKey(), item.getValue())); |
| 103 | + allArguments[0] = messageBuilder.build(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { |
| 108 | + SendReceipt sendReceipt = (SendReceipt) ret; |
| 109 | + if (sendReceipt != null && sendReceipt.getMessageId() != null) { |
| 110 | + AbstractSpan activeSpan = ContextManager.activeSpan(); |
| 111 | + activeSpan.tag(Tags.ofKey("mq.message.id"), sendReceipt.getMessageId().toString()); |
| 112 | + } |
| 113 | + ContextManager.stopSpan(); |
| 114 | + return ret; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { |
| 119 | + ContextManager.activeSpan().log(t); |
| 120 | + } |
| 121 | + |
| 122 | + private String buildOperationName(String topicName) { |
| 123 | + return ASYNC_SEND_OPERATION_NAME_PREFIX + topicName + "/Producer"; |
| 124 | + } |
| 125 | +} |
0 commit comments