-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
parser_hints.go
206 lines (173 loc) · 5.91 KB
/
parser_hints.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package log
import (
"strings"
"github.com/grafana/loki/pkg/logqlmodel"
)
func NoParserHints() ParserHint {
return &Hints{}
}
// ParserHint are hints given to LogQL parsers.
// This is specially useful for parser that extract implicitly all possible label keys.
// This is used only within metric queries since it's rare that you need all label keys.
// For example in the following expression:
//
// sum by (status_code) (rate({app="foo"} | json [5m]))
//
// All we need to extract is the status_code in the json parser.
type ParserHint interface {
// Tells if a label with the given key should be extracted.
ShouldExtract(key string) bool
// Tells if there's any hint that start with the given prefix.
// This allows to speed up key searching in nested structured like json.
ShouldExtractPrefix(prefix string) bool
// Tells if we should not extract any labels.
// For example in :
// sum(rate({app="foo"} | json [5m]))
// We don't need to extract any labels from the log line.
NoLabels() bool
// Holds state about what's already been extracted for the associated
// labels. This assumes that only required labels are ever extracted
RecordExtracted(string)
// Returns true if all the required labels have already been extracted
AllRequiredExtracted() bool
// Resets the state of extracted labels
Reset()
// PreserveError returns true when parsing errors were specifically requested
PreserveError() bool
// ShouldContinueParsingLine returns true when there is no label matcher for the
// provided label or the passed label and value match what's in the pipeline
ShouldContinueParsingLine(labelName string, lbs *LabelsBuilder) bool
}
type Hints struct {
noLabels bool
requiredLabels []string
shouldPreserveError bool
extracted []string
labelFilters []LabelFilterer
labelNames []string
}
func (p *Hints) ShouldExtract(key string) bool {
for _, l := range p.extracted {
if l == key {
return false
}
}
for _, l := range p.requiredLabels {
if l == key {
return true
}
}
return len(p.requiredLabels) == 0
}
func (p *Hints) ShouldExtractPrefix(prefix string) bool {
if len(p.requiredLabels) == 0 {
return true
}
for _, l := range p.requiredLabels {
if strings.HasPrefix(l, prefix) {
return true
}
}
return false
}
func (p *Hints) NoLabels() bool {
return p.noLabels || p.AllRequiredExtracted()
}
func (p *Hints) RecordExtracted(key string) {
p.extracted = append(p.extracted, key)
}
func (p *Hints) AllRequiredExtracted() bool {
if len(p.requiredLabels) == 0 || len(p.extracted) < len(p.requiredLabels) {
return false
}
found := 0
for _, l := range p.requiredLabels {
for _, e := range p.extracted {
if l == e {
found++
break
}
}
}
return len(p.requiredLabels) == found
}
func (p *Hints) Reset() {
p.extracted = p.extracted[:0]
}
func (p *Hints) PreserveError() bool {
return p.shouldPreserveError
}
func (p *Hints) ShouldContinueParsingLine(labelName string, lbs *LabelsBuilder) bool {
for i := 0; i < len(p.labelNames); i++ {
if p.labelNames[i] == labelName {
_, matches := p.labelFilters[i].Process(0, nil, lbs)
return matches
}
}
return true
}
// NewParserHint creates a new parser hint using the list of labels that are seen and required in a query.
func NewParserHint(requiredLabelNames, groups []string, without, noLabels bool, metricLabelName string, stages []Stage) *Hints {
hints := make([]string, 0, 2*(len(requiredLabelNames)+len(groups)+1))
hints = appendLabelHints(hints, requiredLabelNames...)
hints = appendLabelHints(hints, groups...)
hints = appendLabelHints(hints, metricLabelName)
hints = uniqueString(hints)
// Save the names next to the filters to avoid an alloc when f.RequiredLabelNames() is called
var labelNames []string
var labelFilters []LabelFilterer
for _, s := range stages {
switch f := s.(type) {
case *BinaryLabelFilter:
// TODO: as long as each leg of the binary filter operates on the same (and only 1) label,
// we should be able to add this to our filters
continue
case LabelFilterer:
if len(f.RequiredLabelNames()) > 1 {
// Hints can only operate on one label at a time
continue
}
labelFilters = append(labelFilters, f)
labelNames = append(labelNames, f.RequiredLabelNames()...)
}
}
extracted := make([]string, 0, len(hints))
if noLabels {
if len(hints) > 0 {
return &Hints{requiredLabels: hints, extracted: extracted, shouldPreserveError: containsError(hints), labelFilters: labelFilters, labelNames: labelNames}
}
return &Hints{noLabels: true}
}
ph := &Hints{labelFilters: labelFilters, labelNames: labelNames}
// we don't know what is required when a without clause is used.
// Same is true when there's no grouping.
// no hints available then.
if without || len(groups) == 0 {
return ph
}
return &Hints{requiredLabels: hints, extracted: extracted, shouldPreserveError: containsError(hints)}
}
func containsError(hints []string) bool {
for _, s := range hints {
if s == logqlmodel.ErrorLabel {
return true
}
}
return false
}
// appendLabelHints Appends the label to the list of hints with and without the duplicate suffix.
// If a parsed label collides with a stream label we add the `_extracted` suffix to it, however hints
// are used by the parsers before we know they will collide with a stream label and hence before the
// _extracted suffix is added. Therefore we must strip the _extracted suffix from any required labels
// that were parsed from somewhere in the query, say in a filter or an aggregation clause.
// Because it's possible for a valid json or logfmt key to already end with _extracted, we'll just
// leave the existing entry ending with _extracted but also add a version with the suffix removed.
func appendLabelHints(dst []string, src ...string) []string {
for _, l := range src {
dst = append(dst, l)
if strings.HasSuffix(l, duplicateSuffix) {
dst = append(dst, strings.TrimSuffix(l, duplicateSuffix))
}
}
return dst
}