|
| 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.spring.rabbitmq; |
| 20 | + |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.apache.skywalking.apm.agent.core.context.CarrierItem; |
| 26 | +import org.apache.skywalking.apm.agent.core.context.ContextCarrier; |
| 27 | +import org.apache.skywalking.apm.agent.core.context.ContextManager; |
| 28 | +import org.apache.skywalking.apm.agent.core.context.tag.Tags; |
| 29 | +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; |
| 30 | +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; |
| 31 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 32 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.InstanceMethodsAroundInterceptorV2; |
| 33 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.v2.MethodInvocationContext; |
| 34 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 35 | +import org.springframework.amqp.core.Message; |
| 36 | +import org.springframework.amqp.core.MessageProperties; |
| 37 | + |
| 38 | +import com.rabbitmq.client.Channel; |
| 39 | +import com.rabbitmq.client.Connection; |
| 40 | + |
| 41 | +public class SpringRabbitMQConsumerInterceptor implements InstanceMethodsAroundInterceptorV2 { |
| 42 | + public static final String OPERATE_NAME_PREFIX = "RabbitMQ/"; |
| 43 | + public static final String CONSUMER_OPERATE_NAME_SUFFIX = "/Consumer"; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 47 | + MethodInvocationContext context) throws Throwable { |
| 48 | + Channel channel = (Channel) allArguments[0]; |
| 49 | + |
| 50 | + if (allArguments[1] instanceof Message) { |
| 51 | + // Single message consume |
| 52 | + Message message = (Message) allArguments[1]; |
| 53 | + MessageProperties messageProperties = message.getMessageProperties(); |
| 54 | + Map<String, Object> headers = messageProperties.getHeaders(); |
| 55 | + |
| 56 | + ContextCarrier contextCarrier = buildContextCarrier(headers); |
| 57 | + String operationName = buildOperationName(messageProperties); |
| 58 | + AbstractSpan activeSpan = ContextManager.createEntrySpan(operationName, contextCarrier); |
| 59 | + |
| 60 | + setSpanAttributes(activeSpan, channel, messageProperties); |
| 61 | + } else if (allArguments[1] instanceof List) { |
| 62 | + // Batch message consume |
| 63 | + List<?> messages = (List<?>) allArguments[1]; |
| 64 | + if (messages.isEmpty()) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Use the first message to create EntrySpan |
| 69 | + Message firstMessage = (Message) messages.get(0); |
| 70 | + MessageProperties firstMessageProperties = firstMessage.getMessageProperties(); |
| 71 | + Map<String, Object> firstMessageHeaders = firstMessageProperties.getHeaders(); |
| 72 | + |
| 73 | + ContextCarrier contextCarrier = buildContextCarrier(firstMessageHeaders); |
| 74 | + String operationName = buildOperationName(firstMessageProperties); |
| 75 | + AbstractSpan activeSpan = ContextManager.createEntrySpan(operationName, contextCarrier); |
| 76 | + |
| 77 | + setSpanAttributes(activeSpan, channel, firstMessageProperties); |
| 78 | + |
| 79 | + // Extract trace context from remaining messages (skip first, already used for EntrySpan) |
| 80 | + // to correlate all producer traces with this consumer span |
| 81 | + for (int i = 1; i < messages.size(); i++) { |
| 82 | + Object msg = messages.get(i); |
| 83 | + if (msg instanceof Message) { |
| 84 | + Message message = (Message) msg; |
| 85 | + MessageProperties messageProperties = message.getMessageProperties(); |
| 86 | + Map<String, Object> headers = messageProperties.getHeaders(); |
| 87 | + |
| 88 | + ContextCarrier carrier = buildContextCarrier(headers); |
| 89 | + if (carrier.isValid()) { |
| 90 | + ContextManager.extract(carrier); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Build ContextCarrier from message headers |
| 99 | + */ |
| 100 | + private ContextCarrier buildContextCarrier(Map<String, Object> headers) { |
| 101 | + ContextCarrier contextCarrier = new ContextCarrier(); |
| 102 | + CarrierItem next = contextCarrier.items(); |
| 103 | + while (next.hasNext()) { |
| 104 | + next = next.next(); |
| 105 | + Object value = headers.get(next.getHeadKey()); |
| 106 | + if (value != null) { |
| 107 | + next.setHeadValue(value.toString()); |
| 108 | + } |
| 109 | + } |
| 110 | + return contextCarrier; |
| 111 | + } |
| 112 | + |
| 113 | + private String buildOperationName(MessageProperties messageProperties) { |
| 114 | + return OPERATE_NAME_PREFIX + "Topic/" + messageProperties.getReceivedExchange() |
| 115 | + + "Queue/" + messageProperties.getReceivedRoutingKey() |
| 116 | + + CONSUMER_OPERATE_NAME_SUFFIX; |
| 117 | + } |
| 118 | + |
| 119 | + private void setSpanAttributes(AbstractSpan span, Channel channel, MessageProperties messageProperties) { |
| 120 | + Connection connection = channel.getConnection(); |
| 121 | + String serverUrl = connection.getAddress().getHostAddress() + ":" + connection.getPort(); |
| 122 | + Tags.MQ_BROKER.set(span, serverUrl); |
| 123 | + Tags.MQ_TOPIC.set(span, messageProperties.getReceivedExchange()); |
| 124 | + Tags.MQ_QUEUE.set(span, messageProperties.getReceivedRoutingKey()); |
| 125 | + span.setComponent(ComponentsDefine.RABBITMQ_CONSUMER); |
| 126 | + span.setPeer(serverUrl); |
| 127 | + SpanLayer.asMQ(span); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, |
| 132 | + Object ret, MethodInvocationContext context) throws Throwable { |
| 133 | + if (ContextManager.isActive()) { |
| 134 | + ContextManager.stopSpan(); |
| 135 | + } |
| 136 | + return ret; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, |
| 141 | + Class<?>[] argumentsTypes, Throwable t, MethodInvocationContext context) { |
| 142 | + if (ContextManager.isActive()) { |
| 143 | + ContextManager.activeSpan().log(t); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments