Skip to content

Commit

Permalink
patch - adding MoreThanUsual to promql alert condition (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrNovo authored Aug 30, 2023
1 parent 435f1d5 commit 63c734b
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 13 deletions.
21 changes: 18 additions & 3 deletions apis/coralogix/v1alpha1/alert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`

Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/coralogix.com_alerts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ spec:
enum:
- More
- Less
- MoreThanUsual
type: string
manageUndetectedValues:
properties:
Expand Down Expand Up @@ -1092,6 +1093,7 @@ spec:
enum:
- More
- Less
- MoreThanUsual
type: string
manageUndetectedValues:
properties:
Expand Down
20 changes: 13 additions & 7 deletions controllers/alphacontrollers/alert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion controllers/prometheusrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ AlertSpec defines the desired state of Alert
<td>
<br/>
<br/>
<i>Enum</i>: More, Less<br/>
<i>Enum</i>: More, Less, MoreThanUsual<br/>
</td>
<td>true</td>
</tr><tr>
Expand Down Expand Up @@ -2992,7 +2992,7 @@ AlertStatus defines the observed state of Alert
<td>
<br/>
<br/>
<i>Enum</i>: More, Less<br/>
<i>Enum</i>: More, Less, MoreThanUsual<br/>
</td>
<td>true</td>
</tr><tr>
Expand Down
50 changes: 50 additions & 0 deletions tests/e2e/alerts/promql/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions tests/e2e/alerts/promql/01-promql-install.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 63c734b

Please sign in to comment.