Skip to content

Commit

Permalink
[type:feat]Alert module adapts to namespace transformation (#5692)
Browse files Browse the repository at this point in the history
* feat_namespace_alert

* fix admin : update h2sql script to correct table schema.

* Resolve merge conflicts

* fix

* fix

* fix

* fix

* fix

* fix

* refactor plugin-base:refactor alarm-sender.

* fix admin:remove some error namespaceid parameter.

---------

Co-authored-by: aias00 <[email protected]>
Co-authored-by: moremind <[email protected]>
Co-authored-by: xcsnx <[email protected]>
  • Loading branch information
4 people authored Oct 26, 2024
1 parent b267b90 commit f153b56
Show file tree
Hide file tree
Showing 21 changed files with 216 additions and 46 deletions.
3 changes: 2 additions & 1 deletion db/init/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,7 @@ CREATE TABLE IF NOT EXISTS `alert_receiver`
`match_all` tinyint(4) NOT NULL COMMENT 'match all or not',
`labels` varchar(255) COMMENT 'labels',
`levels` varchar(255) COMMENT 'levels',
`namespace_id` varchar(50) COMMENT 'namespace id',
`date_created` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT 'create time',
`date_updated` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT 'update time',
PRIMARY KEY (`id`) USING BTREE
Expand Down Expand Up @@ -2429,4 +2430,4 @@ CREATE TABLE IF NOT EXISTS `scale_history`
`date_created` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT 'create time',
`date_updated` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT 'update time',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
3 changes: 2 additions & 1 deletion db/init/ob/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,7 @@ CREATE TABLE IF NOT EXISTS `alert_receiver`
`match_all` tinyint(4) NOT NULL COMMENT 'match all or not',
`labels` varchar(255) COMMENT 'labels',
`levels` varchar(255) COMMENT 'levels',
`namespace_id` varchar(50) COMMENT 'namespace id',
`date_created` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT 'create time',
`date_updated` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT 'update time',
PRIMARY KEY (`id`) USING BTREE
Expand Down Expand Up @@ -2380,4 +2381,4 @@ INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_creat
INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343264', '1346358560427216896', '1792749362445840483', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000');
INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343265', '1346358560427216896', '1792749362445840484', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000');
INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343266', '1346358560427216896', '1792749362445840485', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000');
INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343267', '1346358560427216896', '1792749362445840486', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000');
INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343267', '1346358560427216896', '1792749362445840486', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000');
4 changes: 3 additions & 1 deletion db/init/og/create-table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,7 @@ CREATE TABLE "public"."alert_receiver"
"match_all" int4 NOT NULL,
"labels" varchar(255) COLLATE "pg_catalog"."default",
"levels" varchar(255) COLLATE "pg_catalog"."default",
"namespace_id" varchar(50) COLLATE "pg_catalog"."default" NOT NULL,
"date_created" timestamp(6) NOT NULL DEFAULT timezone('UTC-8'::text, (now())::timestamp(0) without time zone),
"date_updated" timestamp(6) NOT NULL DEFAULT timezone('UTC-8'::text, (now())::timestamp(0) without time zone)
)
Expand All @@ -2540,6 +2541,7 @@ COMMENT ON COLUMN "public"."alert_receiver"."name" IS 'alarm receiver name';
COMMENT ON COLUMN "public"."alert_receiver"."enable" IS 'enable or not';
COMMENT ON COLUMN "public"."alert_receiver"."type" IS 'notice type 0-SMS 1-Email 2-webhook 3-WeChat Official Account 4-Enterprise WeChat Robot 5-DingTalk Robot 6-FeiShu Robot 7-Telegram Bot 8-SlackWebHook 9-Discord Bot 10-Enterprise WeChat';
COMMENT ON COLUMN "public"."alert_receiver"."match_all" IS 'match all or not';
COMMENT ON COLUMN "public"."alert_receiver"."namespace_id" IS 'namespace id';
COMMENT ON COLUMN "public"."alert_receiver"."date_created" IS 'create time';
COMMENT ON COLUMN "public"."alert_receiver"."date_updated" IS 'update time';

Expand Down Expand Up @@ -2771,4 +2773,4 @@ COMMENT ON COLUMN "public"."scale_history"."num" IS 'number of bootstrap';
COMMENT ON COLUMN "public"."scale_history"."action" IS 'status 1:enable 0:disable';
COMMENT ON COLUMN "public"."scale_history"."msg" IS 'message';
COMMENT ON COLUMN "public"."scale_history"."date_created" IS 'create time';
COMMENT ON COLUMN "public"."scale_history"."date_updated" IS 'update time';
COMMENT ON COLUMN "public"."scale_history"."date_updated" IS 'update time';
8 changes: 5 additions & 3 deletions db/init/oracle/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,7 @@ create table alert_receiver
match_all NUMBER(10),
labels varchar(255),
levels varchar(255),
namespace_id varchar(50) not null,
date_created timestamp(3) default SYSDATE not null,
date_updated timestamp(3) default SYSDATE not null,
PRIMARY KEY (id)
Expand Down Expand Up @@ -2717,6 +2718,9 @@ comment
on column alert_receiver.levels
is 'alarm levels';
comment
on column lert_receiver.namespace_id
is 'namespace id';
comment
on column alert_receiver.date_created
is 'create time';
comment
Expand Down Expand Up @@ -2993,6 +2997,4 @@ comment on column SCALE_HISTORY.msg
comment on column SCALE_HISTORY.date_created
is 'create time';
comment on column SCALE_HISTORY.date_updated
is 'update time';


is 'update time';
5 changes: 3 additions & 2 deletions db/init/pg/create-table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,7 @@ CREATE TABLE "public"."alert_receiver"
"match_all" int4 NOT NULL,
"labels" varchar(255) COLLATE "pg_catalog"."default",
"levels" varchar(255) COLLATE "pg_catalog"."default",
"namespace_id" varchar(50) COLLATE "pg_catalog"."default" NOT NULL,
"date_created" timestamp(6) NOT NULL DEFAULT timezone('UTC-8'::text, (now())::timestamp(0) without time zone),
"date_updated" timestamp(6) NOT NULL DEFAULT timezone('UTC-8'::text, (now())::timestamp(0) without time zone)
)
Expand All @@ -2664,6 +2665,7 @@ COMMENT ON COLUMN "public"."alert_receiver"."name" IS 'alarm receiver name';
COMMENT ON COLUMN "public"."alert_receiver"."enable" IS 'enable or not';
COMMENT ON COLUMN "public"."alert_receiver"."type" IS 'notice type 0-SMS 1-Email 2-webhook 3-WeChat Official Account 4-Enterprise WeChat Robot 5-DingTalk Robot 6-FeiShu Robot 7-Telegram Bot 8-SlackWebHook 9-Discord Bot 10-Enterprise WeChat';
COMMENT ON COLUMN "public"."alert_receiver"."match_all" IS 'match all or not';
COMMENT ON COLUMN "public"."discovery_upstream"."namespace_id" IS 'the namespace id';
COMMENT ON COLUMN "public"."alert_receiver"."date_created" IS 'create time';
COMMENT ON COLUMN "public"."alert_receiver"."date_updated" IS 'update time';

Expand Down Expand Up @@ -2891,5 +2893,4 @@ COMMENT ON COLUMN "public"."scale_history"."num" IS 'number of bootstrap';
COMMENT ON COLUMN "public"."scale_history"."action" IS 'status 1:enable 0:disable';
COMMENT ON COLUMN "public"."scale_history"."msg" IS 'message';
COMMENT ON COLUMN "public"."scale_history"."date_created" IS 'create time';
COMMENT ON COLUMN "public"."scale_history"."date_updated" IS 'update time';

COMMENT ON COLUMN "public"."scale_history"."date_updated" IS 'update time';
12 changes: 7 additions & 5 deletions db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ ALTER TABLE `shenyu`.`discovery_upstream` ADD COLUMN `namespace_id` varchar(50)

ALTER TABLE `shenyu`.`proxy_selector` ADD COLUMN `namespace_id` varchar(50) NOT NULL COMMENT 'namespaceId' AFTER `props`;

ALTER TABLE `shenyu`.`alert_receiver` ADD COLUMN `namespace_id` varchar(50) NOT NULL COMMENT 'namespaceId' AFTER `levels`;

UPDATE selector
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;
Expand Down Expand Up @@ -216,6 +218,10 @@ UPDATE proxy_selector
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;

UPDATE alert_receiver
SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385'
WHERE namespace_id IS NULL;

DROP TABLE IF EXISTS `scale_policy`;
CREATE TABLE IF NOT EXISTS `scale_policy`
(
Expand Down Expand Up @@ -260,8 +266,4 @@ CREATE TABLE IF NOT EXISTS `scale_history`
`date_created` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT 'create time',
`date_updated` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT 'update time',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;




) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
8 changes: 6 additions & 2 deletions db/upgrade/2.6.1-upgrade-2.7.0-og.sql
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ COMMENT ON COLUMN "public"."discovery_upstream"."namespace_id" IS 'namespaceId';
ALTER TABLE "public"."proxy_selector" ADD COLUMN namespace_id VARCHAR(50) NOT NULL;
COMMENT ON COLUMN "public"."proxy_selector"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."alert_receiver" ADD COLUMN namespace_id VARCHAR(50) NOT NULL;
COMMENT ON COLUMN "public"."alert_receiver"."namespace_id" IS 'namespaceId';

UPDATE "public"."selector" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

UPDATE "public"."rule" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;
Expand All @@ -230,6 +233,8 @@ UPDATE "public"."discovery_upstream" SET namespace_id = '649330b6-c2d7-4edc-be8e

UPDATE "public"."proxy_selector" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

UPDATE "public"."alert_receiver" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

-- ----------------------------
-- Table structure for scale
-- ----------------------------
Expand Down Expand Up @@ -298,5 +303,4 @@ COMMENT ON COLUMN "public"."scale_history"."num" IS 'number of bootstrap';
COMMENT ON COLUMN "public"."scale_history"."action" IS 'status 1:enable 0:disable';
COMMENT ON COLUMN "public"."scale_history"."msg" IS 'message';
COMMENT ON COLUMN "public"."scale_history"."date_created" IS 'create time';
COMMENT ON COLUMN "public"."scale_history"."date_updated" IS 'update time';

COMMENT ON COLUMN "public"."scale_history"."date_updated" IS 'update time';
8 changes: 6 additions & 2 deletions db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ COMMENT ON COLUMN discovery_upstream.namespace_id IS 'namespaceId';
ALTER TABLE proxy_selector ADD namespace_id VARCHAR2(50) NOT NULL;
COMMENT ON COLUMN proxy_selector.namespace_id IS 'namespaceId';

ALTER TABLE alert_receiver ADD namespace_id VARCHAR2(50) NOT NULL;
COMMENT ON COLUMN alert_receiver.namespace_id IS 'namespaceId';

UPDATE selector SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

UPDATE rule SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;
Expand All @@ -263,6 +266,8 @@ UPDATE discovery_upstream SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb3

UPDATE proxy_selector SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

UPDATE alert_receiver SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

-- ----------------------------
-- Table structure for scale
-- ----------------------------
Expand Down Expand Up @@ -355,5 +360,4 @@ comment on column SCALE_HISTORY.msg
comment on column SCALE_HISTORY.date_created
is 'create time';
comment on column SCALE_HISTORY.date_updated
is 'update time';

is 'update time';
7 changes: 6 additions & 1 deletion db/upgrade/2.6.1-upgrade-2.7.0-pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ COMMENT ON COLUMN "public"."discovery_upstream"."namespace_id" IS 'namespaceId';
ALTER TABLE "public"."proxy_selector" ADD COLUMN namespace_id VARCHAR(50) NOT NULL;
COMMENT ON COLUMN "public"."proxy_selector"."namespace_id" IS 'namespaceId';

ALTER TABLE "public"."alert_receiver" ADD COLUMN namespace_id VARCHAR(50) NOT NULL;
COMMENT ON COLUMN "public"."alert_receiver"."namespace_id" IS 'namespaceId';

UPDATE "public"."selector" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

UPDATE "public"."rule" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;
Expand All @@ -229,6 +232,8 @@ UPDATE "public"."discovery_upstream" SET namespace_id = '649330b6-c2d7-4edc-be8e

UPDATE "public"."proxy_selector" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

UPDATE "public"."alert_receiver" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL;

-- ----------------------------
-- Table structure for scale
-- ----------------------------
Expand Down Expand Up @@ -297,4 +302,4 @@ COMMENT ON COLUMN "public"."scale_history"."num" IS 'number of bootstrap';
COMMENT ON COLUMN "public"."scale_history"."action" IS 'status 1:enable 0:disable';
COMMENT ON COLUMN "public"."scale_history"."msg" IS 'message';
COMMENT ON COLUMN "public"."scale_history"."date_created" IS 'create time';
COMMENT ON COLUMN "public"."scale_history"."date_updated" IS 'update time';
COMMENT ON COLUMN "public"."scale_history"."date_updated" IS 'update time';
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@

package org.apache.shenyu.admin.controller;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import org.apache.shenyu.admin.aspect.annotation.RestApi;
import org.apache.shenyu.admin.mapper.NamespaceMapper;
import org.apache.shenyu.admin.model.page.CommonPager;
import org.apache.shenyu.admin.model.page.PageParameter;
import org.apache.shenyu.admin.model.query.AlertReceiverQuery;
import org.apache.shenyu.admin.model.result.ShenyuAdminResult;
import org.apache.shenyu.admin.service.AlertReceiverService;
import org.apache.shenyu.admin.utils.ShenyuResultMessage;
import org.apache.shenyu.admin.validation.annotation.Existed;
import org.apache.shenyu.alert.model.AlertReceiverDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -35,17 +39,16 @@
import org.springframework.web.bind.annotation.RequestParam;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.util.List;

/**
* Alert Receiver Controller.
*/
@RestApi("/alert/receiver")
public class AlertReceiverController {

@Autowired
private AlertReceiverService alertReceiverService;

Expand Down Expand Up @@ -84,7 +87,7 @@ public ShenyuAdminResult deleteReceiver(@RequestBody @NotEmpty final List<@NotBl
alertReceiverService.deleteReceiver(ids);
return ShenyuAdminResult.success(ShenyuResultMessage.DELETE_SUCCESS);
}

/**
* delete alert receiver.
*
Expand All @@ -101,12 +104,16 @@ public ShenyuAdminResult getReceiverDetail(@PathVariable("id") final String id)
* query receiver.
* @param currentPage current page
* @param pageSize page size
* @param namespaceId namespaceId.
* @return receiver
*/
@GetMapping
public ShenyuAdminResult getReceivers(@RequestParam @NotNull final Integer currentPage,
@RequestParam @NotNull final Integer pageSize) {
CommonPager<AlertReceiverDTO> commonPager = alertReceiverService.listByPage(new AlertReceiverQuery(new PageParameter(currentPage, pageSize)));
@RequestParam @NotNull final Integer pageSize,
@Valid @Existed(message = "namespaceId is not existed",
provider = NamespaceMapper.class) final String namespaceId
) {
CommonPager<AlertReceiverDTO> commonPager = alertReceiverService.listByPage(new AlertReceiverQuery(new PageParameter(currentPage, pageSize), namespaceId));
return ShenyuAdminResult.success(commonPager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ public class AlertReceiverDO {
* match alert labels.
*/
private Map<String, String> labels;


/**
* namespaceId.
*/
private String namespaceId;

/**
* create time.
*/
Expand Down Expand Up @@ -562,7 +567,23 @@ public List<Byte> getLevels() {
public void setLevels(final List<Byte> levels) {
this.levels = levels;
}


/**
* get namespaceId.
* @return namespaceId
*/
public String getNamespaceId() {
return namespaceId;
}

/**
* set namespaceId.
* @param namespaceId namespaceId
*/
public void setNamespaceId(final String namespaceId) {
this.namespaceId = namespaceId;
}

/**
* get dateCreated.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,41 @@ public class AlertReceiverQuery implements Serializable {
*/
private PageParameter pageParameter;

/**
* namespace id.
*/
private String namespaceId;

public AlertReceiverQuery() {
}

public AlertReceiverQuery(final PageParameter pageParameter) {
this.pageParameter = pageParameter;
}


public AlertReceiverQuery(final PageParameter pageParameter, final String namespaceId) {
this.pageParameter = pageParameter;
this.namespaceId = namespaceId;
}

/**
* Gets the value of namespaceId.
*
* @return the value of namespaceId
*/
public String getNamespaceId() {
return namespaceId;
}

/**
* Sets the namespaceId.
*
* @param namespaceId namespaceId
*/
public void setNamespaceId(final String namespaceId) {
this.namespaceId = namespaceId;
}

/**
* Gets the value of pageParameter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.mapper.AlertReceiverMapper;
import org.apache.shenyu.alert.AlertNotifyHandler;
import org.apache.shenyu.alert.exception.AlertNoticeException;
Expand Down Expand Up @@ -156,6 +157,12 @@ private List<AlertReceiverDTO> matchReceiverByRules(final AlarmContent alert) {
return true;
}

if (StringUtils.isNotBlank(item.getNamespaceId())) {
boolean namespaceIdMatch = Objects.equals(item.getNamespaceId(), alert.getNamespaceId());
if (!namespaceIdMatch) {
return false;
}
}
if (!CollectionUtils.isEmpty(item.getLevels())) {
boolean levelMatch = item.getLevels().stream().anyMatch(level -> level == alert.getLevel());
if (!levelMatch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public void addReceiver(final AlertReceiverDTO alertReceiverDTO) {
alertDispatchService.clearCache();
alertReceiverMapper.insert(receiverDO);
}

@Override
public void deleteReceiver(final List<String> ids) {
alertDispatchService.clearCache();
alertReceiverMapper.deleteByIds(ids);
}

@Override
public void updateReceiver(final AlertReceiverDTO alertReceiverDTO) {
AlertReceiverDO receiverDO = AlertTransfer.INSTANCE.mapToAlertReceiverDO(alertReceiverDTO);
Expand Down
Loading

0 comments on commit f153b56

Please sign in to comment.