-
Notifications
You must be signed in to change notification settings - Fork 479
/
label_keep.go
53 lines (42 loc) · 1.33 KB
/
label_keep.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
package stages
// This package is ported over from grafana/loki/clients/pkg/logentry/stages.
// We aim to port the stages in steps, to avoid introducing huge amounts of
// new code without being able to slowly review, examine and test them.
import (
"errors"
"time"
"github.com/prometheus/common/model"
)
// ErrEmptyLabelAllowStageConfig error is returned if the config is empty.
var ErrEmptyLabelAllowStageConfig = errors.New("labelallow stage config cannot be empty")
// LabelAllowConfig contains the slice of labels to allow through.
type LabelAllowConfig struct {
Values []string `river:"values,attr"`
}
func newLabelAllowStage(config LabelAllowConfig) (Stage, error) {
if len(config.Values) < 1 {
return nil, ErrEmptyLabelAllowStageConfig
}
labelMap := make(map[string]struct{})
for _, label := range config.Values {
labelMap[label] = struct{}{}
}
return toStage(&labelAllowStage{
labels: labelMap,
}), nil
}
type labelAllowStage struct {
labels map[string]struct{}
}
// Process implements Stage.
func (l *labelAllowStage) Process(labels model.LabelSet, extracted map[string]interface{}, t *time.Time, entry *string) {
for label := range labels {
if _, ok := l.labels[string(label)]; !ok {
delete(labels, label)
}
}
}
// Name implements Stage.
func (l *labelAllowStage) Name() string {
return StageTypeLabelAllow
}