io.gravitee.node.monitoring.healthcheck.NodeHealthCheckThread Maven / Gradle / Ivy
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.node.monitoring.healthcheck;
import static io.gravitee.node.monitoring.MonitoringConstants.*;
import io.gravitee.alert.api.event.DefaultEvent;
import io.gravitee.alert.api.event.Event;
import io.gravitee.node.api.Node;
import io.gravitee.node.api.healthcheck.HealthCheck;
import io.gravitee.node.api.healthcheck.Probe;
import io.gravitee.node.api.healthcheck.Result;
import io.gravitee.node.monitoring.DefaultProbeEvaluator;
import io.gravitee.plugin.alert.AlertEventProducer;
import io.vertx.core.eventbus.MessageProducer;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@Slf4j
@RequiredArgsConstructor
public class NodeHealthCheckThread implements Runnable {
private final DefaultProbeEvaluator probeRegistry;
private final AlertEventProducer alertEventProducer;
private final MessageProducer producer;
private final Node node;
private long timestamp;
@Override
public void run() {
try {
this.timestamp = System.currentTimeMillis();
final Map results = probeRegistry.evaluate().get();
// We want to propagate health-check with visible probes only.
final HealthCheck healthCheck = getHealthCheck(results);
producer.write(healthCheck);
sendAlertEngineEvent(healthCheck);
} catch (Exception e) {
log.error("An error occurred when trying to evaluate health check probes.", e);
}
}
private void sendAlertEngineEvent(HealthCheck healthCheck) {
DefaultEvent.Builder builder = Event.now().type(NODE_HEALTHCHECK);
builder.property(PROPERTY_NODE_ID, node.id());
builder.property(PROPERTY_NODE_HOSTNAME, node.hostname());
builder.property(PROPERTY_NODE_APPLICATION, node.application());
builder.property(PROPERTY_NODE_HEALTHY, String.valueOf(healthCheck.isHealthy()));
builder.organizations((Set) node.metadata().get(Node.META_ORGANIZATIONS));
builder.environments((Set) node.metadata().get(Node.META_ENVIRONMENTS));
healthCheck
.getResults()
.forEach((probeId, result) -> {
builder.property(PROPERTY_PROBE_SUFFIX + probeId, result.isHealthy());
if (!result.isHealthy()) {
builder.property(PROPERTY_PROBE_SUFFIX + probeId + ".message", result.getMessage());
}
});
alertEventProducer.send(builder.build());
}
/**
* Returns the health-check with only probes that are visible by default.
*
* @return health-check with all probes which are visible by default.
*/
private HealthCheck getHealthCheck(Map results) {
Map visibleResults = results
.entrySet()
.stream()
.filter(entry -> entry.getKey().isVisibleByDefault())
.collect(Collectors.toMap(probeResultEntry -> probeResultEntry.getKey().id(), Map.Entry::getValue));
return new HealthCheck(timestamp, visibleResults);
}
}