24
24
import com .mongodb .connection .ConnectionPoolSettings ;
25
25
import com .mongodb .connection .ServerId ;
26
26
import com .mongodb .event .*;
27
+ import io .micrometer .common .lang .NonNull ;
27
28
import io .micrometer .core .Issue ;
28
29
import io .micrometer .core .instrument .MeterRegistry ;
29
30
import io .micrometer .core .instrument .Tags ;
30
31
import io .micrometer .core .instrument .simple .SimpleMeterRegistry ;
31
32
import org .junit .jupiter .api .Test ;
32
33
34
+ import java .util .concurrent .TimeUnit ;
33
35
import java .util .concurrent .atomic .AtomicReference ;
34
36
35
37
import static java .util .Collections .singletonList ;
@@ -55,7 +57,7 @@ void shouldCreatePoolMetrics() {
55
57
.applyToClusterSettings (builder -> builder .hosts (singletonList (new ServerAddress (host , port )))
56
58
.addClusterListener (new ClusterListener () {
57
59
@ Override
58
- public void clusterOpening (ClusterOpeningEvent event ) {
60
+ public void clusterOpening (@ NonNull ClusterOpeningEvent event ) {
59
61
clusterId .set (event .getClusterId ().getValue ());
60
62
}
61
63
}))
@@ -91,7 +93,7 @@ void shouldCreatePoolMetricsWithCustomTags() {
91
93
.applyToClusterSettings (builder -> builder .hosts (singletonList (new ServerAddress (host , port )))
92
94
.addClusterListener (new ClusterListener () {
93
95
@ Override
94
- public void clusterOpening (ClusterOpeningEvent event ) {
96
+ public void clusterOpening (@ NonNull ClusterOpeningEvent event ) {
95
97
clusterId .set (event .getClusterId ().getValue ());
96
98
}
97
99
}))
@@ -107,6 +109,7 @@ public void clusterOpening(ClusterOpeningEvent event) {
107
109
108
110
assertThat (registry .get ("mongodb.driver.pool.size" ).tags (tags ).gauge ().value ()).isEqualTo (2 );
109
111
assertThat (registry .get ("mongodb.driver.pool.checkedout" ).gauge ().value ()).isZero ();
112
+ assertThat (registry .get ("mongodb.driver.pool.checkoutfailed" ).counter ().count ()).isZero ();
110
113
assertThat (registry .get ("mongodb.driver.pool.waitqueuesize" ).gauge ().value ()).isZero ();
111
114
112
115
mongo .close ();
@@ -117,6 +120,28 @@ public void clusterOpening(ClusterOpeningEvent event) {
117
120
.isNull ();
118
121
}
119
122
123
+ @ Test
124
+ void shouldIncrementCheckoutFailedCount () {
125
+ ServerId serverId = new ServerId (new ClusterId (), new ServerAddress (host , port ));
126
+ MongoMetricsConnectionPoolListener listener = new MongoMetricsConnectionPoolListener (registry );
127
+ listener
128
+ .connectionPoolCreated (new ConnectionPoolCreatedEvent (serverId , ConnectionPoolSettings .builder ().build ()));
129
+
130
+ // start a connection checkout
131
+ listener .connectionCheckOutStarted (new ConnectionCheckOutStartedEvent (serverId , -1 ));
132
+ assertThat (registry .get ("mongodb.driver.pool.waitqueuesize" ).gauge ().value ()).isEqualTo (1 );
133
+ assertThat (registry .get ("mongodb.driver.pool.checkoutfailed" ).counter ().count ()).isZero ();
134
+
135
+ // let the connection checkout fail, simulating a timeout
136
+ ConnectionCheckOutFailedEvent .Reason reason = ConnectionCheckOutFailedEvent .Reason .TIMEOUT ;
137
+ long elapsedTimeNanos = TimeUnit .SECONDS .toNanos (120 );
138
+ ConnectionCheckOutFailedEvent checkOutFailedEvent = new ConnectionCheckOutFailedEvent (serverId , -1 , reason ,
139
+ elapsedTimeNanos );
140
+ listener .connectionCheckOutFailed (checkOutFailedEvent );
141
+ assertThat (registry .get ("mongodb.driver.pool.waitqueuesize" ).gauge ().value ()).isZero ();
142
+ assertThat (registry .get ("mongodb.driver.pool.checkoutfailed" ).counter ().count ()).isEqualTo (1 );
143
+ }
144
+
120
145
@ Issue ("#2384" )
121
146
@ Test
122
147
void whenConnectionCheckedInAfterPoolClose_thenNoExceptionThrown () {
@@ -125,9 +150,9 @@ void whenConnectionCheckedInAfterPoolClose_thenNoExceptionThrown() {
125
150
MongoMetricsConnectionPoolListener listener = new MongoMetricsConnectionPoolListener (registry );
126
151
listener
127
152
.connectionPoolCreated (new ConnectionPoolCreatedEvent (serverId , ConnectionPoolSettings .builder ().build ()));
128
- listener .connectionCheckedOut (new ConnectionCheckedOutEvent (connectionId ));
153
+ listener .connectionCheckedOut (new ConnectionCheckedOutEvent (connectionId , - 1 , 0 ));
129
154
listener .connectionPoolClosed (new ConnectionPoolClosedEvent (serverId ));
130
- assertThatCode (() -> listener .connectionCheckedIn (new ConnectionCheckedInEvent (connectionId )))
155
+ assertThatCode (() -> listener .connectionCheckedIn (new ConnectionCheckedInEvent (connectionId , - 1 )))
131
156
.doesNotThrowAnyException ();
132
157
}
133
158
0 commit comments