Skip to content

Commit a9ee92b

Browse files
committed
feat: add metadata to pyrra_objective
1 parent a81d58f commit a9ee92b

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

slo/rules.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ func (o Objective) GenericRules() (monitoringv1.RuleGroup, error) {
11501150
rules = append(rules, monitoringv1.Rule{
11511151
Record: "pyrra_objective",
11521152
Expr: intstr.FromString(strconv.FormatFloat(o.Target, 'f', -1, 64)),
1153-
Labels: ruleLabels,
1153+
Labels: o.genericObjectiveLabels(sloName),
11541154
})
11551155
rules = append(rules, monitoringv1.Rule{
11561156
Record: "pyrra_window",
@@ -1263,7 +1263,7 @@ func (o Objective) GenericRules() (monitoringv1.RuleGroup, error) {
12631263
rules = append(rules, monitoringv1.Rule{
12641264
Record: "pyrra_objective",
12651265
Expr: intstr.FromString(strconv.FormatFloat(o.Target, 'f', -1, 64)),
1266-
Labels: ruleLabels,
1266+
Labels: o.genericObjectiveLabels(sloName),
12671267
})
12681268
rules = append(rules, monitoringv1.Rule{
12691269
Record: "pyrra_window",
@@ -1392,3 +1392,24 @@ func (o Objective) GenericRules() (monitoringv1.RuleGroup, error) {
13921392
Rules: rules,
13931393
}, nil
13941394
}
1395+
1396+
func (o *Objective) genericObjectiveLabels(sloName string) map[string]string {
1397+
labels := o.commonRuleLabels(sloName)
1398+
labels["description"] = o.Description
1399+
1400+
switch o.IndicatorType() {
1401+
case Latency:
1402+
for _, matcher := range o.Indicator.Latency.Success.LabelMatchers {
1403+
if matcher.Name == "le" {
1404+
d, err := time.ParseDuration(matcher.Value + "s") // assume that the value is always in seconds
1405+
if err == nil {
1406+
labels["info"] = fmt.Sprintf("%.3f%% in %s must be faster than %s", o.Target*100, o.Window, d.String())
1407+
}
1408+
}
1409+
}
1410+
case Ratio:
1411+
labels["info"] = fmt.Sprintf("%.3f%% in %s", o.Target*100, o.Window)
1412+
}
1413+
1414+
return labels
1415+
}

slo/rules_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
16301630
Rules: []monitoringv1.Rule{{
16311631
Record: "pyrra_objective",
16321632
Expr: intstr.FromString(`0.99`),
1633-
Labels: map[string]string{"slo": "monitoring-http-errors"},
1633+
Labels: map[string]string{"slo": "monitoring-http-errors", "description": "", "info": "99.000% in 4w"},
16341634
}, {
16351635
Record: "pyrra_window",
16361636
Expr: intstr.FromInt(int((28 * 24 * time.Hour).Seconds())),
@@ -1666,7 +1666,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
16661666
Rules: []monitoringv1.Rule{{
16671667
Record: "pyrra_objective",
16681668
Expr: intstr.FromString(`0.999`),
1669-
Labels: map[string]string{"slo": "monitoring-grpc-errors"},
1669+
Labels: map[string]string{"slo": "monitoring-grpc-errors", "description": "", "info": "99.900% in 4w"},
16701670
}, {
16711671
Record: "pyrra_window",
16721672
Expr: intstr.FromInt(int((28 * 24 * time.Hour).Seconds())),
@@ -1698,7 +1698,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
16981698
Rules: []monitoringv1.Rule{{
16991699
Record: "pyrra_objective",
17001700
Expr: intstr.FromString("0.995"),
1701-
Labels: map[string]string{"slo": "monitoring-http-latency"},
1701+
Labels: map[string]string{"slo": "monitoring-http-latency", "description": "", "info": "99.500% in 4w must be faster than 1s"},
17021702
}, {
17031703
Record: "pyrra_window",
17041704
Expr: intstr.FromInt(2419200),
@@ -1734,7 +1734,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
17341734
Rules: []monitoringv1.Rule{{
17351735
Record: "pyrra_objective",
17361736
Expr: intstr.FromString("0.995"),
1737-
Labels: map[string]string{"slo": "monitoring-grpc-latency"},
1737+
Labels: map[string]string{"slo": "monitoring-grpc-latency", "description": "", "info": "99.500% in 1w must be faster than 600ms"},
17381738
}, {
17391739
Record: "pyrra_window",
17401740
Expr: intstr.FromInt(604800),
@@ -1766,7 +1766,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
17661766
Rules: []monitoringv1.Rule{{
17671767
Record: "pyrra_objective",
17681768
Expr: intstr.FromString("0.99"),
1769-
Labels: map[string]string{"slo": "monitoring-prometheus-operator-errors"},
1769+
Labels: map[string]string{"slo": "monitoring-prometheus-operator-errors", "description": "", "info": "99.000% in 2w"},
17701770
}, {
17711771
Record: "pyrra_window",
17721772
Expr: intstr.FromInt(1209600),
@@ -1798,7 +1798,7 @@ func TestObjective_GrafanaRules(t *testing.T) {
17981798
Rules: []monitoringv1.Rule{{
17991799
Record: "pyrra_objective",
18001800
Expr: intstr.FromString(`0.99`),
1801-
Labels: map[string]string{"slo": "apiserver-write-response-errors"},
1801+
Labels: map[string]string{"slo": "apiserver-write-response-errors", "description": "", "info": "99.000% in 2w"},
18021802
}, {
18031803
Record: "pyrra_window",
18041804
Expr: intstr.FromInt(int((14 * 24 * time.Hour).Seconds())),

0 commit comments

Comments
 (0)