Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit ee88112

Browse files
Verify connection before binding metrics.
1 parent fd00aa1 commit ee88112

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

neo4j-java-driver-spring-boot-autoconfigure/src/main/java/org/neo4j/driver/springframework/boot/autoconfigure/Neo4jDriverMetricsAutoConfiguration.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Collections;
2424
import java.util.Map;
2525

26+
import org.apache.commons.logging.Log;
27+
import org.apache.commons.logging.LogFactory;
2628
import org.neo4j.driver.Driver;
2729
import org.neo4j.driver.springframework.boot.actuate.Neo4jDriverMetrics;
2830
import org.springframework.beans.factory.annotation.Autowired;
@@ -53,15 +55,22 @@
5355
@ConditionalOnBean({ Driver.class, MeterRegistry.class })
5456
public class Neo4jDriverMetricsAutoConfiguration {
5557

58+
private static final Log logger = LogFactory.getLog(Neo4jDriverMetricsAutoConfiguration.class);
59+
5660
@Autowired
5761
public void bindDataSourcesToRegistry(Map<String, Driver> drivers, MeterRegistry registry) {
5862

5963
drivers.forEach((name, driver) -> {
6064
if (!Neo4jDriverMetrics.metricsAreEnabled(driver)) {
6165
return;
6266
}
63-
64-
new Neo4jDriverMetrics(name, driver, Collections.emptyList()).bindTo(registry);
67+
driver
68+
.verifyConnectivityAsync()
69+
.thenRunAsync(() -> new Neo4jDriverMetrics(name, driver, Collections.emptyList()).bindTo(registry))
70+
.exceptionally(e -> {
71+
logger.warn("Could not verify connection for " + driver + " and thus not bind to metrics.", e);
72+
return null;
73+
});
6574
});
6675
}
6776
}

neo4j-java-driver-spring-boot-autoconfigure/src/test/java/org/neo4j/driver/springframework/boot/test/Neo4jDriverMocks.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.HashMap;
2424
import java.util.Map;
25+
import java.util.concurrent.CompletableFuture;
2526

2627
import org.neo4j.driver.ConnectionPoolMetrics;
2728
import org.neo4j.driver.Driver;
@@ -47,6 +48,8 @@ public static Driver mockDriverWithMetrics() {
4748
Driver driver = mock(Driver.class);
4849
when(driver.metrics()).thenReturn(metrics);
4950

51+
when(driver.verifyConnectivityAsync()).thenReturn(CompletableFuture.completedFuture(null));
52+
5053
return driver;
5154
}
5255

0 commit comments

Comments
 (0)