Skip to content

Commit

Permalink
feat(slo): labels and rule naming based on ADR
Browse files Browse the repository at this point in the history
Signed-off-by: Hy3n4 <[email protected]>
  • Loading branch information
Hy3n4 committed Oct 30, 2023
1 parent 01d2a60 commit e8e5b49
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
35 changes: 10 additions & 25 deletions internal/controller/openslo/slo_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,21 @@ func (r *SLOReconciler) createPrometheusRule(slo *openslov1.SLO, sli *openslov1.

// for now, total and good are required. bad is optional and is calculated as (total - good) if not provided
// TODO: validate that the SLO budgeting method is Occurrences and that the SLIs are all ratio metrics in other case throw an error
totalRule.Record = fmt.Sprintf("osko:ratio_indicator_total:rate%s", defaultRateWindow)
totalRule.Record = fmt.Sprintf("osko_sli_ratio_total")
totalRule.Expr = intstr.Parse(fmt.Sprintf("sum(increase(%s[%s]))",
sli.Spec.RatioMetric.Total.MetricSource.Spec,
defaultRateWindow,
))
totalRule.Labels = utils.MergeLabels(
map[string]string{
"metric": utils.ExtractMetricNameFromQuery(sli.Spec.RatioMetric.Total.MetricSource.Spec),
},
slo.Labels,
)
totalRule.Labels = utils.GenerateMetricLabels(slo, sli)

monitoringRules = append(monitoringRules, totalRule)

goodRule.Record = fmt.Sprintf("osko:ratio_indicator_good:rate%s", defaultRateWindow)
goodRule.Record = fmt.Sprintf("osko_sli_ratio_good")
goodRule.Expr = intstr.Parse(fmt.Sprintf("sum(increase(%s[%s]))",
sli.Spec.RatioMetric.Good.MetricSource.Spec,
defaultRateWindow,
))
goodRule.Labels = utils.MergeLabels(
map[string]string{
"metric": utils.ExtractMetricNameFromQuery(sli.Spec.RatioMetric.Good.MetricSource.Spec),
},
slo.Labels,
)
goodRule.Labels = utils.GenerateMetricLabels(slo, sli)

monitoringRules = append(monitoringRules, goodRule)

Expand All @@ -251,17 +241,12 @@ func (r *SLOReconciler) createPrometheusRule(slo *openslov1.SLO, sli *openslov1.
)

if sli.Spec.RatioMetric.Bad != (openslov1.MetricSpec{}) {
badRule.Record = fmt.Sprintf("osko:ratio_indicator_bad:rate%s", defaultRateWindow)
badRule.Record = fmt.Sprint("osko_sli_ratio_bad")
badRule.Expr = intstr.Parse(fmt.Sprintf("sum(increase(%s[%s]))",
sli.Spec.RatioMetric.Bad.MetricSource.Spec,
defaultRateWindow,
))
badRule.Labels = utils.MergeLabels(
map[string]string{
"metric": utils.ExtractMetricNameFromQuery(sli.Spec.RatioMetric.Bad.MetricSource.Spec),
},
slo.Labels,
)
badRule.Labels = utils.GenerateMetricLabels(slo, sli)
basicRuleQuery = fmt.Sprintf("(1-%s) * %s[%s:%s] - %s[%s:%s])",
slo.Spec.Objectives[0].Target,
totalRule.Record,
Expand All @@ -275,15 +260,15 @@ func (r *SLOReconciler) createPrometheusRule(slo *openslov1.SLO, sli *openslov1.
}

mRule := monitoringv1.Rule{
Record: fmt.Sprintf("osko:error_budget:rate%s", slo.Spec.TimeWindow[0].Duration),
Record: fmt.Sprint("osko_error_budget_available"),
Expr: intstr.Parse(fmt.Sprint(basicRuleQuery)),
Labels: utils.MergeLabels(
slo.Labels,
),
Labels: utils.GenerateMetricLabels(slo, sli),
}

monitoringRules = append(monitoringRules, mRule)

// Calculate Error ratios for 1h, 6h

rule := &monitoringv1.PrometheusRule{
TypeMeta: metav1.TypeMeta{
APIVersion: "monitoring.coreos.com/v1",
Expand Down
9 changes: 9 additions & 0 deletions internal/utils/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ func ExtractMetricNameFromQuery(query string) string {
return subStr
}

func GenerateMetricLabels(slo *openslov1.SLO, sli *openslov1.SLI) map[string]string {
return map[string]string{
"sli_name": sli.Name,
"slo_name": slo.Name,
"service": slo.Spec.Service,
"window": string(slo.Spec.TimeWindow[0].Duration),
}
}

func MergeLabels(ms ...map[string]string) map[string]string {
res := map[string]string{}
for _, m := range ms {
Expand Down

0 comments on commit e8e5b49

Please sign in to comment.