forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loki.go
70 lines (62 loc) · 1.67 KB
/
loki.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
package main
import (
"bytes"
"github.com/docker/docker/daemon/logger"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/grafana/loki/pkg/logentry/stages"
"github.com/grafana/loki/pkg/promtail/api"
"github.com/grafana/loki/pkg/promtail/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
)
var jobName = "docker"
type loki struct {
client client.Client
handler api.EntryHandler
labels model.LabelSet
logger log.Logger
}
// New create a new Loki logger that forward logs to Loki instance
func New(logCtx logger.Info, logger log.Logger) (logger.Logger, error) {
logger = log.With(logger, "container_id", logCtx.ContainerID)
cfg, err := parseConfig(logCtx)
if err != nil {
return nil, err
}
c, err := client.New(cfg.clientConfig, logger)
if err != nil {
return nil, err
}
var handler api.EntryHandler = c
if len(cfg.pipeline.PipelineStages) != 0 {
pipeline, err := stages.NewPipeline(logger, cfg.pipeline.PipelineStages, &jobName, prometheus.DefaultRegisterer)
if err != nil {
return nil, err
}
handler = pipeline.Wrap(c)
}
return &loki{
client: c,
labels: cfg.labels,
logger: logger,
handler: handler,
}, nil
}
// Log implements `logger.Logger`
func (l *loki) Log(m *logger.Message) error {
if len(bytes.Fields(m.Line)) == 0 {
level.Info(l.logger).Log("msg", "ignoring empty line", "line", string(m.Line))
return nil
}
return l.handler.Handle(l.labels.Clone(), m.Timestamp, string(m.Line))
}
// Log implements `logger.Logger`
func (l *loki) Name() string {
return driverName
}
// Log implements `logger.Logger`
func (l *loki) Close() error {
l.client.Stop()
return nil
}