forked from grafana/loki
/
labeldrop.go
58 lines (46 loc) · 1.17 KB
/
labeldrop.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
package stages
import (
"time"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
)
const (
// ErrEmptyLabelDropStageConfig error returned if config is empty
ErrEmptyLabelDropStageConfig = "labeldrop stage config cannot be empty"
)
// LabelDropConfig is a slice of labels to be dropped
type LabelDropConfig []string
func validateLabelDropConfig(c LabelDropConfig) error {
if c == nil || len(c) < 1 {
return errors.New(ErrEmptyLabelDropStageConfig)
}
return nil
}
func newLabelDropStage(configs interface{}) (Stage, error) {
cfgs := &LabelDropConfig{}
err := mapstructure.Decode(configs, cfgs)
if err != nil {
return nil, err
}
err = validateLabelDropConfig(*cfgs)
if err != nil {
return nil, err
}
return toStage(&labelDropStage{
cfgs: *cfgs,
}), nil
}
type labelDropStage struct {
cfgs LabelDropConfig
}
// Process implements Stage
func (l *labelDropStage) Process(labels model.LabelSet, _ map[string]interface{}, _ *time.Time, _ *string) {
for _, label := range l.cfgs {
delete(labels, model.LabelName(label))
}
}
// Name implements Stage
func (l *labelDropStage) Name() string {
return StageTypeLabelDrop
}