Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions slo/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ func (o Objective) GenericRules() (monitoringv1.RuleGroup, error) {
rules = append(rules, monitoringv1.Rule{
Record: "pyrra_objective",
Expr: intstr.FromString(strconv.FormatFloat(o.Target, 'f', -1, 64)),
Labels: ruleLabels,
Labels: o.genericObjectiveLabels(sloName),
})
rules = append(rules, monitoringv1.Rule{
Record: "pyrra_window",
Expand Down Expand Up @@ -1263,7 +1263,7 @@ func (o Objective) GenericRules() (monitoringv1.RuleGroup, error) {
rules = append(rules, monitoringv1.Rule{
Record: "pyrra_objective",
Expr: intstr.FromString(strconv.FormatFloat(o.Target, 'f', -1, 64)),
Labels: ruleLabels,
Labels: o.genericObjectiveLabels(sloName),
})
rules = append(rules, monitoringv1.Rule{
Record: "pyrra_window",
Expand Down Expand Up @@ -1392,3 +1392,24 @@ func (o Objective) GenericRules() (monitoringv1.RuleGroup, error) {
Rules: rules,
}, nil
}

func (o *Objective) genericObjectiveLabels(sloName string) map[string]string {
labels := o.commonRuleLabels(sloName)
labels["description"] = o.Description

switch o.IndicatorType() {
case Latency:
for _, matcher := range o.Indicator.Latency.Success.LabelMatchers {
if matcher.Name == "le" {
d, err := time.ParseDuration(matcher.Value + "s") // assume that the value is always in seconds
if err == nil {
labels["info"] = fmt.Sprintf("%.3f%% in %s must be faster than %s", o.Target*100, o.Window, d.String())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using %.3f%% formats the float using the 3 decimals. Maybe we can use strconv.FormatFloat(o.Target * 100, 'f', -1, 64) at the end and then use its string in the fmt.Sprintf.

}
}
}
case Ratio:
labels["info"] = fmt.Sprintf("%.3f%% in %s", o.Target*100, o.Window)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

}

return labels
}
12 changes: 6 additions & 6 deletions slo/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
Rules: []monitoringv1.Rule{{
Record: "pyrra_objective",
Expr: intstr.FromString(`0.99`),
Labels: map[string]string{"slo": "monitoring-http-errors"},
Labels: map[string]string{"slo": "monitoring-http-errors", "description": "", "info": "99.000% in 4w"},
}, {
Record: "pyrra_window",
Expr: intstr.FromInt(int((28 * 24 * time.Hour).Seconds())),
Expand Down Expand Up @@ -1666,7 +1666,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
Rules: []monitoringv1.Rule{{
Record: "pyrra_objective",
Expr: intstr.FromString(`0.999`),
Labels: map[string]string{"slo": "monitoring-grpc-errors"},
Labels: map[string]string{"slo": "monitoring-grpc-errors", "description": "", "info": "99.900% in 4w"},
}, {
Record: "pyrra_window",
Expr: intstr.FromInt(int((28 * 24 * time.Hour).Seconds())),
Expand Down Expand Up @@ -1698,7 +1698,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
Rules: []monitoringv1.Rule{{
Record: "pyrra_objective",
Expr: intstr.FromString("0.995"),
Labels: map[string]string{"slo": "monitoring-http-latency"},
Labels: map[string]string{"slo": "monitoring-http-latency", "description": "", "info": "99.500% in 4w must be faster than 1s"},
}, {
Record: "pyrra_window",
Expr: intstr.FromInt(2419200),
Expand Down Expand Up @@ -1734,7 +1734,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
Rules: []monitoringv1.Rule{{
Record: "pyrra_objective",
Expr: intstr.FromString("0.995"),
Labels: map[string]string{"slo": "monitoring-grpc-latency"},
Labels: map[string]string{"slo": "monitoring-grpc-latency", "description": "", "info": "99.500% in 1w must be faster than 600ms"},
}, {
Record: "pyrra_window",
Expr: intstr.FromInt(604800),
Expand Down Expand Up @@ -1766,7 +1766,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
Rules: []monitoringv1.Rule{{
Record: "pyrra_objective",
Expr: intstr.FromString("0.99"),
Labels: map[string]string{"slo": "monitoring-prometheus-operator-errors"},
Labels: map[string]string{"slo": "monitoring-prometheus-operator-errors", "description": "", "info": "99.000% in 2w"},
}, {
Record: "pyrra_window",
Expr: intstr.FromInt(1209600),
Expand Down Expand Up @@ -1798,7 +1798,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
Rules: []monitoringv1.Rule{{
Record: "pyrra_objective",
Expr: intstr.FromString(`0.99`),
Labels: map[string]string{"slo": "apiserver-write-response-errors"},
Labels: map[string]string{"slo": "apiserver-write-response-errors", "description": "", "info": "99.000% in 2w"},
}, {
Record: "pyrra_window",
Expr: intstr.FromInt(int((14 * 24 * time.Hour).Seconds())),
Expand Down