From 63c734b4e53064928880442ba1b37fb61d931ded Mon Sep 17 00:00:00 2001 From: Or Novogroder <108669655+OrNovo@users.noreply.github.com> Date: Wed, 30 Aug 2023 08:46:10 +0300 Subject: [PATCH] patch - adding MoreThanUsual to promql alert condition (#76) --- apis/coralogix/v1alpha1/alert_types.go | 21 ++++++-- config/crd/bases/coralogix.com_alerts.yaml | 2 + .../alphacontrollers/alert_controller.go | 20 +++++--- controllers/prometheusrule_controller.go | 2 +- docs/api.md | 4 +- tests/e2e/alerts/promql/01-assert.yaml | 50 +++++++++++++++++++ .../e2e/alerts/promql/01-promql-install.yaml | 45 +++++++++++++++++ 7 files changed, 131 insertions(+), 13 deletions(-) create mode 100644 tests/e2e/alerts/promql/01-assert.yaml create mode 100644 tests/e2e/alerts/promql/01-promql-install.yaml diff --git a/apis/coralogix/v1alpha1/alert_types.go b/apis/coralogix/v1alpha1/alert_types.go index 483488a..aac5240 100644 --- a/apis/coralogix/v1alpha1/alert_types.go +++ b/apis/coralogix/v1alpha1/alert_types.go @@ -472,18 +472,24 @@ func expandPromqlCondition(conditions *PromqlConditions, searchQuery string) *al } switch conditions.AlertWhen { - case "More": + case PromqlAlertWhenMoreThan: return &alerts.AlertCondition{ Condition: &alerts.AlertCondition_MoreThan{ MoreThan: &alerts.MoreThanCondition{Parameters: parameters}, }, } - case "Less": + case PromqlAlertWhenLessThan: return &alerts.AlertCondition{ Condition: &alerts.AlertCondition_LessThan{ LessThan: &alerts.LessThanCondition{Parameters: parameters}, }, } + case PromqlAlertWhenMoreThanUsual: + return &alerts.AlertCondition{ + Condition: &alerts.AlertCondition_MoreThanUsual{ + MoreThanUsual: &alerts.MoreThanUsualCondition{Parameters: parameters}, + }, + } } return nil @@ -2399,7 +2405,7 @@ func (in *LuceneConditions) DeepEqual(actualCondition LuceneConditions) (bool, u } type PromqlConditions struct { - AlertWhen AlertWhen `json:"alertWhen"` + AlertWhen PromqlAlertWhen `json:"alertWhen"` Threshold resource.Quantity `json:"threshold"` @@ -2535,6 +2541,15 @@ const ( AlertWhenMoreThan AlertWhen = "More" ) +// +kubebuilder:validation:Enum=More;Less;MoreThanUsual +type PromqlAlertWhen string + +const ( + PromqlAlertWhenLessThan PromqlAlertWhen = "Less" + PromqlAlertWhenMoreThan PromqlAlertWhen = "More" + PromqlAlertWhenMoreThanUsual PromqlAlertWhen = "MoreThanUsual" +) + // +kubebuilder:validation:Enum=More;Less;Immediately;MoreThanUsual type StandardAlertWhen string diff --git a/config/crd/bases/coralogix.com_alerts.yaml b/config/crd/bases/coralogix.com_alerts.yaml index 966d3d4..4558ff0 100644 --- a/config/crd/bases/coralogix.com_alerts.yaml +++ b/config/crd/bases/coralogix.com_alerts.yaml @@ -193,6 +193,7 @@ spec: enum: - More - Less + - MoreThanUsual type: string manageUndetectedValues: properties: @@ -1092,6 +1093,7 @@ spec: enum: - More - Less + - MoreThanUsual type: string manageUndetectedValues: properties: diff --git a/controllers/alphacontrollers/alert_controller.go b/controllers/alphacontrollers/alert_controller.go index 1cedf70..5b60e5b 100644 --- a/controllers/alphacontrollers/alert_controller.go +++ b/controllers/alphacontrollers/alert_controller.go @@ -594,26 +594,32 @@ func flattenMetricAlert(filters *alerts.AlertFilters, condition *alerts.AlertCon metric := new(coralogixv1alpha1.Metric) var conditionParams *alerts.ConditionParameters - var alertWhen coralogixv1alpha1.AlertWhen + var promqlAlertWhen coralogixv1alpha1.PromqlAlertWhen + var luceneAlertWhen coralogixv1alpha1.AlertWhen switch condition := condition.GetCondition().(type) { case *alerts.AlertCondition_LessThan: - alertWhen = coralogixv1alpha1.AlertWhenLessThan + promqlAlertWhen = coralogixv1alpha1.PromqlAlertWhenLessThan + luceneAlertWhen = coralogixv1alpha1.AlertWhenLessThan conditionParams = condition.LessThan.GetParameters() case *alerts.AlertCondition_MoreThan: conditionParams = condition.MoreThan.GetParameters() - alertWhen = coralogixv1alpha1.AlertWhenMoreThan + promqlAlertWhen = coralogixv1alpha1.PromqlAlertWhenMoreThan + luceneAlertWhen = coralogixv1alpha1.AlertWhenMoreThan + case *alerts.AlertCondition_MoreThanUsual: + conditionParams = condition.MoreThanUsual.GetParameters() + promqlAlertWhen = coralogixv1alpha1.PromqlAlertWhenMoreThanUsual } if promqlParams := conditionParams.GetMetricAlertPromqlParameters(); promqlParams != nil { - metric.Promql = flattenPromqlAlert(conditionParams, promqlParams, alertWhen) + metric.Promql = flattenPromqlAlert(conditionParams, promqlParams, promqlAlertWhen) } else { - metric.Lucene = flattenLuceneAlert(conditionParams, filters.GetText(), alertWhen) + metric.Lucene = flattenLuceneAlert(conditionParams, filters.GetText(), luceneAlertWhen) } return metric } -func flattenPromqlAlert(conditionParams *alerts.ConditionParameters, promqlParams *alerts.MetricAlertPromqlConditionParameters, alertWhen coralogixv1alpha1.AlertWhen) *coralogixv1alpha1.Promql { +func flattenPromqlAlert(conditionParams *alerts.ConditionParameters, promqlParams *alerts.MetricAlertPromqlConditionParameters, alertWhen coralogixv1alpha1.PromqlAlertWhen) *coralogixv1alpha1.Promql { promql := new(coralogixv1alpha1.Promql) promql.SearchQuery = promqlParams.GetPromqlText().GetValue() @@ -630,7 +636,7 @@ func flattenPromqlAlert(conditionParams *alerts.ConditionParameters, promqlParam *promql.Conditions.MinNonNullValuesPercentage = int(minNonNullValuesPercentage.GetValue()) } - if alertWhen == coralogixv1alpha1.AlertWhenLessThan { + if alertWhen == coralogixv1alpha1.PromqlAlertWhenLessThan { if actualManageUndetectedValues := conditionParams.GetRelatedExtendedData(); actualManageUndetectedValues != nil { actualShouldTriggerDeadman, actualCleanupDeadmanDuration := actualManageUndetectedValues.GetShouldTriggerDeadman().GetValue(), actualManageUndetectedValues.GetCleanupDeadmanDuration() autoRetireRatio := alertProtoAutoRetireRatioToSchemaAutoRetireRatio[actualCleanupDeadmanDuration] diff --git a/controllers/prometheusrule_controller.go b/controllers/prometheusrule_controller.go index 25bf4c8..1f79195 100644 --- a/controllers/prometheusrule_controller.go +++ b/controllers/prometheusrule_controller.go @@ -286,7 +286,7 @@ func prometheusInnerRuleToCoralogixAlert(prometheusRule prometheus.Rule) coralog SearchQuery: prometheusRule.Expr.StrVal, Conditions: coralogixv1alpha1.PromqlConditions{ TimeWindow: timeWindow, - AlertWhen: coralogixv1alpha1.AlertWhenMoreThan, + AlertWhen: coralogixv1alpha1.PromqlAlertWhenMoreThan, Threshold: resource.MustParse("0"), SampleThresholdPercentage: 100, MinNonNullValuesPercentage: pointer.Int(0), diff --git a/docs/api.md b/docs/api.md index 88fc331..d89b3f8 100644 --- a/docs/api.md +++ b/docs/api.md @@ -730,7 +730,7 @@ AlertSpec defines the desired state of Alert

- Enum: More, Less
+ Enum: More, Less, MoreThanUsual
true @@ -2992,7 +2992,7 @@ AlertStatus defines the observed state of Alert

- Enum: More, Less
+ Enum: More, Less, MoreThanUsual
true diff --git a/tests/e2e/alerts/promql/01-assert.yaml b/tests/e2e/alerts/promql/01-assert.yaml new file mode 100644 index 0000000..614ecc0 --- /dev/null +++ b/tests/e2e/alerts/promql/01-assert.yaml @@ -0,0 +1,50 @@ +apiVersion: coralogix.com/v1alpha1 +kind: Alert +metadata: + labels: + app.kubernetes.io/name: alert + app.kubernetes.io/instance: alert-sample + app.kubernetes.io/part-of: coralogix-operator + app.kuberentes.io/managed-by: kustomize + app.kubernetes.io/created-by: coralogix-operator + name: promql-alert-example-2 +status: + active: true + alertType: + metric: + promql: + conditions: + alertWhen: MoreThanUsual + minNonNullValuesPercentage: 10 + sampleThresholdPercentage: 50 + threshold: "3" + timeWindow: TwelveHours + searchQuery: http_requests_total{status!~\"4..\"} + description: alert from k8s operator + name: promql alert example + labels: + managed-by: coralogix-operator + # notificationGroups: + # - notifications: + # - notifyOn: TriggeredOnly + # integrationName: WebhookAlerts + # retriggeringPeriodMinutes: 1 + # - notifyOn: TriggeredAndResolved + # emailRecipients: [ "example@coralogix.com" ] + # retriggeringPeriodMinutes: 1440 + # - groupByFields: [ "coralogix.metadata.sdkId" ] + # notifications: + # - notifyOn: TriggeredOnly + # integrationName: WebhookAlerts + # retriggeringPeriodMinutes: 1 + # - notifyOn: TriggeredAndResolved + # emailRecipients: [ "example2@coralogix.com" ] + # retriggeringPeriodMinutes: 1440 + scheduling: + daysEnabled: + - Wednesday + - Thursday + endTime: "20:30" + startTime: "08:30" + timeZone: UTC+02 + severity: Critical diff --git a/tests/e2e/alerts/promql/01-promql-install.yaml b/tests/e2e/alerts/promql/01-promql-install.yaml new file mode 100644 index 0000000..d4dcb06 --- /dev/null +++ b/tests/e2e/alerts/promql/01-promql-install.yaml @@ -0,0 +1,45 @@ +apiVersion: coralogix.com/v1alpha1 +kind: Alert +metadata: + labels: + app.kubernetes.io/name: alert + app.kubernetes.io/instance: alert-sample + app.kubernetes.io/part-of: coralogix-operator + app.kuberentes.io/managed-by: kustomize + app.kubernetes.io/created-by: coralogix-operator + name: promql-alert-example-2 +spec: + name: promql alert example + description: alert from k8s operator + severity: Critical + notificationGroups: + - notifications: + - notifyOn: TriggeredOnly + integrationName: WebhookAlerts + retriggeringPeriodMinutes: 1 + - notifyOn: TriggeredAndResolved + emailRecipients: [ "example@coralogix.com" ] + retriggeringPeriodMinutes: 1440 + - groupByFields: [ "coralogix.metadata.sdkId" ] + notifications: + - notifyOn: TriggeredOnly + integrationName: WebhookAlerts + retriggeringPeriodMinutes: 1 + - notifyOn: TriggeredAndResolved + emailRecipients: [ "example2@coralogix.com" ] + retriggeringPeriodMinutes: 1440 + scheduling: + daysEnabled: ["Wednesday", "Thursday"] + timeZone: UTC+02 + startTime: 08:30 + endTime: 20:30 + alertType: + metric: + promql: + searchQuery: http_requests_total{status!~\"4..\"} + conditions: + alertWhen: MoreThanUsual + threshold: 3 + sampleThresholdPercentage: 50 + timeWindow: TwelveHours + minNonNullValuesPercentage: 10