Skip to content

Commit 390934f

Browse files
committed
Improve conventions
Signed-off-by: Xabier Larrakoetxea <me@slok.dev>
1 parent 6bd6397 commit 390934f

11 files changed

Lines changed: 80 additions & 70 deletions

File tree

internal/plugin/slo/contrib/denominator_corrected_rules_v1/plugin.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const (
2222
PluginID = "sloth.dev/contrib/denominator_corrected_rules/v1"
2323
)
2424

25+
const (
26+
numeratorCorrectionMetric = "slo:numerator_correction:ratio" // The correction factor metric name.
27+
)
28+
2529
type PluginConfig struct{}
2630

2731
func NewPlugin(c json.RawMessage, _ pluginslov1.AppUtils) (pluginslov1.Plugin, error) {
@@ -109,7 +113,7 @@ func getAlertGroupWindows(alerts model.MWMBAlertGroup) []time.Duration {
109113

110114
func denominatorCorrectedSLIRecordGenerator(slo model.PromSLO, window time.Duration, alerts model.MWMBAlertGroup) (*rulefmt.Rule, error) {
111115
const sliExprTplFmt = `(
112-
slo:numerator_correction:ratio{{.window}}{{.filter}}
116+
{{.numeratorCorrectionMetric}}{{.window}}{{.filter}}
113117
* on()
114118
%s
115119
)
@@ -129,9 +133,10 @@ slo:numerator_correction:ratio{{.window}}{{.filter}}
129133

130134
var b bytes.Buffer
131135
err = tpl.Execute(&b, map[string]string{
132-
tplKeyWindow: strWindow,
133-
"filter": promutils.LabelsToPromFilter(conventions.GetSLOIDPromLabels(slo)),
134-
"windowKey": conventions.PromSLOWindowLabelName,
136+
"numeratorCorrectionMetric": numeratorCorrectionMetric,
137+
"window": strWindow,
138+
"filter": promutils.LabelsToPromFilter(conventions.GetSLOIDPromLabels(slo)),
139+
"windowKey": conventions.PromSLOWindowLabelName,
135140
})
136141
if err != nil {
137142
return nil, fmt.Errorf("could not render SLI expression template: %w", err)
@@ -150,13 +155,9 @@ slo:numerator_correction:ratio{{.window}}{{.filter}}
150155
}, nil
151156
}
152157

153-
const (
154-
tplKeyWindow = "window"
155-
)
156-
157158
func createNumeratorCorrection(slo model.PromSLO, labels map[string]string, currentWindow, totalWindow time.Duration) (*rulefmt.Rule, error) {
158159
windowString := promutils.TimeDurationToPromStr(currentWindow)
159-
metricSLONumeratorCorrection := fmt.Sprintf("slo:numerator_correction:ratio%s", windowString)
160+
metricSLONumeratorCorrection := numeratorCorrectionMetric + windowString
160161

161162
tpl, err := template.New("sliExpr").Option("missingkey=error").Parse(slo.SLI.Events.TotalQuery)
162163
if err != nil {
@@ -165,7 +166,7 @@ func createNumeratorCorrection(slo model.PromSLO, labels map[string]string, curr
165166

166167
var numeratorBuffer bytes.Buffer
167168
err = tpl.Execute(&numeratorBuffer, map[string]string{
168-
tplKeyWindow: windowString,
169+
conventions.TplSLIQueryWindowVarName: windowString,
169170
})
170171
if err != nil {
171172
return nil, fmt.Errorf("could not create numerator for %s: %w", metricSLONumeratorCorrection, err)
@@ -174,7 +175,7 @@ func createNumeratorCorrection(slo model.PromSLO, labels map[string]string, curr
174175
denominatorWindow := promutils.TimeDurationToPromStr(totalWindow)
175176
var denominatorBuffer bytes.Buffer
176177
err = tpl.Execute(&denominatorBuffer, map[string]string{
177-
tplKeyWindow: denominatorWindow,
178+
conventions.TplSLIQueryWindowVarName: denominatorWindow,
178179
})
179180
if err != nil {
180181
return nil, fmt.Errorf("could not create denominator for %s: %w", metricSLONumeratorCorrection, err)

internal/plugin/slo/contrib/error_budget_exhausted_alert_v1/plugin.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/prometheus/common/model"
1010
"github.com/prometheus/prometheus/model/rulefmt"
1111

12+
"github.com/slok/sloth/pkg/common/conventions"
1213
pluginslov1 "github.com/slok/sloth/pkg/prometheus/plugin/slo/v1"
1314
)
1415

@@ -66,9 +67,9 @@ func (p plugin) ProcessSLO(_ context.Context, req *pluginslov1.Request, result *
6667

6768
// Base labels for the alert
6869
labels := map[string]string{
69-
"sloth_slo": slo.Name,
70-
"sloth_service": slo.Service,
71-
"sloth_id": fmt.Sprintf("%s-%s", slo.Service, slo.Name),
70+
conventions.PromSLONameLabelName: slo.Name,
71+
conventions.PromSLOServiceLabelName: slo.Service,
72+
conventions.PromSLOIDLabelName: fmt.Sprintf("%s-%s", slo.Service, slo.Name),
7273
}
7374

7475
// Add all SLO custom labels
@@ -81,7 +82,7 @@ func (p plugin) ProcessSLO(_ context.Context, req *pluginslov1.Request, result *
8182
labels[k] = v
8283
}
8384

84-
expr := fmt.Sprintf(`slo:period_error_budget_remaining:ratio%s <= %g`, labelMatcher(labels), p.config.Threshold)
85+
expr := fmt.Sprintf(`%s%s <= %g`, conventions.PromMetaSLOPeriodErrorBudgetRemainingRatioMetric, labelMatcher(labels), p.config.Threshold)
8586

8687
// Alert annotations mixed in too
8788
annotations := make(map[string]string)

internal/plugin/slo/contrib/info_labels_v1/plugin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77

8+
"github.com/slok/sloth/pkg/common/conventions"
89
utilsdata "github.com/slok/sloth/pkg/common/utils/data"
910
pluginslov1 "github.com/slok/sloth/pkg/prometheus/plugin/slo/v1"
1011
)
@@ -27,7 +28,7 @@ func NewPlugin(configData json.RawMessage, _ pluginslov1.AppUtils) (pluginslov1.
2728
}
2829

2930
if config.MetricName == "" {
30-
config.MetricName = "sloth_slo_info" // Default info label.
31+
config.MetricName = conventions.PromMetaSLOInfoMetric
3132
}
3233

3334
if len(config.Labels) == 0 {

internal/plugin/slo/contrib/validate_victoria_metrics_v1/plugin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/VictoriaMetrics/metricsql"
1111

12+
"github.com/slok/sloth/pkg/common/conventions"
1213
"github.com/slok/sloth/pkg/common/validation"
1314
pluginslov1 "github.com/slok/sloth/pkg/prometheus/plugin/slo/v1"
1415
)
@@ -46,7 +47,7 @@ type victoriaMetricsDialectValidator struct {
4647
promValidator validation.SLODialectValidator
4748
}
4849

49-
var promExprTplAllowedFakeData = map[string]string{"window": "1m"}
50+
var promExprTplAllowedFakeData = map[string]string{conventions.TplSLIQueryWindowVarName: "1m"}
5051

5152
func (v victoriaMetricsDialectValidator) ValidateLabelKey(key string) error {
5253
return v.promValidator.ValidateLabelKey(key)

internal/plugin/slo/core/metadata_rules_v1/plugin.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ func (plugin) ProcessSLO(ctx context.Context, request *pluginslov1.Request, resu
3939
func generateMetadataRecordingRules(ctx context.Context, info model.Info, slo model.PromSLO, alerts model.MWMBAlertGroup) ([]rulefmt.Rule, error) {
4040
labels := utilsdata.MergeLabels(conventions.GetSLOIDPromLabels(slo), slo.Labels)
4141

42-
// Metatada Recordings.
43-
const (
44-
metricSLOObjectiveRatio = "slo:objective:ratio"
45-
metricSLOErrorBudgetRatio = "slo:error_budget:ratio"
46-
metricSLOTimePeriodDays = "slo:time_period:days"
47-
metricSLOCurrentBurnRateRatio = "slo:current_burn_rate:ratio"
48-
metricSLOPeriodBurnRateRatio = "slo:period_burn_rate:ratio"
49-
metricSLOPeriodErrorBudgetRemainingRatio = "slo:period_error_budget_remaining:ratio"
50-
metricSLOInfo = "sloth_slo_info"
51-
)
52-
5342
sloObjectiveRatio := slo.Objective / 100
5443

5544
sloFilter := promutils.LabelsToPromFilter(conventions.GetSLOIDPromLabels(slo))
@@ -61,7 +50,7 @@ func generateMetadataRecordingRules(ctx context.Context, info model.Info, slo mo
6150
"SLOIDName": conventions.PromSLOIDLabelName,
6251
"SLOLabelName": conventions.PromSLONameLabelName,
6352
"SLOServiceName": conventions.PromSLOServiceLabelName,
64-
"ErrorBudgetRatioMetric": metricSLOErrorBudgetRatio,
53+
"ErrorBudgetRatioMetric": conventions.PromMetaSLOErrorBudgetRatioMetric,
6554
})
6655
if err != nil {
6756
return nil, fmt.Errorf("could not render current burn rate prometheus metadata recording rule expression: %w", err)
@@ -74,7 +63,7 @@ func generateMetadataRecordingRules(ctx context.Context, info model.Info, slo mo
7463
"SLOIDName": conventions.PromSLOIDLabelName,
7564
"SLOLabelName": conventions.PromSLONameLabelName,
7665
"SLOServiceName": conventions.PromSLOServiceLabelName,
77-
"ErrorBudgetRatioMetric": metricSLOErrorBudgetRatio,
66+
"ErrorBudgetRatioMetric": conventions.PromMetaSLOErrorBudgetRatioMetric,
7867
})
7968
if err != nil {
8069
return nil, fmt.Errorf("could not render period burn rate prometheus metadata recording rule expression: %w", err)
@@ -83,49 +72,49 @@ func generateMetadataRecordingRules(ctx context.Context, info model.Info, slo mo
8372
rules := []rulefmt.Rule{
8473
// SLO Objective.
8574
{
86-
Record: metricSLOObjectiveRatio,
75+
Record: conventions.PromMetaSLOObjectiveRatioMetric,
8776
Expr: fmt.Sprintf(`vector(%g)`, sloObjectiveRatio),
8877
Labels: labels,
8978
},
9079

9180
// Error budget.
9281
{
93-
Record: metricSLOErrorBudgetRatio,
82+
Record: conventions.PromMetaSLOErrorBudgetRatioMetric,
9483
Expr: fmt.Sprintf(`vector(1-%g)`, sloObjectiveRatio),
9584
Labels: labels,
9685
},
9786

9887
// Total period.
9988
{
100-
Record: metricSLOTimePeriodDays,
89+
Record: conventions.PromMetaSLOTimePeriodDaysMetric,
10190
Expr: fmt.Sprintf(`vector(%g)`, slo.TimeWindow.Hours()/24),
10291
Labels: labels,
10392
},
10493

10594
// Current burning speed.
10695
{
107-
Record: metricSLOCurrentBurnRateRatio,
96+
Record: conventions.PromMetaSLOCurrentBurnRateRatioMetric,
10897
Expr: currentBurnRateExpr.String(),
10998
Labels: labels,
11099
},
111100

112101
// Total period burn rate.
113102
{
114-
Record: metricSLOPeriodBurnRateRatio,
103+
Record: conventions.PromMetaSLOPeriodBurnRateRatioMetric,
115104
Expr: periodBurnRateExpr.String(),
116105
Labels: labels,
117106
},
118107

119108
// Total Error budget remaining period.
120109
{
121-
Record: metricSLOPeriodErrorBudgetRemainingRatio,
122-
Expr: fmt.Sprintf(`1 - %s%s`, metricSLOPeriodBurnRateRatio, sloFilter),
110+
Record: conventions.PromMetaSLOPeriodErrorBudgetRemainingRatioMetric,
111+
Expr: fmt.Sprintf(`1 - %s%s`, conventions.PromMetaSLOPeriodBurnRateRatioMetric, sloFilter),
123112
Labels: labels,
124113
},
125114

126115
// Info.
127116
{
128-
Record: metricSLOInfo,
117+
Record: conventions.PromMetaSLOInfoMetric,
129118
Expr: `vector(1)`,
130119
Labels: utilsdata.MergeLabels(labels, map[string]string{
131120
conventions.PromSLOVersionLabelName: info.Version,

internal/plugin/slo/core/sli_rules_v1/plugin.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ func optimizedFactorySLIRecordGenerator(slo model.PromSLO, window time.Duration,
8888
return factorySLIRecordGenerator(slo, window, alerts)
8989
}
9090

91-
const (
92-
tplKeyWindow = "window"
93-
)
94-
9591
// factorySLIRecordGenerator knows how to generate the SLI prometheus recording rules
9692
// form an SLO.
9793
// Normally these rules are used by the SLO alerts.
@@ -119,7 +115,7 @@ func rawSLIRecordGenerator(slo model.PromSLO, window time.Duration, alerts model
119115
strWindow := promutils.TimeDurationToPromStr(window)
120116
var b bytes.Buffer
121117
err = tpl.Execute(&b, map[string]string{
122-
tplKeyWindow: strWindow,
118+
conventions.TplSLIQueryWindowVarName: strWindow,
123119
})
124120
if err != nil {
125121
return nil, fmt.Errorf("could not render SLI expression template: %w", err)
@@ -155,7 +151,7 @@ func eventsSLIRecordGenerator(slo model.PromSLO, window time.Duration, alerts mo
155151
strWindow := promutils.TimeDurationToPromStr(window)
156152
var b bytes.Buffer
157153
err = tpl.Execute(&b, map[string]string{
158-
tplKeyWindow: strWindow,
154+
conventions.TplSLIQueryWindowVarName: strWindow,
159155
})
160156
if err != nil {
161157
return nil, fmt.Errorf("could not render SLI expression template: %w", err)

internal/pluginengine/slo/custom/github.qkg1.top-prometheus-prometheus-promql-parser.go

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pluginengine/slo/custom/github.qkg1.top-slok-sloth-pkg-common-conventions.go

Lines changed: 22 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)