-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
categorized_labels_iterator.go
70 lines (55 loc) · 1.51 KB
/
categorized_labels_iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package iter
import (
"fmt"
"github.com/prometheus/prometheus/model/labels"
"github.com/grafana/loki/v3/pkg/logql/syntax"
)
type categorizeLabelsIterator struct {
EntryIterator
currStreamLabels string
currHash uint64
currErr error
}
func NewCategorizeLabelsIterator(wrap EntryIterator) EntryIterator {
return &categorizeLabelsIterator{
EntryIterator: wrap,
}
}
func (c *categorizeLabelsIterator) Next() bool {
if !c.EntryIterator.Next() {
return false
}
currEntry := c.At()
if len(currEntry.StructuredMetadata) == 0 && len(currEntry.Parsed) == 0 {
c.currStreamLabels = c.EntryIterator.Labels()
c.currHash = c.EntryIterator.StreamHash()
return true
}
// We need to remove the structured metadata labels and parsed labels from the stream labels.
streamLabels := c.EntryIterator.Labels()
lbls, err := syntax.ParseLabels(streamLabels)
if err != nil {
c.currErr = fmt.Errorf("failed to parse series labels to categorize labels: %w", err)
return false
}
builder := labels.NewBuilder(lbls)
for _, label := range currEntry.StructuredMetadata {
builder.Del(label.Name)
}
for _, label := range currEntry.Parsed {
builder.Del(label.Name)
}
newLabels := builder.Labels()
c.currStreamLabels = newLabels.String()
c.currHash = newLabels.Hash()
return true
}
func (c *categorizeLabelsIterator) Err() error {
return c.currErr
}
func (c *categorizeLabelsIterator) Labels() string {
return c.currStreamLabels
}
func (c *categorizeLabelsIterator) StreamHash() uint64 {
return c.currHash
}