Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] alert integration extern source #2978

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.alert.service.AlertService;
import org.apache.hertzbeat.common.entity.alerter.SingleAlert;
import org.apache.hertzbeat.alert.service.ExternAlertService;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.dto.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -40,23 +42,56 @@
@RequestMapping(path = "/api/alerts/report", produces = {APPLICATION_JSON_VALUE})
@Slf4j
public class AlertReportController {

private final List<ExternAlertService> externAlertServiceList;

@Autowired
private AlertService alertService;
public AlertReportController(List<ExternAlertService> externAlertServiceList) {
this.externAlertServiceList = externAlertServiceList;
}

@PostMapping("/{cloud}")
@Operation(summary = "Interface for reporting external alarm information of cloud service")
public ResponseEntity<Message<Void>> addNewAlertReportFromCloud(@PathVariable("cloud") String cloudServiceName,
@RequestBody String alertReport) {
alertService.addNewAlertReportFromCloud(cloudServiceName, alertReport);
return ResponseEntity.ok(Message.success("Add report success"));
@PostMapping("/{source}")
@Operation(summary = "Api for receive external alarm information")
public ResponseEntity<Message<Void>> receiveExternAlert(@PathVariable(value = "source") String source,
@RequestBody String content) {
log.info("Receive extern alert from source: {}, content: {}", source, content);
if (!StringUtils.hasText(source)) {
source = "default";
}
for (ExternAlertService externAlertService : externAlertServiceList) {
if (externAlertService.supportSource().equals(source)) {
try {
externAlertService.addExternAlert(content);
return ResponseEntity.ok(Message.success("Add extern alert success"));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Message.fail(CommonConstants.FAIL_CODE,
"Add extern alert failed: " + e.getMessage()));
}
}
}
log.warn("Not support extern alert from source: {}", source);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Message.fail(CommonConstants.FAIL_CODE, "Not support the " + source + " source alert"));
}

@PostMapping
@Operation(summary = "Interface for reporting external and general alarm information",
description = "The interface is used to report external and general alarm information")
public ResponseEntity<Message<Void>> addNewAlertReport(@RequestBody SingleAlert alertReport) {
alertService.addNewAlertReport(alertReport);
return ResponseEntity.ok(Message.success("Add report success"));
@Operation(summary = "Api for receive default external alarm information")
public ResponseEntity<Message<Void>> receiveDefaultExternAlert(@RequestBody String content) {
log.info("Receive default extern alert content: {}", content);
ExternAlertService externAlertService = externAlertServiceList.stream()
.filter(item -> "default".equals(item.supportSource())).findFirst().orElse(null);
if (externAlertService != null) {
try {
externAlertService.addExternAlert(content);
return ResponseEntity.ok(Message.success("Add extern alert success"));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Message.fail(CommonConstants.FAIL_CODE,
"Add extern alert failed: " + e.getMessage()));
}
}
log.error("Not support default extern alert");
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Message.success("Not support the default source alert"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.hertzbeat.alert.dto;

import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* AlertManagerExternAlert
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AlertManagerExternAlert {

private String groupKey;

private String externalURL;

private String version;

private String status;

private Map<String, String> groupLabels;

private Map<String, String> commonLabels;

private Map<String, String> commonAnnotations;

private List<PrometheusExternAlert> alerts;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.hertzbeat.alert.dto;

import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
* Prometheus Alert Content Entity
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PrometheusExternAlert {

private Map<String, String> labels;

private Map<String, String> annotations;

private String status;

private Long startsAt;

private Long activeAt;

private Long endsAt;

private String generatorURL;
}
Loading
Loading