Skip to content
New issue

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

Use Alerting spec to propagate labels and annotations #1003

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,22 @@ spec:
absent:
default: true
type: boolean
annotations:
additionalProperties:
type: string
description: Annotations are added to the alerts generated by Pyrra.
type: object
burnrates:
default: true
type: boolean
disabled:
description: Disabled is used to disable the generation of alerts. Recording rules are still generated.
type: boolean
labels:
additionalProperties:
type: string
description: Labels are added to the alerts generated by Pyrra.
type: object
name:
description: Name is used as the name of the alert generated by Pyrra. Defaults to "ErrorBudgetBurn".
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,22 @@ spec:
absent:
default: true
type: boolean
annotations:
additionalProperties:
type: string
description: Annotations are added to the alerts generated by Pyrra.
type: object
burnrates:
default: true
type: boolean
disabled:
description: Disabled is used to disable the generation of alerts. Recording rules are still generated.
type: boolean
labels:
additionalProperties:
type: string
description: Labels are added to the alerts generated by Pyrra.
type: object
name:
description: Name is used as the name of the alert generated by Pyrra. Defaults to "ErrorBudgetBurn".
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,22 @@ spec:
absent:
default: true
type: boolean
annotations:
additionalProperties:
type: string
description: Annotations are added to the alerts generated by Pyrra.
type: object
burnrates:
default: true
type: boolean
disabled:
description: Disabled is used to disable the generation of alerts. Recording rules are still generated.
type: boolean
labels:
additionalProperties:
type: string
description: Labels are added to the alerts generated by Pyrra.
type: object
name:
description: Name is used as the name of the alert generated by Pyrra. Defaults to "ErrorBudgetBurn".
type: string
Expand Down
4 changes: 3 additions & 1 deletion examples/parca-grpc-queryrange-errors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ kind: ServiceLevelObjective
metadata:
labels:
prometheus: k8s
pyrra.dev/team: parca
role: alert-rules
name: parca-grpc-queryrange-errors
namespace: parca
Expand All @@ -16,3 +15,6 @@ spec:
metric: grpc_server_handled_total{grpc_service="parca.query.v1alpha1.QueryService",grpc_method="QueryRange"}
target: "99"
window: 2w
alerting:
labels:
team: parca
14 changes: 14 additions & 0 deletions jsonnet/controller-gen/pyrra.dev_servicelevelobjectives.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
"default": true,
"type": "boolean"
},
"annotations": {
"additionalProperties": {
"type": "string"
},
"description": "Annotations are added to the alerts generated by Pyrra.",
"type": "object"
},
"burnrates": {
"default": true,
"type": "boolean"
Expand All @@ -78,6 +85,13 @@
"description": "Disabled is used to disable the generation of alerts. Recording rules are still generated.",
"type": "boolean"
},
"labels": {
"additionalProperties": {
"type": "string"
},
"description": "Labels are added to the alerts generated by Pyrra.",
"type": "object"
},
"name": {
"description": "Name is used as the name of the alert generated by Pyrra. Defaults to \"ErrorBudgetBurn\".",
"type": "string"
Expand Down
16 changes: 16 additions & 0 deletions kubernetes/api/v1alpha1/servicelevelobjective_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ type Alerting struct {
// +optional
// Name is used as the name of the alert generated by Pyrra. Defaults to "ErrorBudgetBurn".
Name string `json:"name,omitempty"`

// +optional
// Labels are added to the alerts generated by Pyrra.
Labels map[string]string `json:"labels,omitempty"`

// +optional
// Annotations are added to the alerts generated by Pyrra.
Annotations map[string]string `json:"annotations,omitempty"`
}

type RatioIndicator struct {
Expand Down Expand Up @@ -369,6 +377,14 @@ func (in *ServiceLevelObjective) Internal() (slo.Objective, error) {
alerting.Name = in.Spec.Alerting.Name
}

if in.Spec.Alerting.Labels != nil {
alerting.Labels = in.Spec.Alerting.Labels
}

if in.Spec.Alerting.Annotations != nil {
alerting.Annotations = in.Spec.Alerting.Annotations
}

if in.Spec.ServiceLevelIndicator.Ratio != nil && in.Spec.ServiceLevelIndicator.Latency != nil {
return slo.Objective{}, fmt.Errorf("cannot have ratio and latency indicators at the same time")
}
Expand Down
16 changes: 11 additions & 5 deletions kubernetes/api/v1alpha1/servicelevelobjective_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ metadata:
labels:
prometheus: k8s
role: alert-rules
pyrra.dev/team: foo
annotations:
pyrra.dev/description: "foo"
spec:
target: 99
window: 1w
Expand All @@ -41,20 +38,29 @@ spec:
metric: http_requests_total{job="metrics-service-thanos-receive-default",code=~"5.."}
total:
metric: http_requests_total{job="metrics-service-thanos-receive-default"}
alerting:
labels:
team: foo
annotations:
description: foo
`,
objective: slo.Objective{
Labels: labels.FromStrings(
labels.MetricName, "http-errors",
"namespace", "monitoring",
"pyrra.dev/team", "foo",
),
Annotations: map[string]string{"pyrra.dev/description": "foo"},
Description: "",
Target: 0.99,
Window: model.Duration(7 * 24 * time.Hour),
Alerting: slo.Alerting{
Burnrates: true,
Absent: true,
Labels: map[string]string{
"team": "foo",
},
Annotations: map[string]string{
"description": "foo",
},
},
Indicator: slo.Indicator{
Ratio: &slo.RatioIndicator{
Expand Down
21 changes: 21 additions & 0 deletions slo/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,26 +595,47 @@ func (o Objective) commonRuleLabels(sloName string) map[string]string {
"slo": sloName,
}

// DEPRECATED: Labels prefixed with "pyrra.dev" are still being used for backwards compatibility.
// Use "o.Alerting.Labels" instead.
// TODO: Remove this in a future release.
for _, label := range o.Labels {
if strings.HasPrefix(label.Name, PropagationLabelsPrefix) {
ruleLabels[strings.TrimPrefix(label.Name, PropagationLabelsPrefix)] = label.Value
}
}

if o.Alerting.Labels != nil {
for key, value := range o.Alerting.Labels {
ruleLabels[key] = value
}
}

return ruleLabels
}

func (o Objective) commonRuleAnnotations() map[string]string {
var annotations map[string]string
if len(o.Annotations) > 0 {
annotations = make(map[string]string)
// DEPRECATED: Annotations prefixed with "pyrra.dev" are still being used for backwards compatibility.
// Use "o.Alerting.Annotations" instead.
// TODO: Remove this in a future release.
for key, value := range o.Annotations {
if strings.HasPrefix(key, PropagationLabelsPrefix) {
annotations[strings.TrimPrefix(key, PropagationLabelsPrefix)] = value
}
}
}

if o.Alerting.Annotations != nil {
if annotations == nil {
annotations = make(map[string]string)
}
for key, value := range o.Alerting.Annotations {
annotations[key] = value
}
}

return annotations
}

Expand Down
12 changes: 8 additions & 4 deletions slo/slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
const (
// PropagationLabelsPrefix provides a way to propagate labels from the
// ObjectMeta to the PrometheusRule.
// DEPRECATED: use the `Alerting.labels` field instead.
// This is being kept for backwards compatibility.
PropagationLabelsPrefix = "pyrra.dev/"
defaultAlertname = "ErrorBudgetBurn"
)
Expand Down Expand Up @@ -134,10 +136,12 @@ type BoolGaugeIndicator struct {
}

type Alerting struct {
Disabled bool // deprecated, use Burnrates instead
Burnrates bool
Absent bool
Name string
Disabled bool // deprecated, use Burnrates instead
Burnrates bool
Absent bool
Name string
Labels map[string]string
Annotations map[string]string
}

type Metric struct {
Expand Down