-
Notifications
You must be signed in to change notification settings - Fork 479
/
static_labels.go
80 lines (69 loc) · 2.07 KB
/
static_labels.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
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"
"fmt"
"reflect"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/common/model"
)
// ErrEmptyStaticLabelStageConfig error returned if the config is empty.
var ErrEmptyStaticLabelStageConfig = errors.New("static_labels stage config cannot be empty")
// StaticLabelsConfig contains a map of static labels to be set.
type StaticLabelsConfig struct {
Values map[string]*string `river:"values,attr"`
}
func newStaticLabelsStage(logger log.Logger, config StaticLabelsConfig) (Stage, error) {
err := validateLabelStaticConfig(config)
if err != nil {
return nil, err
}
return toStage(&staticLabelStage{
Config: config,
logger: logger,
}), nil
}
func validateLabelStaticConfig(c StaticLabelsConfig) error {
if c.Values == nil {
return ErrEmptyStaticLabelStageConfig
}
for labelName := range c.Values {
if !model.LabelName(labelName).IsValid() {
return fmt.Errorf(ErrInvalidLabelName, labelName)
}
}
return nil
}
// staticLabelStage implements Stage.
type staticLabelStage struct {
Config StaticLabelsConfig
logger log.Logger
}
// Process implements Stage.
func (l *staticLabelStage) Process(labels model.LabelSet, extracted map[string]interface{}, t *time.Time, entry *string) {
for lName, lSrc := range l.Config.Values {
if lSrc == nil || *lSrc == "" {
continue
}
s, err := getString(*lSrc)
if err != nil {
level.Debug(l.logger).Log("msg", "failed to convert static label value to string", "err", err, "type", reflect.TypeOf(lSrc))
continue
}
lvalue := model.LabelValue(s)
if !lvalue.IsValid() {
level.Debug(l.logger).Log("msg", "invalid label value parsed", "value", lvalue)
continue
}
lname := model.LabelName(lName)
labels[lname] = lvalue
}
}
// Name implements Stage.
func (l *staticLabelStage) Name() string {
return StageTypeStaticLabels
}