|
| 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 | +package org.apache.skywalking.apm.plugin.activemq.artemis.jakarta.client; |
| 19 | + |
| 20 | +import jakarta.jms.Message; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import org.apache.activemq.artemis.jms.client.ActiveMQDestination; |
| 23 | +import org.apache.skywalking.apm.agent.core.context.CarrierItem; |
| 24 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 25 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 26 | +import org.apache.skywalking.apm.agent.core.context.tag.StringTag; |
| 27 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 28 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 29 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 30 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 31 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; |
| 32 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; |
| 33 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 34 | +import org.apache.skywalking.apm.plugin.activemq.artemis.jakarta.client.define.EnhanceInfo; |
| 35 | + |
| 36 | +/** |
| 37 | + * {@link MessageProducerInterceptor} create exit span when the method {@link org.apache.activemq.artemis.jms.client.ActiveMQMessageProducer#doSendx} |
| 38 | + * execute |
| 39 | + */ |
| 40 | +public class MessageProducerInterceptor implements InstanceMethodsAroundInterceptor { |
| 41 | + private static final String OPERATION_NAME_PREFIX = "ActiveMQ/"; |
| 42 | + private static final String PRODUCER_OPERATION_NAME_SUFFIX = "/Producer"; |
| 43 | + private static final StringTag MQ_MESSAGE_ID = new StringTag("mq.message.id"); |
| 44 | + private static final String QUEUE = "Queue"; |
| 45 | + private static final String TOPIC = "Topic"; |
| 46 | + |
| 47 | + @Override |
| 48 | + public void beforeMethod(final EnhancedInstance objInst, |
| 49 | + final Method method, |
| 50 | + final Object[] allArguments, |
| 51 | + final Class<?>[] classes, |
| 52 | + final MethodInterceptResult methodInterceptResult) throws Throwable { |
| 53 | + ContextCarrier contextCarrier = new ContextCarrier(); |
| 54 | + Message message = (Message) allArguments[1]; |
| 55 | + EnhanceInfo enhanceInfo = (EnhanceInfo) objInst.getSkyWalkingDynamicField(); |
| 56 | + boolean queue = isQueue(enhanceInfo.getType()); |
| 57 | + AbstractSpan activeSpan = ContextManager.createExitSpan( |
| 58 | + buildOperationName(queue, enhanceInfo.getName()), |
| 59 | + contextCarrier, enhanceInfo.getBrokerUrl() |
| 60 | + ); |
| 61 | + contextCarrier.extensionInjector().injectSendingTimestamp(); |
| 62 | + Tags.MQ_BROKER.set(activeSpan, enhanceInfo.getBrokerUrl()); |
| 63 | + if (queue) { |
| 64 | + Tags.MQ_QUEUE.set(activeSpan, enhanceInfo.getName()); |
| 65 | + } else { |
| 66 | + Tags.MQ_TOPIC.set(activeSpan, enhanceInfo.getName()); |
| 67 | + } |
| 68 | + SpanLayer.asMQ(activeSpan); |
| 69 | + activeSpan.setComponent(ComponentsDefine.ACTIVEMQ_PRODUCER); |
| 70 | + CarrierItem next = contextCarrier.items(); |
| 71 | + |
| 72 | + while (next.hasNext()) { |
| 73 | + next = next.next(); |
| 74 | + message.setStringProperty(next.getHeadKey().replace("-", "_"), next.getHeadValue()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Object afterMethod(final EnhancedInstance enhancedInstance, |
| 80 | + final Method method, |
| 81 | + final Object[] allArguments, |
| 82 | + final Class<?>[] classes, |
| 83 | + final Object ret) throws Throwable { |
| 84 | + AbstractSpan activeSpan = ContextManager.activeSpan(); |
| 85 | + Message message = (Message) allArguments[1]; |
| 86 | + activeSpan.tag(MQ_MESSAGE_ID, message.getJMSMessageID()); |
| 87 | + ContextManager.stopSpan(); |
| 88 | + return ret; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void handleMethodException(final EnhancedInstance enhancedInstance, |
| 93 | + final Method method, |
| 94 | + final Object[] objects, |
| 95 | + final Class<?>[] classes, |
| 96 | + final Throwable t) { |
| 97 | + ContextManager.activeSpan().log(t); |
| 98 | + } |
| 99 | + |
| 100 | + private boolean isQueue(ActiveMQDestination.TYPE type) { |
| 101 | + return ActiveMQDestination.TYPE.QUEUE.equals(type) || ActiveMQDestination.TYPE.TEMP_QUEUE.equals(type); |
| 102 | + } |
| 103 | + |
| 104 | + private String buildOperationName(boolean isQueue, String name) { |
| 105 | + if (isQueue) { |
| 106 | + return OPERATION_NAME_PREFIX + QUEUE + "/" + name + PRODUCER_OPERATION_NAME_SUFFIX; |
| 107 | + } else { |
| 108 | + return OPERATION_NAME_PREFIX + TOPIC + "/" + name + PRODUCER_OPERATION_NAME_SUFFIX; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments