29
29
30
30
import org .springframework .aop .support .AopUtils ;
31
31
import org .springframework .cloud .sleuth .Span ;
32
- import org .springframework .cloud .sleuth .event .ClientReceivedEvent ;
33
- import org .springframework .cloud .sleuth .event .ClientSentEvent ;
34
- import org .springframework .context .ApplicationEvent ;
35
- import org .springframework .context .ApplicationEventPublisher ;
36
- import org .springframework .context .ApplicationEventPublisherAware ;
37
32
import org .springframework .integration .channel .DirectChannel ;
38
33
import org .springframework .messaging .Message ;
39
34
import org .springframework .messaging .MessageChannel ;
44
39
import org .springframework .util .Assert ;
45
40
46
41
/**
47
- * The {@link ExecutorChannelInterceptor} implementation responsible for
48
- * the {@link Span} propagation from one message flow's thread to another
49
- * through the {@link MessageChannel}s involved in the flow.
42
+ * The {@link ExecutorChannelInterceptor} implementation responsible for the {@link Span}
43
+ * propagation from one message flow's thread to another through the
44
+ * {@link MessageChannel}s involved in the flow.
50
45
* <p>
51
- * In addition this interceptor cleans up (restores) the {@link Span}
52
- * in the containers Threads for channels like
53
- * {@link org.springframework.integration.channel.ExecutorChannel}
54
- * and {@link org.springframework.integration.channel.QueueChannel}.
46
+ * In addition this interceptor cleans up (restores) the {@link Span} in the containers
47
+ * Threads for channels like
48
+ * {@link org.springframework.integration.channel.ExecutorChannel} and
49
+ * {@link org.springframework.integration.channel.QueueChannel}.
55
50
* @author Spencer Gibb
56
51
* @since 1.0
57
52
*/
58
- public class TraceContextPropagationChannelInterceptor
59
- extends ChannelInterceptorAdapter implements ExecutorChannelInterceptor , ApplicationEventPublisherAware {
53
+ public class TraceContextPropagationChannelInterceptor extends ChannelInterceptorAdapter
54
+ implements ExecutorChannelInterceptor {
60
55
61
56
private final static ThreadLocal <Span > ORIGINAL_CONTEXT = new ThreadLocal <>();
62
57
63
- private ApplicationEventPublisher publisher ;
64
-
65
- @ Override
66
- public void setApplicationEventPublisher (ApplicationEventPublisher publisher ) {
67
- this .publisher = publisher ;
68
- }
69
-
70
58
@ Override
71
59
public final Message <?> preSend (Message <?> message , MessageChannel channel ) {
72
60
if (DirectChannel .class .isAssignableFrom (AopUtils .getTargetClass (channel ))) {
73
61
return message ;
74
62
}
75
63
76
- //TODO: start span from headers?
77
64
Span span = getCurrentSpan ();
78
65
79
66
if (span != null ) {
80
- publish (new ClientSentEvent (this , span ));
81
67
return new MessageWithSpan (message , span );
82
68
}
83
69
else {
@@ -86,63 +72,59 @@ public final Message<?> preSend(Message<?> message, MessageChannel channel) {
86
72
}
87
73
88
74
@ Override
89
- @ SuppressWarnings ("unchecked" )
90
75
public final Message <?> postReceive (Message <?> message , MessageChannel channel ) {
91
76
if (message instanceof MessageWithSpan ) {
92
77
MessageWithSpan messageWithSpan = (MessageWithSpan ) message ;
93
78
Message <?> messageToHandle = messageWithSpan .message ;
94
79
populatePropagatedContext (messageWithSpan .span , messageToHandle , channel );
95
80
96
- publish (new ClientReceivedEvent (this , messageWithSpan .span ));
97
81
return message ;
98
82
}
99
83
return message ;
100
84
}
101
85
102
86
@ Override
103
- public void afterMessageHandled (Message <?> message , MessageChannel channel , MessageHandler handler , Exception ex ) {
104
- Span originalContext = ORIGINAL_CONTEXT .get ();
105
- try {
106
- if (originalContext == null ) {
107
- setCurrentSpan (null );
108
- ORIGINAL_CONTEXT .remove ();
109
- }
110
- else {
111
- setCurrentSpan (originalContext );
112
- }
113
- }
114
- catch (Throwable t ) {//NOSONAR
115
- setCurrentSpan (null );
116
- }
87
+ public void afterMessageHandled (Message <?> message , MessageChannel channel ,
88
+ MessageHandler handler , Exception ex ) {
89
+ resetPropagatedContext ();
117
90
}
118
91
119
92
@ Override
120
- public final Message <?> beforeHandle (Message <?> message , MessageChannel channel , MessageHandler handler ) {
93
+ public final Message <?> beforeHandle (Message <?> message , MessageChannel channel ,
94
+ MessageHandler handler ) {
121
95
return postReceive (message , channel );
122
96
}
123
97
124
- private void publish (ApplicationEvent event ) {
125
- if (this .publisher !=null ) {
126
- this .publisher .publishEvent (event );
127
- }
128
- }
129
-
130
98
private String getParentId (Span span ) {
131
99
return span .getParents () != null && !span .getParents ().isEmpty () ? span
132
100
.getParents ().get (0 ) : null ;
133
101
}
134
102
135
103
protected void populatePropagatedContext (Span span , Message <?> message ,
136
- MessageChannel channel ) {
104
+ MessageChannel channel ) {
137
105
if (span != null ) {
138
106
Span currentContext = getCurrentSpan ();
139
-
140
107
ORIGINAL_CONTEXT .set (currentContext );
141
-
142
108
setCurrentSpan (span );
143
109
}
144
110
}
145
111
112
+ protected void resetPropagatedContext () {
113
+ Span originalContext = ORIGINAL_CONTEXT .get ();
114
+ try {
115
+ if (originalContext == null ) {
116
+ setCurrentSpan (null );
117
+ ORIGINAL_CONTEXT .remove ();
118
+ }
119
+ else {
120
+ setCurrentSpan (originalContext );
121
+ }
122
+ }
123
+ catch (Throwable t ) {// NOSONAR
124
+ setCurrentSpan (null );
125
+ }
126
+ }
127
+
146
128
private class MessageWithSpan implements Message <Object > {
147
129
148
130
private final Message <?> message ;
@@ -192,11 +174,8 @@ public MessageHeaders getHeaders() {
192
174
193
175
@ Override
194
176
public String toString () {
195
- return "MessageWithThreadState{" +
196
- "message=" + message +
197
- ", span=" + span +
198
- ", messageHeaders=" + messageHeaders +
199
- '}' ;
177
+ return "MessageWithThreadState{" + "message=" + this .message + ", span="
178
+ + this .span + ", messageHeaders=" + this .messageHeaders + '}' ;
200
179
}
201
180
202
181
}
0 commit comments