-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRocketMQMessageProducerSendInterceptor.java
115 lines (96 loc) · 3.79 KB
/
RocketMQMessageProducerSendInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.navercorp.pinpoint.plugin.rocketmq.client.interceptor;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.protocol.header.SendMessageRequestHeader;
import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.common.trace.AnnotationKey;
import com.navercorp.pinpoint.plugin.rocketmq.client.RocketMQConstants;
import java.util.Map;
/**
* @author HyunGil Jeong
*/
public class RocketMQMessageProducerSendInterceptor implements AroundInterceptor {
private final PLogger logger = PLoggerFactory.getLogger(this.getClass());
private final boolean isDebug = logger.isDebugEnabled();
private final TraceContext traceContext;
private final MethodDescriptor descriptor;
public RocketMQMessageProducerSendInterceptor(TraceContext traceContext, MethodDescriptor descriptor) {
this.traceContext = traceContext;
this.descriptor = descriptor;
}
@Override
public void before(Object target, Object[] args) {
if (isDebug) {
logger.beforeInterceptor(target, args);
}
Trace trace = traceContext.currentRawTraceObject();
if (trace == null) {
return;
}
Message message = (Message) args[2];
Map<String, String> header = message.getProperties();
try {
if (trace.canSampled()) {
SpanEventRecorder recorder = trace.traceBlockBegin();
recorder.recordServiceType(RocketMQConstants.ROCKETMQ_CLIENT);
TraceId nextId = trace.getTraceId().getNextTraceId();
recorder.recordNextSpanId(nextId.getSpanId());
header.put("Pinpoint-TraceID", nextId.getTransactionId());
header.put("Pinpoint-SpanID", nextId.getSpanId() + "");
header.put("Pinpoint-pSpanID", nextId.getParentSpanId() + "");
header.put("Pinpoint-Flags", nextId.getFlags() + "");
header.put("Pinpoint-pAppName", traceContext.getApplicationName());
header.put("Pinpoint-pAppType", traceContext.getServerTypeCode() + "");
} else {
header.put("Pinpoint-Sampled", 0 + "");
}
} catch (Throwable t) {
logger.warn("BEFORE. Cause:{}", t.getMessage(), t);
}
}
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
if (isDebug) {
logger.afterInterceptor(target, args);
}
Trace trace = traceContext.currentTraceObject();
if (trace == null) {
return;
}
String brokerAddr = "UNKNOW";
String brokerName = "UNKNOW";
if(null!= args[0]){
brokerAddr = (String) args[0];
}
if(null!=args[1]){
brokerName = (String) args[1];
}
Message message = (Message) args[2];
try {
SpanEventRecorder recorder = trace.currentSpanEventRecorder();
recorder.recordApi(descriptor);
recorder.recordAttribute(RocketMQConstants.ROCKETMQ_MESSAGE, new String(message.getBody()));
if (throwable == null) {
recorder.recordEndPoint(brokerAddr);
recorder.recordAttribute(RocketMQConstants.ROCKETMQ_BROKER_URL, brokerAddr);
// This annotation indicates the uri to which the call is made
recorder.recordAttribute(AnnotationKey.MESSAGE_QUEUE_URI,brokerName);
SendMessageRequestHeader requestHeader=(SendMessageRequestHeader) args[3];
// DestinationId is used to render the virtual queue node.
recorder.recordDestinationId(requestHeader.getTopic());
} else {
recorder.recordException(throwable);
}
} catch (Throwable t) {
logger.warn("AFTER error. Cause:{}", t.getMessage(), t);
} finally {
trace.traceBlockEnd();
}
}
}