Skip to content

Commit

Permalink
Address PR feedback: refactor GroupLabelsBytesFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskorn committed Feb 13, 2025
1 parent 1cff08f commit eb3e3eb
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions pkg/streamingpromql/operators/aggregations/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,24 @@ func GetAggregationItemType(aggregation string) (parser.ItemType, bool) {
type SeriesToGroupLabelsBytesFunc func(labels.Labels) []byte

func GroupLabelsBytesFunc(grouping []string, without bool) SeriesToGroupLabelsBytesFunc {
switch {
case without:
// Why 1024 bytes? It's what labels.Labels.String() uses as a buffer size, so we use that as a sensible starting point too.
b := make([]byte, 0, 1024)
return func(l labels.Labels) []byte {
b = l.BytesWithoutLabels(b, grouping...) // NewAggregation and NewTopKBottomK will add __name__ to Grouping for 'without' aggregations, so no need to add it here.
return b
}

case len(grouping) == 0:
if len(grouping) == 0 {
return groupToSingleSeriesLabelsBytesFunc
}

default:
// Why 1024 bytes? It's what labels.Labels.String() uses as a buffer size, so we use that as a sensible starting point too.
b := make([]byte, 0, 1024)
// Why 1024 bytes? It's what labels.Labels.String() uses as a buffer size, so we use that as a sensible starting point too.
b := make([]byte, 0, 1024)
if without {
return func(l labels.Labels) []byte {
b = l.BytesWithLabels(b, grouping...)
// NewAggregation and NewTopKBottomK will add __name__ to Grouping for 'without' aggregations, so no need to add it here.
b = l.BytesWithoutLabels(b, grouping...)
return b
}
}

return func(l labels.Labels) []byte {
b = l.BytesWithLabels(b, grouping...)
return b
}
}

var groupToSingleSeriesLabelsBytesFunc = func(_ labels.Labels) []byte { return nil }

0 comments on commit eb3e3eb

Please sign in to comment.