File tree Expand file tree Collapse file tree 2 files changed +24
-6
lines changed
src/main/java/dev/vality/alerting/mayday/prometheus/client/k8s Expand file tree Collapse file tree 2 files changed +24
-6
lines changed Original file line number Diff line number Diff line change 1616import org .springframework .http .HttpStatus ;
1717import org .springframework .stereotype .Component ;
1818
19- import java .util .Optional ;
20- import java .util .Set ;
19+ import java .util .*;
2120import java .util .function .UnaryOperator ;
2221import java .util .stream .Collectors ;
2322
@@ -64,11 +63,15 @@ public Set<PrometheusRuleSpec.Rule> getPrometheusRuleGroupAlerts(String ruleName
6463 if (rule == null ) {
6564 return Set .of ();
6665 }
67- return rule .getSpec ().getGroups ().stream ()
66+ List < PrometheusRuleSpec . Rule > rules = rule .getSpec ().getGroups ().stream ()
6867 .flatMap (group -> group .getRules ().stream ()
6968 .filter (rule1 -> rule1 .getLabels ().containsKey (PrometheusRuleLabel .USERNAME )
7069 && rule1 .getLabels ().get (PrometheusRuleLabel .USERNAME ).equals (groupName )))
71- .collect (Collectors .toSet ());
70+ .toList ();
71+
72+ Map <String , PrometheusRuleSpec .Rule > distinctRules = new HashMap <>();
73+ rules .forEach (rule1 -> distinctRules .put (rule1 .getAlert (), rule1 ));
74+ return new HashSet <>(distinctRules .values ());
7275 }
7376 }
7477
Original file line number Diff line number Diff line change 55import dev .vality .alerting .mayday .prometheus .client .k8s .model .PrometheusRuleSpec ;
66import lombok .experimental .UtilityClass ;
77import lombok .extern .slf4j .Slf4j ;
8+ import org .springframework .util .ObjectUtils ;
89
910import java .util .HashSet ;
1011import java .util .Set ;
@@ -69,8 +70,22 @@ public static UnaryOperator<PrometheusRule> getAddAlertToGroupFunc(String groupN
6970 groups = new HashSet <>();
7071 prometheusRule .getSpec ().setGroups (groups );
7172 }
72- PrometheusRuleSpec .Group group = groups .stream ().filter (g -> g .getName ().equals (groupName )).findFirst ()
73- .orElse (createPrometheusRuleGroup (groupName ));
73+
74+ var groupIterator = groups .iterator ();
75+ PrometheusRuleSpec .Group group = null ;
76+ while (groupIterator .hasNext ()) {
77+ var cursor = groupIterator .next ();
78+ log .info ("Found group with name '{}'..." , cursor .getName ());
79+ if (cursor .getName ().equals (groupName )) {
80+ log .info ("Found matching group with name '{}'! Group: {}" , cursor .getName (), cursor );
81+ group = cursor ;
82+ break ;
83+ }
84+ }
85+
86+ if (ObjectUtils .isEmpty (group )) {
87+ group = createPrometheusRuleGroup (groupName );
88+ }
7489 groups .add (group );
7590 var rules = group .getRules ();
7691 if (rules == null ) {
You can’t perform that action at this time.
0 commit comments