forked from grafana/loki
/
output.go
83 lines (74 loc) · 1.88 KB
/
output.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
package stages
import (
"errors"
"reflect"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/mitchellh/mapstructure"
"github.com/prometheus/common/model"
)
// Config Errors
const (
ErrEmptyOutputStageConfig = "output stage config cannot be empty"
ErrOutputSourceRequired = "output source value is required if output is specified"
)
// OutputConfig configures output value extraction
type OutputConfig struct {
Source string `mapstructure:"source"`
}
// validateOutput validates the outputStage config
func validateOutputConfig(cfg *OutputConfig) error {
if cfg == nil {
return errors.New(ErrEmptyOutputStageConfig)
}
if cfg.Source == "" {
return errors.New(ErrOutputSourceRequired)
}
return nil
}
// newOutputStage creates a new outputStage
func newOutputStage(logger log.Logger, config interface{}) (*outputStage, error) {
cfg := &OutputConfig{}
err := mapstructure.Decode(config, cfg)
if err != nil {
return nil, err
}
err = validateOutputConfig(cfg)
if err != nil {
return nil, err
}
return &outputStage{
cfgs: cfg,
logger: logger,
}, nil
}
// outputStage will mutate the incoming entry and set it from extracted data
type outputStage struct {
cfgs *OutputConfig
logger log.Logger
}
// Process implements Stage
func (o *outputStage) Process(labels model.LabelSet, extracted map[string]interface{}, t *time.Time, entry *string) {
if o.cfgs == nil {
return
}
if v, ok := extracted[o.cfgs.Source]; ok {
s, err := getString(v)
if err != nil {
if Debug {
level.Debug(o.logger).Log("msg", "extracted output could not be converted to a string", "err", err, "type", reflect.TypeOf(v).String())
}
return
}
*entry = s
} else {
if Debug {
level.Debug(o.logger).Log("msg", "extracted data did not contain output source")
}
}
}
// Name implements Stage
func (o *outputStage) Name() string {
return StageTypeOutput
}