21
21
import java .util .concurrent .atomic .AtomicReference ;
22
22
import java .util .stream .Stream ;
23
23
24
- import io .micrometer .jakarta9 .instrument .jms .JmsProcessObservationContext ;
25
24
import io .micrometer .observation .Observation ;
26
- import io .micrometer .observation .ObservationHandler ;
27
25
import io .micrometer .observation .tck .TestObservationRegistry ;
28
26
import jakarta .jms .MessageListener ;
29
27
import org .apache .activemq .artemis .jms .client .ActiveMQConnectionFactory ;
@@ -65,15 +63,15 @@ void setupServer() {
65
63
@ ParameterizedTest (name = "[{index}] {0}" )
66
64
@ MethodSource ("listenerContainers" )
67
65
void shouldRecordJmsProcessObservations (AbstractMessageListenerContainer listenerContainer ) throws Exception {
68
- JmsTemplate jmsTemplate = new JmsTemplate (connectionFactory );
69
- jmsTemplate .convertAndSend ("spring.test.observation" , "message content" );
70
66
CountDownLatch latch = new CountDownLatch (1 );
71
67
listenerContainer .setConnectionFactory (connectionFactory );
72
68
listenerContainer .setObservationRegistry (registry );
73
69
listenerContainer .setDestinationName ("spring.test.observation" );
74
70
listenerContainer .setMessageListener ((MessageListener ) message -> latch .countDown ());
75
71
listenerContainer .afterPropertiesSet ();
76
72
listenerContainer .start ();
73
+ JmsTemplate jmsTemplate = new JmsTemplate (connectionFactory );
74
+ jmsTemplate .convertAndSend ("spring.test.observation" , "message content" );
77
75
latch .await (2 , TimeUnit .SECONDS );
78
76
assertThat (registry ).hasObservationWithNameEqualTo ("jms.message.process" )
79
77
.that ()
@@ -86,8 +84,6 @@ void shouldRecordJmsProcessObservations(AbstractMessageListenerContainer listene
86
84
@ ParameterizedTest (name = "[{index}] {0}" )
87
85
@ MethodSource ("listenerContainers" )
88
86
void shouldHaveObservationScopeInErrorHandler (AbstractMessageListenerContainer listenerContainer ) throws Exception {
89
- JmsTemplate jmsTemplate = new JmsTemplate (connectionFactory );
90
- jmsTemplate .convertAndSend ("spring.test.observation" , "message content" );
91
87
CountDownLatch latch = new CountDownLatch (1 );
92
88
AtomicReference <Observation > observationInErrorHandler = new AtomicReference <>();
93
89
listenerContainer .setConnectionFactory (connectionFactory );
@@ -102,64 +98,19 @@ void shouldHaveObservationScopeInErrorHandler(AbstractMessageListenerContainer l
102
98
});
103
99
listenerContainer .afterPropertiesSet ();
104
100
listenerContainer .start ();
105
- latch .await (2 , TimeUnit .SECONDS );
106
- Assertions .assertThat (observationInErrorHandler .get ()).isNotNull ();
107
- assertThat (registry ).hasObservationWithNameEqualTo ("jms.message.process" )
108
- .that ()
109
- .hasHighCardinalityKeyValue ("messaging.destination.name" , "spring.test.observation" )
110
- .hasLowCardinalityKeyValue ("exception" , "none" );
111
- assertThat (registry ).hasNumberOfObservationsEqualTo (1 );
112
- listenerContainer .stop ();
113
- listenerContainer .shutdown ();
114
- }
115
-
116
- @ ParameterizedTest (name = "[{index}] {0}" )
117
- @ MethodSource ("listenerContainers" )
118
- void shouldHaveObservationErrorWhenRethrown (AbstractMessageListenerContainer listenerContainer ) throws Exception {
119
101
JmsTemplate jmsTemplate = new JmsTemplate (connectionFactory );
120
102
jmsTemplate .convertAndSend ("spring.test.observation" , "message content" );
121
- CountDownLatch latch = new CountDownLatch (1 );
122
- registry .observationConfig ().observationHandler (new ErrorHandlerObservationHandler (latch ));
123
- listenerContainer .setConnectionFactory (connectionFactory );
124
- listenerContainer .setObservationRegistry (registry );
125
- listenerContainer .setDestinationName ("spring.test.observation" );
126
- listenerContainer .setMessageListener ((MessageListener ) message -> {
127
- throw new IllegalStateException ("error" );
128
- });
129
- listenerContainer .setErrorHandler (error -> {
130
- throw new IllegalStateException ("not handled" );
131
- });
132
- listenerContainer .afterPropertiesSet ();
133
- listenerContainer .start ();
134
103
latch .await (2 , TimeUnit .SECONDS );
104
+ Assertions .assertThat (observationInErrorHandler .get ()).isNotNull ();
135
105
assertThat (registry ).hasObservationWithNameEqualTo ("jms.message.process" )
136
106
.that ()
137
107
.hasHighCardinalityKeyValue ("messaging.destination.name" , "spring.test.observation" )
138
- .hasLowCardinalityKeyValue ("exception" , "IllegalStateException " );
108
+ .hasLowCardinalityKeyValue ("exception" , "none " );
139
109
assertThat (registry ).hasNumberOfObservationsEqualTo (1 );
140
110
listenerContainer .stop ();
141
111
listenerContainer .shutdown ();
142
112
}
143
113
144
- static class ErrorHandlerObservationHandler implements ObservationHandler <JmsProcessObservationContext > {
145
-
146
- private final CountDownLatch latch ;
147
-
148
- ErrorHandlerObservationHandler (CountDownLatch latch ) {
149
- this .latch = latch ;
150
- }
151
-
152
- @ Override
153
- public boolean supportsContext (Observation .Context context ) {
154
- return context instanceof JmsProcessObservationContext ;
155
- }
156
-
157
- @ Override
158
- public void onError (JmsProcessObservationContext context ) {
159
- this .latch .countDown ();
160
- }
161
- }
162
-
163
114
static Stream <Arguments > listenerContainers () {
164
115
return Stream .of (
165
116
arguments (named (DefaultMessageListenerContainer .class .getSimpleName (), new DefaultMessageListenerContainer ())),
0 commit comments