Skip to content

Commit c44305c

Browse files
committed
Add grouping to SLO status
1 parent 2aff53c commit c44305c

File tree

17 files changed

+429
-266
lines changed

17 files changed

+429
-266
lines changed

api.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ paths:
3535
required: true
3636
schema:
3737
type: string
38+
- in: query
39+
name: grouping
40+
schema:
41+
type: string
3842
responses:
3943
'200':
4044
description: Get objective status

examples/pyrra-http-errors.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ spec:
1616
metric: http_requests_total{job="pyrra",code=~"5.."}
1717
total:
1818
metric: http_requests_total{job="pyrra"}
19+
grouping:
20+
- route

filesystem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (f FilesystemObjectiveServer) GetObjectiveErrorBudget(ctx context.Context,
266266
return openapiserver.ImplResponse{}, fmt.Errorf("endpoint not implement")
267267
}
268268

269-
func (f FilesystemObjectiveServer) GetObjectiveStatus(ctx context.Context, expr string) (openapiserver.ImplResponse, error) {
269+
func (f FilesystemObjectiveServer) GetObjectiveStatus(ctx context.Context, expr string, grouping string) (openapiserver.ImplResponse, error) {
270270
return openapiserver.ImplResponse{}, fmt.Errorf("endpoint not implement")
271271
}
272272

kubernetes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (o *ObjectiveServer) GetObjectiveErrorBudget(ctx context.Context, expr stri
182182
return openapiserver.ImplResponse{}, fmt.Errorf("endpoint not implement")
183183
}
184184

185-
func (o *ObjectiveServer) GetObjectiveStatus(ctx context.Context, expr string) (openapiserver.ImplResponse, error) {
185+
func (o *ObjectiveServer) GetObjectiveStatus(ctx context.Context, expr string, grouping string) (openapiserver.ImplResponse, error) {
186186
return openapiserver.ImplResponse{}, fmt.Errorf("endpoint not implement")
187187
}
188188

main.go

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (o *ObjectivesServer) ListObjectives(ctx context.Context, query string) (op
303303
}, nil
304304
}
305305

306-
func (o *ObjectivesServer) GetObjectiveStatus(ctx context.Context, expr string) (openapiserver.ImplResponse, error) {
306+
func (o *ObjectivesServer) GetObjectiveStatus(ctx context.Context, expr string, grouping string) (openapiserver.ImplResponse, error) {
307307
clientObjectives, _, err := o.apiclient.ObjectivesApi.ListObjectives(ctx).Expr(expr).Execute()
308308
if err != nil {
309309
var apiErr openapiclient.GenericOpenAPIError
@@ -320,6 +320,26 @@ func (o *ObjectivesServer) GetObjectiveStatus(ctx context.Context, expr string)
320320

321321
objective := openapi.InternalFromClient(clientObjectives[0])
322322

323+
// Merge grouping into objective's query
324+
if grouping != "" {
325+
groupingMatchers, err := parser.ParseMetricSelector(grouping)
326+
if err != nil {
327+
return openapiserver.ImplResponse{}, err
328+
}
329+
if objective.Indicator.Ratio != nil {
330+
for _, m := range groupingMatchers {
331+
objective.Indicator.Ratio.Errors.LabelMatchers = append(objective.Indicator.Ratio.Errors.LabelMatchers, m)
332+
objective.Indicator.Ratio.Total.LabelMatchers = append(objective.Indicator.Ratio.Total.LabelMatchers, m)
333+
}
334+
}
335+
if objective.Indicator.Latency != nil {
336+
for _, m := range groupingMatchers {
337+
objective.Indicator.Latency.Success.LabelMatchers = append(objective.Indicator.Latency.Success.LabelMatchers, m)
338+
objective.Indicator.Latency.Total.LabelMatchers = append(objective.Indicator.Latency.Total.LabelMatchers, m)
339+
}
340+
}
341+
}
342+
323343
ts := RoundUp(time.Now().UTC(), 5*time.Minute)
324344

325345
queryTotal := objective.QueryTotal(objective.Window)
@@ -403,15 +423,37 @@ func (o *ObjectivesServer) GetObjectiveErrorBudget(ctx context.Context, expr str
403423
return openapiserver.ImplResponse{}, err
404424
}
405425
if objective.Indicator.Ratio != nil {
426+
groupings := map[string]struct{}{}
427+
for _, g := range objective.Indicator.Ratio.Grouping {
428+
groupings[g] = struct{}{}
429+
}
430+
406431
for _, m := range groupingMatchers {
407432
objective.Indicator.Ratio.Errors.LabelMatchers = append(objective.Indicator.Ratio.Errors.LabelMatchers, m)
408433
objective.Indicator.Ratio.Total.LabelMatchers = append(objective.Indicator.Ratio.Total.LabelMatchers, m)
434+
delete(groupings, m.Name)
435+
}
436+
437+
objective.Indicator.Ratio.Grouping = []string{}
438+
for g := range groupings {
439+
objective.Indicator.Ratio.Grouping = append(objective.Indicator.Ratio.Grouping, g)
409440
}
410441
}
411442
if objective.Indicator.Latency != nil {
443+
groupings := map[string]struct{}{}
444+
for _, g := range objective.Indicator.Ratio.Grouping {
445+
groupings[g] = struct{}{}
446+
}
447+
412448
for _, m := range groupingMatchers {
413449
objective.Indicator.Latency.Success.LabelMatchers = append(objective.Indicator.Latency.Success.LabelMatchers, m)
414450
objective.Indicator.Latency.Total.LabelMatchers = append(objective.Indicator.Latency.Total.LabelMatchers, m)
451+
delete(groupings, m.Name)
452+
}
453+
454+
objective.Indicator.Ratio.Grouping = []string{}
455+
for g := range groupings {
456+
objective.Indicator.Ratio.Grouping = append(objective.Indicator.Ratio.Grouping, g)
415457
}
416458
}
417459
}
@@ -443,10 +485,6 @@ func (o *ObjectivesServer) GetObjectiveErrorBudget(ctx context.Context, expr str
443485
return openapiserver.ImplResponse{Code: http.StatusInternalServerError}, fmt.Errorf("no matrix returned")
444486
}
445487

446-
//if len(matrix) != 1 {
447-
// return openapiserver.ImplResponse{Code: http.StatusNotFound}, fmt.Errorf("no data")
448-
//}
449-
450488
if len(matrix) == 0 {
451489
return openapiserver.ImplResponse{Code: http.StatusNotFound}, fmt.Errorf("no data")
452490
}
@@ -487,6 +525,31 @@ func (o *ObjectivesServer) GetMultiBurnrateAlerts(ctx context.Context, expr stri
487525

488526
objective := openapi.InternalFromClient(clientObjectives[0])
489527

528+
// Merge grouping into objective's query
529+
if grouping != "" {
530+
groupingMatchers, err := parser.ParseMetricSelector(grouping)
531+
if err != nil {
532+
return openapiserver.ImplResponse{}, err
533+
}
534+
535+
if objective.Indicator.Ratio != nil {
536+
objective.Indicator.Ratio.Grouping = nil // Don't group by here
537+
538+
for _, m := range groupingMatchers {
539+
objective.Indicator.Ratio.Errors.LabelMatchers = append(objective.Indicator.Ratio.Errors.LabelMatchers, m)
540+
objective.Indicator.Ratio.Total.LabelMatchers = append(objective.Indicator.Ratio.Total.LabelMatchers, m)
541+
}
542+
}
543+
if objective.Indicator.Latency != nil {
544+
objective.Indicator.Latency.Grouping = nil // Don't group by here
545+
546+
for _, m := range groupingMatchers {
547+
objective.Indicator.Latency.Success.LabelMatchers = append(objective.Indicator.Latency.Success.LabelMatchers, m)
548+
objective.Indicator.Latency.Total.LabelMatchers = append(objective.Indicator.Latency.Total.LabelMatchers, m)
549+
}
550+
}
551+
}
552+
490553
baseAlerts, err := objective.Alerts()
491554
if err != nil {
492555
return openapiserver.ImplResponse{Code: http.StatusInternalServerError}, err

openapi/client/api/openapi.yaml

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

openapi/client/api_objectives.go

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

openapi/server/api/openapi.yaml

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

openapi/server/go/api.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openapi/server/go/api_objectives.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)