Skip to content

Commit

Permalink
Support PrometheusRule alert description (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
assafad1 authored Oct 10, 2024
1 parent 0d9121d commit 52fb754
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ integration-tests:

.PHONY: e2e-tests
e2e-tests:
go test ./tests/e2e/ -ldflags $(LDFLAGS) -ginkgo.v
go test ./tests/e2e/ -ldflags $(LDFLAGS) -ginkgo.v -v

.PHONY: helm-check-crd-version
helm-check-crd-version:
Expand Down
29 changes: 15 additions & 14 deletions controllers/prometheusrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,26 @@ func prometheusInnerRulesToCoralogixInnerRules(rules []prometheus.Rule) []coralo
return result
}

func prometheusRuleToCoralogixAlertSpec(prometheusRule prometheus.Rule) coralogixv1alpha1.AlertSpec {
func prometheusRuleToCoralogixAlertSpec(rule prometheus.Rule) coralogixv1alpha1.AlertSpec {
return coralogixv1alpha1.AlertSpec{
Severity: getSeverity(prometheusRule),
Description: rule.Annotations["description"],
Severity: getSeverity(rule),
NotificationGroups: []coralogixv1alpha1.NotificationGroup{
{
Notifications: []coralogixv1alpha1.Notification{
{
RetriggeringPeriodMinutes: getNotificationPeriod(prometheusRule),
RetriggeringPeriodMinutes: getNotificationPeriod(rule),
},
},
},
},
Name: prometheusRule.Alert,
Name: rule.Alert,
AlertType: coralogixv1alpha1.AlertType{
Metric: &coralogixv1alpha1.Metric{
Promql: &coralogixv1alpha1.Promql{
SearchQuery: prometheusRule.Expr.StrVal,
SearchQuery: rule.Expr.StrVal,
Conditions: coralogixv1alpha1.PromqlConditions{
TimeWindow: getTimeWindow(prometheusRule),
TimeWindow: getTimeWindow(rule),
AlertWhen: coralogixv1alpha1.PromqlAlertWhenMoreThan,
Threshold: resource.MustParse("0"),
SampleThresholdPercentage: 100,
Expand All @@ -323,36 +324,36 @@ func prometheusRuleToCoralogixAlertSpec(prometheusRule prometheus.Rule) coralogi
},
},
},
Labels: prometheusRule.Labels,
Labels: rule.Labels,
}
}

func getSeverity(prometheusRule prometheus.Rule) coralogixv1alpha1.AlertSeverity {
func getSeverity(rule prometheus.Rule) coralogixv1alpha1.AlertSeverity {
severity := coralogixv1alpha1.AlertSeverityInfo
if severityStr, ok := prometheusRule.Labels["severity"]; ok && severityStr != "" {
if severityStr, ok := rule.Labels["severity"]; ok && severityStr != "" {
severityStr = strings.ToUpper(severityStr[:1]) + strings.ToLower(severityStr[1:])
severity = coralogixv1alpha1.AlertSeverity(severityStr)
}
return severity
}

func getTimeWindow(prometheusRule prometheus.Rule) coralogixv1alpha1.MetricTimeWindow {
if timeWindow, ok := prometheusAlertForToCoralogixPromqlAlertTimeWindow[prometheusRule.For]; ok {
func getTimeWindow(rule prometheus.Rule) coralogixv1alpha1.MetricTimeWindow {
if timeWindow, ok := prometheusAlertForToCoralogixPromqlAlertTimeWindow[rule.For]; ok {
return timeWindow
}
return prometheusAlertForToCoralogixPromqlAlertTimeWindow["1m"]
}

func getNotificationPeriod(prometheusRule prometheus.Rule) int32 {
if cxNotifyEveryMin, ok := prometheusRule.Annotations["cxNotifyEveryMin"]; ok {
func getNotificationPeriod(rule prometheus.Rule) int32 {
if cxNotifyEveryMin, ok := rule.Annotations["cxNotifyEveryMin"]; ok {
if notificationPeriod, err := strconv.Atoi(cxNotifyEveryMin); err == nil {
if notificationPeriod > 0 {
return int32(notificationPeriod)
}
}
}

if duration, err := time.ParseDuration(string(prometheusRule.For)); err == nil {
if duration, err := time.ParseDuration(string(rule.For)); err == nil {
notificationPeriod := int(duration.Minutes())
if notificationPeriod > 0 {
return int32(notificationPeriod)
Expand Down

0 comments on commit 52fb754

Please sign in to comment.