public void addExternAlert()

in hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/PrometheusExternAlertService.java [48:93]


    public void addExternAlert(String content) {

        TypeReference<List<PrometheusExternAlert>> typeReference = new TypeReference<>() {};
        List<PrometheusExternAlert> alerts = JsonUtil.fromJson(content, typeReference);
        if (alerts == null || alerts.isEmpty()) {
            log.warn("parse prometheus extern alert content failed! content: {}", content);
            return;
        }
        for (PrometheusExternAlert alert : alerts) {
            Map<String, String> annotations = alert.getAnnotations();
            if (annotations == null) {
                annotations = new HashMap<>(8);
            }
            if (StringUtils.hasText(alert.getGeneratorURL())) {
                annotations.put("generatorURL", alert.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 = alert.getLabels();
            if (labels == null) {
                labels = new HashMap<>(8);
            }
            labels.put("__source__", "prometheus");
            String status = CommonConstants.ALERT_STATUS_FIRING;
            if (alert.getEndsAt() != null && alert.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(alert.getStartsAt() != null ? alert.getStartsAt().toEpochMilli() : Instant.now().toEpochMilli())
                    .endAt(CommonConstants.ALERT_STATUS_RESOLVED.equals(status) ? alert.getEndsAt().toEpochMilli() : null)
                    .labels(labels)
                    .annotations(alert.getAnnotations())
                    .triggerTimes(1)
                    .build();

            alarmCommonReduce.reduceAndSendAlarm(singleAlert);   
        }
    }