use of com.sequenceiq.periscope.model.PrometheusResponse in project cloudbreak by hortonworks.
the class PrometheusEvaluator method run.
@Override
public void run() {
try {
Cluster cluster = clusterService.find(clusterId);
MDCBuilder.buildMdcContext(cluster);
TlsConfiguration tlsConfig = tlsSecurityService.getConfiguration(cluster);
Client client = RestClientUtil.createClient(tlsConfig.getServerCert(), tlsConfig.getClientCert(), tlsConfig.getClientKey(), true, PrometheusEvaluator.class);
String prometheusAddress = String.format("https://%s:%s/prometheus", cluster.getAmbari().getHost(), cluster.getPort());
WebTarget target = client.target(prometheusAddress);
for (PrometheusAlert alert : alertRepository.findAllByCluster(clusterId)) {
String alertName = alert.getName();
LOGGER.info("Checking Prometheus based alert: '{}'", alertName);
String query = URLEncoder.encode(String.format("ALERTS{alertname=\"%s\"}[%dm]", alert.getName(), alert.getPeriod()), "UTF-8");
Response response = target.path("/api/v1/query").queryParam("query", query).request().header("Accept", MediaType.APPLICATION_JSON_VALUE).get();
PrometheusResponse prometheusResponse = JaxRSUtil.response(response, PrometheusResponse.class);
boolean triggerScale = false;
switch(alert.getAlertState()) {
case OK:
triggerScale = prometheusResponse.getData().getResult().isEmpty();
break;
case CRITICAL:
for (Result alertResult : prometheusResponse.getData().getResult()) {
if ("firing".equals(alertResult.getMetric().getAlertstate())) {
List<Object> lastSample = alertResult.getValues().get(alertResult.getValues().size() - 1);
Object alertValue = lastSample.get(1);
if (alertValue instanceof String) {
if ("0".equals(alertValue)) {
break;
}
triggerScale = true;
}
}
}
break;
default:
triggerScale = false;
break;
}
if (triggerScale && isPolicyAttached(alert)) {
publishEvent(new ScalingEvent(alert));
}
}
} catch (Exception e) {
LOGGER.error("Failed to retrieve alerts from Prometheus", e);
publishEvent(new UpdateFailedEvent(clusterId));
}
}
Aggregations