-
Notifications
You must be signed in to change notification settings - Fork 479
/
output.go
63 lines (54 loc) · 1.71 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
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"
"reflect"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/common/model"
)
// Config Errors.
var (
ErrEmptyOutputStageConfig = errors.New("output stage config cannot be empty")
ErrOutputSourceRequired = errors.New("output source value is required if output is specified")
)
// OutputConfig initializes a configuration stage which sets the log line to a
// value from the extracted map.
type OutputConfig struct {
Source string `river:"source,attr"`
}
// newOutputStage creates a new outputStage
func newOutputStage(logger log.Logger, config OutputConfig) (Stage, error) {
if config.Source == "" {
return nil, ErrOutputSourceRequired
}
return toStage(&outputStage{
config: config,
logger: logger,
}), nil
}
// outputStage will mutate the incoming entry and set it from extracted data
type outputStage struct {
config OutputConfig
logger log.Logger
}
// Process implements Stage
func (o *outputStage) Process(labels model.LabelSet, extracted map[string]interface{}, t *time.Time, entry *string) {
if v, ok := extracted[o.config.Source]; ok {
s, err := getString(v)
if err != nil {
level.Debug(o.logger).Log("msg", "extracted output could not be converted to a string", "err", err, "type", reflect.TypeOf(v))
return
}
*entry = s
} else {
level.Debug(o.logger).Log("msg", "extracted data did not contain output source")
}
}
// Name implements Stage
func (o *outputStage) Name() string {
return StageTypeOutput
}