in hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertManagerExternAlertService.java [48:97]
public void addExternAlert(String content) {
AlertManagerExternAlert alert = JsonUtil.fromJson(content, AlertManagerExternAlert.class);
if (alert == null) {
log.warn("parse alertmanager extern alert content failed! content: {}", content);
return;
}
List<PrometheusExternAlert> alerts = alert.getAlerts();
if (alerts == null || alerts.isEmpty()) {
log.warn("receive alertmanager extern alert without alerts! content: {}", content);
return;
}
for (PrometheusExternAlert prometheusAlert : alerts) {
Map<String, String> annotations = prometheusAlert.getAnnotations();
if (annotations == null) {
annotations = new HashMap<>(8);
}
if (StringUtils.hasText(prometheusAlert.getGeneratorURL())) {
annotations.put("generatorURL", prometheusAlert.getGeneratorURL());
}
String description = annotations.get("description");
if (description == null) {
description = annotations.get("summary");
}
if (description == null) {
description = annotations.values().stream().findFirst().orElse("");
}
Map<String, String> labels = prometheusAlert.getLabels();
if (labels == null) {
labels = new HashMap<>(8);
}
labels.put("__source__", "alertmanager");
String status = CommonConstants.ALERT_STATUS_FIRING;
if (prometheusAlert.getEndsAt() != null && prometheusAlert.getEndsAt().isBefore(Instant.now())) {
status = CommonConstants.ALERT_STATUS_RESOLVED;
}
SingleAlert singleAlert = SingleAlert.builder()
.content(description)
.status(status)
.activeAt(CommonConstants.ALERT_STATUS_FIRING.equals(status) ? Instant.now().toEpochMilli() : null)
.startAt(prometheusAlert.getStartsAt() != null ? prometheusAlert.getStartsAt().toEpochMilli() : Instant.now().toEpochMilli())
.endAt(CommonConstants.ALERT_STATUS_RESOLVED.equals(status) ? prometheusAlert.getEndsAt().toEpochMilli() : null)
.labels(labels)
.annotations(prometheusAlert.getAnnotations())
.triggerTimes(1)
.build();
alarmCommonReduce.reduceAndSendAlarm(singleAlert);
}
}