-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
metrics_extraction.go
302 lines (256 loc) · 8.77 KB
/
metrics_extraction.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package log
import (
"sort"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/model/labels"
"github.com/dustin/go-humanize"
)
const (
ConvertBytes = "bytes"
ConvertDuration = "duration"
ConvertFloat = "float"
)
// LineExtractor extracts a float64 from a log line.
type LineExtractor func([]byte) float64
var (
CountExtractor LineExtractor = func(line []byte) float64 { return 1. }
BytesExtractor LineExtractor = func(line []byte) float64 { return float64(len(line)) }
)
// SampleExtractor creates StreamSampleExtractor that can extract samples for a given log stream.
type SampleExtractor interface {
ForStream(labels labels.Labels) StreamSampleExtractor
}
// StreamSampleExtractor extracts sample for a log line.
// A StreamSampleExtractor never mutate the received line.
type StreamSampleExtractor interface {
BaseLabels() LabelsResult
Process(ts int64, line []byte, structuredMetadata ...labels.Label) (float64, LabelsResult, bool)
ProcessString(ts int64, line string, structuredMetadata ...labels.Label) (float64, LabelsResult, bool)
}
type lineSampleExtractor struct {
Stage
LineExtractor
baseBuilder *BaseLabelsBuilder
streamExtractors map[uint64]StreamSampleExtractor
}
// NewLineSampleExtractor creates a SampleExtractor from a LineExtractor.
// Multiple log stages are run before converting the log line.
func NewLineSampleExtractor(ex LineExtractor, stages []Stage, groups []string, without, noLabels bool) (SampleExtractor, error) {
s := ReduceStages(stages)
hints := NewParserHint(s.RequiredLabelNames(), groups, without, noLabels, "", stages)
return &lineSampleExtractor{
Stage: s,
LineExtractor: ex,
baseBuilder: NewBaseLabelsBuilderWithGrouping(groups, hints, without, noLabels),
streamExtractors: make(map[uint64]StreamSampleExtractor),
}, nil
}
func (l *lineSampleExtractor) ForStream(labels labels.Labels) StreamSampleExtractor {
hash := l.baseBuilder.Hash(labels)
if res, ok := l.streamExtractors[hash]; ok {
return res
}
res := &streamLineSampleExtractor{
Stage: l.Stage,
LineExtractor: l.LineExtractor,
builder: l.baseBuilder.ForLabels(labels, hash),
}
l.streamExtractors[hash] = res
return res
}
type streamLineSampleExtractor struct {
Stage
LineExtractor
builder *LabelsBuilder
}
func (l *streamLineSampleExtractor) Process(ts int64, line []byte, structuredMetadata ...labels.Label) (float64, LabelsResult, bool) {
l.builder.Reset()
l.builder.Add(structuredMetadata...)
// short circuit.
if l.Stage == NoopStage {
return l.LineExtractor(line), l.builder.GroupedLabels(), true
}
line, ok := l.Stage.Process(ts, line, l.builder)
if !ok {
return 0, nil, false
}
return l.LineExtractor(line), l.builder.GroupedLabels(), true
}
func (l *streamLineSampleExtractor) ProcessString(ts int64, line string, structuredMetadata ...labels.Label) (float64, LabelsResult, bool) {
// unsafe get bytes since we have the guarantee that the line won't be mutated.
return l.Process(ts, unsafeGetBytes(line), structuredMetadata...)
}
func (l *streamLineSampleExtractor) BaseLabels() LabelsResult { return l.builder.currentResult }
type convertionFn func(value string) (float64, error)
type labelSampleExtractor struct {
preStage Stage
postFilter Stage
labelName string
conversionFn convertionFn
baseBuilder *BaseLabelsBuilder
streamExtractors map[uint64]StreamSampleExtractor
}
// LabelExtractorWithStages creates a SampleExtractor that will extract metrics from a labels.
// A set of log stage is executed before the conversion. A Filtering stage is executed after the conversion allowing
// to remove sample containing the __error__ label.
func LabelExtractorWithStages(
labelName, conversion string,
groups []string, without, noLabels bool,
preStages []Stage,
postFilter Stage,
) (SampleExtractor, error) {
var convFn convertionFn
switch conversion {
case ConvertBytes:
convFn = convertBytes
case ConvertDuration:
convFn = convertDuration
case ConvertFloat:
convFn = convertFloat
default:
return nil, errors.Errorf("unsupported conversion operation %s", conversion)
}
if len(groups) == 0 || without {
without = true
groups = append(groups, labelName)
sort.Strings(groups)
}
preStage := ReduceStages(preStages)
hints := NewParserHint(append(preStage.RequiredLabelNames(), postFilter.RequiredLabelNames()...), groups, without, noLabels, labelName, append(preStages, postFilter))
return &labelSampleExtractor{
preStage: preStage,
conversionFn: convFn,
labelName: labelName,
postFilter: postFilter,
baseBuilder: NewBaseLabelsBuilderWithGrouping(groups, hints, without, noLabels),
streamExtractors: make(map[uint64]StreamSampleExtractor),
}, nil
}
type streamLabelSampleExtractor struct {
*labelSampleExtractor
builder *LabelsBuilder
}
func (l *labelSampleExtractor) ForStream(labels labels.Labels) StreamSampleExtractor {
hash := l.baseBuilder.Hash(labels)
if res, ok := l.streamExtractors[hash]; ok {
return res
}
res := &streamLabelSampleExtractor{
labelSampleExtractor: l,
builder: l.baseBuilder.ForLabels(labels, hash),
}
l.streamExtractors[hash] = res
return res
}
func (l *streamLabelSampleExtractor) Process(ts int64, line []byte, structuredMetadata ...labels.Label) (float64, LabelsResult, bool) {
// Apply the pipeline first.
l.builder.Reset()
l.builder.Add(structuredMetadata...)
line, ok := l.preStage.Process(ts, line, l.builder)
if !ok {
return 0, nil, false
}
// convert the label value.
var v float64
stringValue, _ := l.builder.Get(l.labelName)
if stringValue == "" {
// NOTE: It's totally fine for log line to not have this particular label.
// See Issue: https://github.com/grafana/loki/issues/6713
return 0, nil, false
}
var err error
v, err = l.conversionFn(stringValue)
if err != nil {
l.builder.SetErr(errSampleExtraction)
l.builder.SetErrorDetails(err.Error())
}
// post filters
if _, ok = l.postFilter.Process(ts, line, l.builder); !ok {
return 0, nil, false
}
return v, l.builder.GroupedLabels(), true
}
func (l *streamLabelSampleExtractor) ProcessString(ts int64, line string, structuredMetadata ...labels.Label) (float64, LabelsResult, bool) {
// unsafe get bytes since we have the guarantee that the line won't be mutated.
return l.Process(ts, unsafeGetBytes(line), structuredMetadata...)
}
func (l *streamLabelSampleExtractor) BaseLabels() LabelsResult { return l.builder.currentResult }
// NewFilteringSampleExtractor creates a sample extractor where entries from
// the underlying log stream are filtered by pipeline filters before being
// passed to extract samples. Filters are always upstream of the extractor.
func NewFilteringSampleExtractor(f []PipelineFilter, e SampleExtractor) SampleExtractor {
return &filteringSampleExtractor{
filters: f,
extractor: e,
}
}
type filteringSampleExtractor struct {
filters []PipelineFilter
extractor SampleExtractor
}
func (p *filteringSampleExtractor) ForStream(labels labels.Labels) StreamSampleExtractor {
var streamFilters []streamFilter
for _, f := range p.filters {
if allMatch(f.Matchers, labels) {
streamFilters = append(streamFilters, streamFilter{
start: f.Start,
end: f.End,
pipeline: f.Pipeline.ForStream(labels),
})
}
}
return &filteringStreamExtractor{
filters: streamFilters,
extractor: p.extractor.ForStream(labels),
}
}
type filteringStreamExtractor struct {
filters []streamFilter
extractor StreamSampleExtractor
}
func (sp *filteringStreamExtractor) BaseLabels() LabelsResult {
return sp.extractor.BaseLabels()
}
func (sp *filteringStreamExtractor) Process(ts int64, line []byte, structuredMetadata ...labels.Label) (float64, LabelsResult, bool) {
for _, filter := range sp.filters {
if ts < filter.start || ts > filter.end {
continue
}
_, _, matches := filter.pipeline.Process(ts, line, structuredMetadata...)
if matches { // When the filter matches, don't run the next step
return 0, nil, false
}
}
return sp.extractor.Process(ts, line)
}
func (sp *filteringStreamExtractor) ProcessString(ts int64, line string, structuredMetadata ...labels.Label) (float64, LabelsResult, bool) {
for _, filter := range sp.filters {
if ts < filter.start || ts > filter.end {
continue
}
_, _, matches := filter.pipeline.ProcessString(ts, line, structuredMetadata...)
if matches { // When the filter matches, don't run the next step
return 0, nil, false
}
}
return sp.extractor.ProcessString(ts, line)
}
func convertFloat(v string) (float64, error) {
return strconv.ParseFloat(v, 64)
}
func convertDuration(v string) (float64, error) {
d, err := time.ParseDuration(v)
if err != nil {
return 0, err
}
return d.Seconds(), nil
}
func convertBytes(v string) (float64, error) {
b, err := humanize.ParseBytes(v)
if err != nil {
return 0, err
}
return float64(b), nil
}