@@ -736,6 +736,8 @@ func buildRuleFileContentNew(ruleFileName, guidExpr, addressExpr, ipExpr string,
736
736
}
737
737
738
738
func buildStrategyAlarmRuleExpr (guidExpr , addressExpr , ipExpr string , strategy * models.AlarmStrategyMetricObj ) {
739
+ var logType string
740
+ var err error
739
741
if strings .Contains (strategy .MetricExpr , "$address" ) {
740
742
if strings .Contains (addressExpr , "|" ) {
741
743
strategy .MetricExpr = strings .Replace (strategy .MetricExpr , "=\" $address\" " , "=~\" " + addressExpr + "\" " , - 1 )
@@ -763,18 +765,35 @@ func buildStrategyAlarmRuleExpr(guidExpr, addressExpr, ipExpr string, strategy *
763
765
strategy .MetricExpr = strings .ReplaceAll (strategy .MetricExpr , "$ip" , ipExpr )
764
766
}
765
767
}
768
+ // 看指标是否为 业务配置过来的,查询业务配置类型,自定义类型需要特殊处理 tags,tags="test_service_code=deleteUser,test_retcode=304"
769
+ if logType , err = GetLogTypeByMetric (strategy .Metric ); err != nil {
770
+ log .Logger .Error ("GetLogTypeByMetric err" , log .Error (err ))
771
+ }
766
772
if len (strategy .Tags ) > 0 {
767
- for _ , tagObj := range strategy .Tags {
768
- tagSourceString := "$t_" + tagObj .TagName
769
- if strings .Contains (strategy .MetricExpr , tagSourceString ) {
770
- if len (tagObj .TagValue ) == 0 {
771
- strategy .MetricExpr = strings .Replace (strategy .MetricExpr , "=\" " + tagSourceString + "\" " , "=~\" .*\" " , - 1 )
773
+ if logType == models .LogMonitorCustomType {
774
+ originPromQl := strategy .MetricExpr
775
+ // 原表达式: node_log_metric_monitor_value{key="key",agg="max",service_group="log_sys",tags="$t_tags"}
776
+ // 正则字符串匹配 替换成 node_log_metric_monitor_value{key="key",agg="max",service_group="log_sys",tags!~".*test_service_code=addUser.*"}
777
+ for i , tagObj := range strategy .Tags {
778
+ if i == 0 {
779
+ strategy .MetricExpr = getTagPromQl (convertMetricTag2Dto (tagObj ), originPromQl )
772
780
} else {
773
- tmpEqual := "=~"
774
- if tagObj .Equal == "notin" {
775
- tmpEqual = "!~"
781
+ strategy .MetricExpr = "(" + strategy .MetricExpr + ") and (" + getTagPromQl (convertMetricTag2Dto (tagObj ), originPromQl ) + ")"
782
+ }
783
+ }
784
+ } else {
785
+ for _ , tagObj := range strategy .Tags {
786
+ tagSourceString := "$t_" + tagObj .TagName
787
+ if strings .Contains (strategy .MetricExpr , tagSourceString ) {
788
+ if len (tagObj .TagValue ) == 0 {
789
+ strategy .MetricExpr = strings .Replace (strategy .MetricExpr , "=\" " + tagSourceString + "\" " , "=~\" .*\" " , - 1 )
790
+ } else {
791
+ tmpEqual := "=~"
792
+ if tagObj .Equal == "notin" {
793
+ tmpEqual = "!~"
794
+ }
795
+ strategy .MetricExpr = strings .Replace (strategy .MetricExpr , "=\" " + tagSourceString + "\" " , tmpEqual + "\" " + strings .Join (tagObj .TagValue , "|" )+ "\" " , - 1 )
776
796
}
777
- strategy .MetricExpr = strings .Replace (strategy .MetricExpr , "=\" " + tagSourceString + "\" " , tmpEqual + "\" " + strings .Join (tagObj .TagValue , "|" )+ "\" " , - 1 )
778
797
}
779
798
}
780
799
}
@@ -784,6 +803,17 @@ func buildStrategyAlarmRuleExpr(guidExpr, addressExpr, ipExpr string, strategy *
784
803
}
785
804
}
786
805
806
+ func convertMetricTag2Dto (tag * models.MetricTag ) * models.TagDto {
807
+ if tag == nil {
808
+ return & models.TagDto {}
809
+ }
810
+ return & models.TagDto {
811
+ TagName : tag .TagName ,
812
+ Equal : tag .Equal ,
813
+ TagValue : tag .TagValue ,
814
+ }
815
+ }
816
+
787
817
func copyStrategyListNew (inputs []* models.AlarmStrategyMetricObj ) (result []* models.AlarmStrategyMetricObj ) {
788
818
result = []* models.AlarmStrategyMetricObj {}
789
819
for _ , strategy := range inputs {
0 commit comments