Actuator health endpoint #2

Merged
schatz merged 3 commits from add_health_endpoint into master 2024-06-12 13:00:30 +02:00
4 changed files with 58 additions and 13 deletions
Showing only changes of commit 061954cc47 - Show all commits

View File

@ -1,7 +1,10 @@
package eu.openaire.api.config;
package eu.openaire.api.config.health;
import eu.openaire.api.repositories.SolrRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
@ -15,17 +18,24 @@ public class HealthCheck implements HealthIndicator {
@Autowired
private SolrRepository solrRepository;
private final Logger log = LogManager.getLogger(this.getClass());
@Override
public Health health() {
System.out.println("HEALTH CHECK");
log.info("Health check triggered");
// Custom health check logic
boolean isHealthy = checkHealth();
if (isHealthy) {
return Health.up().withDetail("message", "Successfully pinged Solr instances").build();
String healthMsg = "Successfully pinged Solr cluster";
log.info(healthMsg);
return Health.up().withDetail("message", healthMsg).build();
} else {
return Health.down().withDetail("message", "Could not ping Solr instances; please check the logs for details").build();
String notHealthyMsg = "Could not ping Solr cluster; please check the logs for details";
log.error(notHealthyMsg);
return Health.down().withDetail("message", notHealthyMsg).build();
}
}
@ -36,7 +46,7 @@ public class HealthCheck implements HealthIndicator {
if (solrRepository.ping().getStatus() != 0) {
return false;
}
} catch (SolrServerException | IOException e) {
} catch (SolrException | IOException | SolrServerException e) {
return false;
}

View File

@ -1,2 +1,34 @@
package eu.openaire.api.config.health;public class SolrStartupHealthCheck {
package eu.openaire.api.config.health;
import eu.openaire.api.repositories.SolrRepository;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class SolrStartupHealthCheck implements ApplicationRunner {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private SolrRepository solrRepository;
@Override
public void run(ApplicationArguments args) throws Exception {
try {
if (solrRepository.ping().getStatus() != 0) {
throw new RuntimeException("Could not ping Solr cluster at startup");
}
} catch (SolrException | IOException | SolrServerException e) {
throw new RuntimeException("Failed to connect to Solr cluster at startup", e);
}
log.info("Successfully pinged Solr cluster at startup");
}
}

View File

@ -9,8 +9,8 @@ import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrException;
import org.springframework.stereotype.Repository;
import java.io.IOException;
@ -64,7 +64,7 @@ public class SolrRepository {
}
public void ping() throws SolrServerException, SolrException, IOException {
solrConnectionManager.ping();
public SolrPingResponse ping() throws SolrServerException, IOException {
return solrConnectionManager.ping();
}
}

View File

@ -8,7 +8,7 @@ import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.common.SolrException;
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@ -42,7 +42,10 @@ public class SolrConnectionManager {
// create SolrClient with ZooKeeper hosts
// TODO: add socket (or connection) timeout?
solrClient = new CloudSolrClient.Builder(zkHosts, Optional.empty()).build();
solrClient = new CloudSolrClient.Builder(zkHosts, Optional.empty())
.withConnectionTimeout(15000)
.withSocketTimeout(1000)
.build();
// set the appropriate Solr collection
((CloudSolrClient) solrClient).setDefaultCollection(solrProperties.getCollection());
@ -52,8 +55,8 @@ public class SolrConnectionManager {
}
public void ping() throws IOException, SolrException, SolrServerException {
log.info("Solr ping response: " + solrClient.ping().toString());
public SolrPingResponse ping() throws IOException, SolrServerException {
return solrClient.ping();
}
@PreDestroy