Skip to content
Open
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
2 changes: 1 addition & 1 deletion examples/kubernetes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Next to Prometheus, Alertmanager, node-exporter, prometheus-adapter and Grafana,
git clone git@github.com:prometheus-operator/kube-prometheus.git
cd kube-prometheus
# Deploy the CRDs and the Prometheus Operator
kubectl apply -f ./manifests/setup
kubectl apply --server-side=true -f ./manifests/setup
# Deploy all the resource like Prometheus, StatefulSets, and Deployments.
kubectl apply -f ./manifests/
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: pyrra.dev/v1alpha1
kind: ServiceLevelObjective
metadata:
labels:
prometheus: k8s
role: alert-rules
name: prometheus-query-errors-with-offset
namespace: monitoring
spec:
description: ""
indicator:
ratio:
errors:
metric: prometheus_http_requests_total{job="prometheus-k8s",handler=~"/api/v1/query.*",code=~"5.."} offset 1m
grouping:
- handler
total:
metric: prometheus_http_requests_total{job="prometheus-k8s",handler=~"/api/v1/query.*"} offset 1m
target: "99"
window: 2w
30 changes: 18 additions & 12 deletions kubernetes/api/v1alpha1/servicelevelobjective_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,14 @@ func (in *ServiceLevelObjective) Internal() (slo.Objective, error) {

ratio = &slo.RatioIndicator{
Errors: slo.Metric{
Name: errorVec.Name,
LabelMatchers: errorMatchers,
Name: errorVec.Name,
LabelMatchers: errorMatchers,
OriginalOffset: &errorVec.OriginalOffset,
},
Total: slo.Metric{
Name: totalVec.Name,
LabelMatchers: totalVec.LabelMatchers,
Name: totalVec.Name,
LabelMatchers: totalVec.LabelMatchers,
OriginalOffset: &totalVec.OriginalOffset,
},
Grouping: in.Spec.ServiceLevelIndicator.Ratio.Grouping,
}
Expand Down Expand Up @@ -463,12 +465,14 @@ func (in *ServiceLevelObjective) Internal() (slo.Objective, error) {

latency = &slo.LatencyIndicator{
Success: slo.Metric{
Name: successVec.Name,
LabelMatchers: successMatchers,
Name: successVec.Name,
LabelMatchers: successMatchers,
OriginalOffset: &successVec.OriginalOffset,
},
Total: slo.Metric{
Name: totalVec.Name,
LabelMatchers: totalMatchers,
Name: totalVec.Name,
LabelMatchers: totalMatchers,
OriginalOffset: &totalVec.OriginalOffset,
},
Grouping: in.Spec.ServiceLevelIndicator.Latency.Grouping,
}
Expand Down Expand Up @@ -500,8 +504,9 @@ func (in *ServiceLevelObjective) Internal() (slo.Objective, error) {
latencyNative = &slo.LatencyNativeIndicator{
Latency: latency,
Total: slo.Metric{
Name: totalVec.Name,
LabelMatchers: totalMatchers,
Name: totalVec.Name,
LabelMatchers: totalMatchers,
OriginalOffset: &totalVec.OriginalOffset,
},
Grouping: in.Spec.ServiceLevelIndicator.LatencyNative.Grouping,
}
Expand All @@ -527,8 +532,9 @@ func (in *ServiceLevelObjective) Internal() (slo.Objective, error) {

boolGauge = &slo.BoolGaugeIndicator{
Metric: slo.Metric{
Name: vec.Name,
LabelMatchers: matchers,
Name: vec.Name,
LabelMatchers: matchers,
OriginalOffset: &vec.OriginalOffset,
},
Grouping: in.Spec.ServiceLevelIndicator.BoolGauge.Grouping,
}
Expand Down
77 changes: 75 additions & 2 deletions kubernetes/api/v1alpha1/servicelevelobjective_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -65,13 +66,15 @@ spec:
{Type: labels.MatchRegexp, Name: "code", Value: "5.."},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "http_requests_total"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
Total: slo.Metric{
Name: "http_requests_total",
LabelMatchers: []*labels.Matcher{
{Type: labels.MatchEqual, Name: "job", Value: "metrics-service-thanos-receive-default"},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "http_requests_total"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
},
},
Expand Down Expand Up @@ -120,6 +123,7 @@ spec:
{Type: labels.MatchRegexp, Name: "grpc_code", Value: "Aborted|Unavailable|Internal|Unknown|Unimplemented|DataLoss"},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "grpc_server_handled_total"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
Total: slo.Metric{
Name: "grpc_server_handled_total",
Expand All @@ -129,6 +133,67 @@ spec:
{Type: labels.MatchEqual, Name: "grpc_method", Value: "Write"},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "grpc_server_handled_total"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
},
},
},
},
{
config: `
apiVersion: pyrra.dev/v1alpha1
kind: ServiceLevelObjective
metadata:
name: http-errors-with-offset
namespace: monitoring
labels:
prometheus: k8s
role: alert-rules
pyrra.dev/team: foo
annotations:
pyrra.dev/description: "foo"
spec:
target: 99
window: 1w
indicator:
ratio:
errors:
metric: http_requests_total{job="metrics-service-thanos-receive-default",code=~"5.."} offset 10m
total:
metric: http_requests_total{job="metrics-service-thanos-receive-default"} offset 10m
`,
objective: slo.Objective{
Labels: labels.FromStrings(
labels.MetricName, "http-errors-with-offset",
"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,
},
Indicator: slo.Indicator{
Ratio: &slo.RatioIndicator{
Errors: slo.Metric{
Name: "http_requests_total",
LabelMatchers: []*labels.Matcher{
{Type: labels.MatchEqual, Name: "job", Value: "metrics-service-thanos-receive-default"},
{Type: labels.MatchRegexp, Name: "code", Value: "5.."},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "http_requests_total"},
},
OriginalOffset: ptr.To(10 * time.Minute),
},
Total: slo.Metric{
Name: "http_requests_total",
LabelMatchers: []*labels.Matcher{
{Type: labels.MatchEqual, Name: "job", Value: "metrics-service-thanos-receive-default"},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "http_requests_total"},
},
OriginalOffset: ptr.To(10 * time.Minute),
},
},
},
Expand Down Expand Up @@ -175,6 +240,7 @@ spec:
{Type: labels.MatchEqual, Name: "le", Value: "1"},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "http_request_duration_seconds_bucket"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
Total: slo.Metric{
Name: "http_request_duration_seconds_count",
Expand All @@ -183,6 +249,7 @@ spec:
{Type: labels.MatchRegexp, Name: "code", Value: "2.."},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "http_request_duration_seconds_count"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
},
},
Expand Down Expand Up @@ -230,6 +297,7 @@ spec:
{Type: labels.MatchEqual, Name: "le", Value: "0.6"},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "grpc_server_handling_seconds_bucket"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
Total: slo.Metric{
Name: "grpc_server_handling_seconds_count",
Expand All @@ -239,6 +307,7 @@ spec:
{Type: labels.MatchEqual, Name: "grpc_method", Value: "Write"},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "grpc_server_handling_seconds_count"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
},
},
Expand Down Expand Up @@ -286,6 +355,7 @@ spec:
{Type: labels.MatchEqual, Name: "le", Value: "0.5"},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "nginx_ingress_controller_request_duration_seconds_bucket"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
Total: slo.Metric{
Name: "nginx_ingress_controller_request_duration_seconds_count",
Expand All @@ -294,6 +364,7 @@ spec:
{Type: labels.MatchEqual, Name: "path", Value: "/"},
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "nginx_ingress_controller_request_duration_seconds_count"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
},
},
Expand Down Expand Up @@ -337,12 +408,14 @@ spec:
LabelMatchers: []*labels.Matcher{
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "prometheus_operator_reconcile_errors_total"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
Total: slo.Metric{
Name: "prometheus_operator_reconcile_operations_total",
LabelMatchers: []*labels.Matcher{
{Type: labels.MatchEqual, Name: labels.MetricName, Value: "prometheus_operator_reconcile_operations_total"},
},
OriginalOffset: ptr.To(0 * time.Second),
},
},
},
Expand Down Expand Up @@ -419,8 +492,8 @@ func TestServiceLevelObjective_Validate(t *testing.T) {
Window: "2w",
ServiceLevelIndicator: v1alpha1.ServiceLevelIndicator{
Ratio: &v1alpha1.RatioIndicator{
Errors: v1alpha1.Query{Metric: `errors{foo="bar"}`},
Total: v1alpha1.Query{Metric: `total{foo="bar"}`},
Errors: v1alpha1.Query{Metric: `errors{foo="bar"} offset 10m`},
Total: v1alpha1.Query{Metric: `total{foo="bar"} offset 10m`},
Grouping: nil,
},
},
Expand Down
Loading