[fix] allow long threshold expr when binding many monitors (#4330)

This commit is contained in:
NekoPunch
2026-08-26 12:13:20 +08:00
committed by GitHub
parent b0d8516822
commit eda420d118
5 changed files with 163 additions and 2 deletions
@@ -69,8 +69,8 @@ public class AlertDefine {
private String type;
@Schema(title = "Alarm Threshold Expr", example = "usage>90", accessMode = READ_WRITE)
@Size(max = 2048)
@Column(length = 2048)
@Size(max = 65535)
@Column(columnDefinition = "TEXT")
private String expr;
@Schema(title = "Execution Period/ Window Size (seconds) - For periodic rules/ For log realtime", example = "300")
@@ -0,0 +1,72 @@
/*
* 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.common.entity.alerter;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Test case for {@link AlertDefine}
*/
class AlertDefineTest {
private final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
private final Validator validator = factory.getValidator();
@Test
void exprShouldAllowBindingManyMonitors() {
String boundMonitors = LongStream.range(0, 76)
.mapToObj(id -> "equals(__instance__, \"" + (653868108767488L + id) + "\")")
.collect(Collectors.joining(" or "));
String expr = "equals(__app__,\"ping\") && equals(__available__,\"down\") && (" + boundMonitors + ")";
assertTrue(expr.length() > 2048);
AlertDefine define = AlertDefine.builder()
.name("ping-offline")
.type("realtime_metric")
.expr(expr)
.template("instance {{ $labels.instance }} is offline")
.build();
Set<ConstraintViolation<AlertDefine>> violations = validator.validate(define);
assertTrue(violations.isEmpty(), "binding many monitors should not fail validation: " + violations);
}
@Test
void oversizedExprShouldReportCharacterLength() {
AlertDefine define = AlertDefine.builder()
.name("ping-offline")
.type("realtime_metric")
.expr("x".repeat(65536))
.template("template")
.build();
Set<ConstraintViolation<AlertDefine>> violations = validator.validate(define);
assertEquals(1, violations.size());
assertEquals("expr", violations.iterator().next().getPropertyPath().toString());
}
}
@@ -0,0 +1,19 @@
-- 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.
-- Enlarge alert define expr to fit rules binding many monitors (#4171)
ALTER TABLE HZB_ALERT_DEFINE ALTER COLUMN expr CLOB;
@@ -0,0 +1,50 @@
-- 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.
-- Enlarge alert define expr to fit rules binding many monitors (#4171)
DELIMITER //
CREATE PROCEDURE ModifyAlertDefineExprColumn()
BEGIN
DECLARE table_exists INT;
DECLARE col_exists INT;
SELECT COUNT(*) INTO table_exists
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'hzb_alert_define';
IF table_exists = 1 THEN
SELECT COUNT(*) INTO col_exists
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'hzb_alert_define'
AND COLUMN_NAME = 'expr'
AND DATA_TYPE != 'longtext';
IF col_exists = 1 THEN
ALTER TABLE hzb_alert_define MODIFY COLUMN expr LONGTEXT;
END IF;
END IF;
END //
DELIMITER ;
CALL ModifyAlertDefineExprColumn();
DROP PROCEDURE IF EXISTS ModifyAlertDefineExprColumn;
COMMIT;
@@ -0,0 +1,20 @@
-- 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.
-- Enlarge alert define expr to fit rules binding many monitors (#4171)
ALTER TABLE HZB_ALERT_DEFINE ALTER COLUMN expr TYPE TEXT;
commit;