8000 feat: add custom labels to `sloth_slo_info` metric by cxdy · Pull Request #4 · linode-obs/sloth · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add custom labels to sloth_slo_info metric #4

New issue
8000

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2024
Merged
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## [v0.13.0] - 2024-10-30

- Custom labels for `sloth_slo_info{}` metric [#4](https://github.com/linode-obs/sloth/pull/4)

## [v0.12.0] - 2023-07-03

- Custom rule_group intervals for all recording rule types or a global default.
Expand Down Expand Up @@ -166,7 +170,8 @@
- Support raw query based SLI.
- Kubernetes (prometheus-operator) CRD generation support.

[unreleased]: https://github.com/slok/sloth/compare/v0.12.0...HEAD
[unreleased]: https://github.com/linode-obs/sloth/compare/v0.13.0...HEAD
[v0.13.0]: https://github.com/slok/sloth/compare/v0.12.0...v0.13.0
[v0.12.0]: https://github.com/slok/sloth/compare/v0.11.0...v0.12.0
[v0.11.0]: https://github.com/slok/sloth/compare/v0.10.0...v0.11.0
[v0.10.0]: https://github.com/slok/sloth/compare/v0.9.0...v0.10.0
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ slos:
description: "Common SLO based on availability for HTTP request responses."
labels:
category: availability
# These labels only apply to the `sloth_slo_info{}` metric - they are `string: string` typed.
infoLabels:
foo: "bar"
sli:
events:
error_query: sum(rate(http_request_duration_seconds_count{job="myservice",code=~"(5..|429)"}[{{.window}}]))
Expand Down
3 changes: 2 additions & 1 deletion cmd/sloth/commands/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ type generateCommand struct {
sliPluginsPaths []string
sloPeriodWindowsPath string
sloPeriod string
infoLabels map[string]string
}

// NewGenerateCommand returns the generate command.
func NewGenerateCommand(app *kingpin.Application) Command {
c := &generateCommand{extraLabels: map[string]string{}}
c := &generateCommand{extraLabels: map[string]string{}, infoLabels: map[string]string{}}
cmd := app.Command("generate", "Generates Prometheus SLOs.")
cmd.Flag("input", "SLO spec input file path or directory (if directory is used, slos will be discovered recursively and out must be a directory).").Short('i').StringVar(&c.slosInput)
cmd.Flag("out", "Generated rules output file path or directory. If `-` it will use stdout (if input is a directory this must be a directory).").Default("-").Short('o').StringVar(&c.slosOut)
Expand Down
3 changes: 2 additions & 1 deletion cmd/sloth/commands/k8scontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (

type kubeControllerCommand struct {
extraLabels map[string]string
infoLabels map[string]string
workers int
kubeConfig string
kubeContext string
Expand All @@ -73,7 +74,7 @@ type kubeControllerCommand struct {

// NewKubeControllerCommand returns the Kubernetes controller command.
func NewKubeControllerCommand(app *kingpin.Application) Command {
c := &kubeControllerCommand{extraLabels: map[string]string{}}
c := &kubeControllerCommand{extraLabels: map[string]string{}, infoLabels: map[string]string{}}
cmd := app.Command("kubernetes-controller", "Runs Sloth in Kubernetes controller/operator mode.")
cmd.Alias("controller")
cmd.Alias("k8s-controller")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ spec:
runAsUser: 100
containers:
- name: sloth
image: slok/sloth-test:v1.42.42
image: linode-obs/sloth-test:v1.42.42
args:
- kubernetes-controller
- --resync-interval=17m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ spec:
runAsUser: 100
containers:
- name: sloth
image: slok/sloth-test:v1.42.42
image: linode-obs/sloth-test:v1.42.42
args:
- kubernetes-controller
- --resync-interval=17m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ spec:
runAsUser: 100
containers:
- name: sloth
image: slok/sloth-test:v1.42.42
image: linode-obs/sloth-test:v1.42.42
args:
- kubernetes-controller
- --resync-interval=17m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ spec:
serviceAccountName: sloth
containers:
- name: sloth
image: ghcr.io/slok/sloth:v0.11.0
image: ghcr.io/linode-obs/sloth:v0.13.0
args:
- kubernetes-controller
- --sli-plugins-path=/plugins
Expand Down
2 changes: 1 addition & 1 deletion deploy/kubernetes/helm/sloth/tests/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func customValues() msi {
},

"image": msi{
"repository": "slok/sloth-test",
"repository": "linode-obs/sloth-test",
"tag": "v1.42.42",
},

Expand Down
4 changes: 2 additions & 2 deletions deploy/kubernetes/helm/sloth/values.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
labels: {}

image:
repository: ghcr.io/slok/sloth
tag: v0.11.0
repository: ghcr.io/linode-obs/sloth
tag: v0.13.0

# -- Container resources: requests and limits for CPU, Memory
resources:
Expand Down
2 changes: 1 addition & 1 deletion deploy/kubernetes/raw/sloth-with-common-plugins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ spec:
serviceAccountName: sloth
containers:
- name: sloth
image: ghcr.io/slok/sloth:v0.11.0
image: ghcr.io/linode-obs/sloth:v0.13.0
args:
- kubernetes-controller
- --sli-plugins-path=/plugins
Expand Down
2 changes: 1 addition & 1 deletion deploy/kubernetes/raw/sloth.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ spec:
serviceAccountName: sloth
containers:
- name: sloth
image: ghcr.io/slok/sloth:v0.11.0
image: ghcr.io/linode-obs/sloth:v0.13.0
args:
- kubernetes-controller
ports:
Expand Down
1 change: 1 addition & 0 deletions internal/k8sprometheus/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type K8sMeta struct {
Namespace string
Annotations map[string]string
Labels map[string]string
InfoLabels map[string]string
}

// SLOGroup is a Kubernetes SLO group. Is created based on a regular Prometheus
Expand Down
1 change: 1 addition & 0 deletions internal/k8sprometheus/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func mapSpecToModel(ctx context.Context, defaultWindowPeriod time.Duration, plug
TimeWindow: defaultWindowPeriod,
Objective: specSLO.Objective,
Labels: mergeLabels(spec.Labels, specSLO.Labels),
InfoLabels: specSLO.InfoLabels,
PageAlertMeta: prometheus.AlertMeta{Disable: true},
TicketAlertMeta: prometheus.AlertMeta{Disable: true},
}
Expand Down
1 change: 1 addition & 0 deletions internal/prometheus/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type SLO struct {
Labels map[string]string `validate:"dive,keys,prom_label_key,endkeys,required,prom_label_value"`
PageAlertMeta AlertMeta
TicketAlertMeta AlertMeta
InfoLabels map[string]string `validate:"dive,keys,prom_label_key,endkeys,required,prom_label_value"`
}

type SLOGroup struct {
Expand Down
16 changes: 10 additions & 6 deletions internal/prometheus/recording_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ const MetadataRecordingRulesGenerator = metadataRecordingRulesGenerator(false)
func (m metadataRecordingRulesGenerator) GenerateMetadataRecordingRules(ctx context.Context, info info.Info, slo SLO, alerts alert.MWMBAlertGroup) ([]rulefmt.Rule, error) {
labels := mergeLabels(slo.GetSLOIDPromLabels(), slo.Labels)

infoLabels := mergeLabels(labels, map[string]string{
sloVersionLabelName: info.Version,
sloModeLabelName: string(info.Mode),
sloSpecLabelName: info.Spec,
sloObjectiveLabelName: strconv.FormatFloat(slo.Objective, 'f', -1, 64),
})

infoLabels = mergeLabels(infoLabels, slo.InfoLabels)

// Metatada Recordings.
const (
metricSLOObjectiveRatio = "slo:objective:ratio"
Expand Down Expand Up @@ -293,12 +302,7 @@ func (m metadataRecordingRulesGenerator) GenerateMetadataRecordingRules(ctx cont
{
Record: metricSLOInfo,
Expr: `vector(1)`,
Labels: mergeLabels(labels, map[string]string{
sloVersionLabelName: info.Version,
sloModeLabelName: string(info.Mode),
sloSpecLabelName: info.Spec,
sloObjectiveLabelName: strconv.FormatFloat(slo.Objective, 'f', -1, 64),
}),
Labels: infoLabels,
},
}

Expand Down
2 changes: 2 additions & 0 deletions internal/prometheus/spec.go
F438
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (y YAMLSpecLoader) LoadSpec(ctx context.Context, data []byte) (*SLOGroup, e
func (y YAMLSpecLoader) mapSpecToModel(ctx context.Context, spec prometheusv1.Spec) (*SLOGroup, error) {
models := make([]SLO, 0, len(spec.SLOs))
for _, specSLO := range spec.SLOs {

slo := SLO{
ID: fmt.Sprintf("%s-%s", spec.Service, specSLO.Name),
RuleGroupInterval: specSLO.Interval.RuleGroupInterval,
Expand All @@ -82,6 +83,7 @@ func (y YAMLSpecLoader) mapSpecToModel(ctx context.Context, spec prometheusv1.Sp
Labels: mergeLabels(spec.Labels, specSLO.Labels),
PageAlertMeta: AlertMeta{Disable: true},
TicketAlertMeta: AlertMeta{Disable: true},
InfoLabels: specSLO.InfoLabels,
}

// Set SLIs.
Expand Down
12 changes: 12 additions & 0 deletions internal/prometheus/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ labels:
slos:
- name: "slo-test"
objective: 99
infoLabels:
foo: bar
sli:
plugin:
id: test_plugin
Expand All @@ -156,6 +158,7 @@ slos:
Service: "test-svc",
TimeWindow: 30 * 24 * time.Hour,
Labels: map[string]string{"gk1": "gv1"},
InfoLabels: map[string]string{"foo": "bar"},
SLI: prometheus.SLI{
Raw: &prometheus.SLIRaw{
ErrorRatioQuery: `plugin_raw_expr{service="test-svc",slo="slo-test",objective="99.000000",gk1="gv1",k1="v1",k2="true"}`,
Expand All @@ -178,6 +181,8 @@ labels:
slos:
- name: "slo-test"
objective: 99
infoLabels:
foo: bar
sli:
raw:
error_ratio_query: test_expr_ratio_2
Expand All @@ -194,6 +199,7 @@ slos:
Service: "test-svc",
TimeWindow: 28 * 24 * time.Hour,
Labels: map[string]string{"gk1": "gv1"},
InfoLabels: map[string]string{"foo": "bar"},
SLI: prometheus.SLI{
Raw: &prometheus.SLIRaw{
ErrorRatioQuery: `test_expr_ratio_2`,
Expand All @@ -217,6 +223,8 @@ slos:
- name: "slo1"
labels:
category: test
infoLabels:
foo: bar
objective: 99.99
description: "This is a test."
sli:
Expand Down Expand Up @@ -244,6 +252,8 @@ slos:
- name: "slo2"
labels:
category: test2
infoLabels:
foo: bar
objective: 99.9
sli:
raw:
Expand Down Expand Up @@ -272,6 +282,7 @@ slos:
"owner": "myteam",
"category": "test",
},
InfoLabels: map[string]string{"foo": "bar"},
PageAlertMeta: prometheus.AlertMeta{
Disable: false,
Name: "testAlert",
Expand Down Expand Up @@ -314,6 +325,7 @@ slos:
"owner": "myteam",
"category": "test2",
},
InfoLabels: map[string]string{"foo": "bar"},
PageAlertMeta: prometheus.AlertMeta{Disable: true},
TicketAlertMeta: prometheus.AlertMeta{Disable: true},
},
Expand Down
3 changes: 3 additions & 0 deletions pkg/kubernetes/api/sloth/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type SLO struct {
// +optional
Labels map[string]string `json:"labels,omitempty"`

// Info labels added to the `sloth_slo_info` metric
InfoLabels map[string]string `json:"infoLabels,omitempty"`

// +kubebuilder:validation:Required
//
// SLI is the indicator (service level indicator) for this specific SLO.
Expand Down
2 changes: 2 additions & 0 deletions pkg/prometheus/api/v1/v1.go
Original file line number Diff line number Diff line change
6120 Expand Up @@ -86,6 +86,8 @@ type SLO struct {
// alerting rules for this specific SLO. These labels are merged with the
// previous level labels.
Labels map[string]string `yaml:"labels,omitempty"`
// Labels appended to `sloth_slo_info`
InfoLabels map[string]string `yaml:"infoLabels,omitempty"`
// SLI is the indicator (service level indicator) for this specific SLO.
SLI SLI `yaml:"sli"`
// Alerting is the configuration with all the things related with the SLO
Expand Down
Loading
0