-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
DataSourceHealthCheck.java
83 lines (72 loc) · 3.16 KB
/
DataSourceHealthCheck.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
package io.quarkus.agroal.runtime.health;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.sql.DataSource;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
import org.eclipse.microprofile.health.Readiness;
import io.agroal.api.AgroalDataSource;
import io.quarkus.agroal.runtime.AgroalDataSourceSupport;
import io.quarkus.agroal.runtime.DataSources;
import io.quarkus.arc.Arc;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.datasource.runtime.DataSourceSupport;
@Readiness
@ApplicationScoped
public class DataSourceHealthCheck implements HealthCheck {
@Inject
Instance<DataSources> dataSources;
private final Map<String, DataSource> checkedDataSources = new HashMap<>();
@PostConstruct
protected void init() {
if (!dataSources.isResolvable()) {
// No configured Agroal datasource at build time.
return;
}
DataSourceSupport support = Arc.container().instance(DataSourceSupport.class)
.get();
AgroalDataSourceSupport agroalSupport = Arc.container().instance(AgroalDataSourceSupport.class)
.get();
Set<String> excludedNames = support.getInactiveOrHealthCheckExcludedNames();
for (String name : agroalSupport.entries.keySet()) {
if (excludedNames.contains(name)) {
continue;
}
DataSource ds = dataSources.get().getDataSource(name);
if (ds != null) {
checkedDataSources.put(name, ds);
}
}
}
@Override
public HealthCheckResponse call() {
HealthCheckResponseBuilder builder = HealthCheckResponse.named("Database connections health check").up();
for (Map.Entry<String, DataSource> dataSource : checkedDataSources.entrySet()) {
boolean isDefault = DataSourceUtil.isDefault(dataSource.getKey());
AgroalDataSource ads = (AgroalDataSource) dataSource.getValue();
String dsName = dataSource.getKey();
try {
boolean valid = ads.isHealthy(false);
if (!valid) {
String data = isDefault ? "validation check failed for the default DataSource"
: "validation check failed for DataSource '" + dataSource.getKey() + "'";
builder.down().withData(dsName, data);
} else {
builder.withData(dsName, "UP");
}
} catch (SQLException e) {
String data = isDefault ? "Unable to execute the validation check for the default DataSource: "
: "Unable to execute the validation check for DataSource '" + dataSource.getKey() + "': ";
builder.down().withData(dsName, data + e.getMessage());
}
}
return builder.build();
}
}