/
journaltarget.go
206 lines (170 loc) · 5.04 KB
/
journaltarget.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// +build linux,cgo
package targets
import (
"fmt"
"io"
"io/ioutil"
"strings"
"time"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/relabel"
"github.com/go-kit/kit/log/level"
"github.com/grafana/loki/pkg/promtail/positions"
"github.com/go-kit/kit/log"
"github.com/grafana/loki/pkg/promtail/scrape"
"github.com/coreos/go-systemd/sdjournal"
"github.com/grafana/loki/pkg/promtail/api"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
)
const (
// journalEmptyStr is represented as a single-character space because
// returning an empty string from sdjournal.JournalReaderConfig's
// Formatter causes an immediate EOF and induces performance issues
// with how that is handled in sdjournal.
journalEmptyStr = " "
)
type journalReader interface {
io.Closer
Follow(until <-chan time.Time, writer io.Writer) error
}
type journalReaderFunc func(sdjournal.JournalReaderConfig) (journalReader, error)
var defaultJournalReaderFunc = func(c sdjournal.JournalReaderConfig) (journalReader, error) {
return sdjournal.NewJournalReader(c)
}
// JournalTarget tails systemd journal entries.
type JournalTarget struct {
logger log.Logger
handler api.EntryHandler
positions *positions.Positions
positionPath string
relabelConfig []*relabel.Config
config *scrape.JournalTargetConfig
labels model.LabelSet
r journalReader
until chan time.Time
}
// NewJournalTarget configures a new JournalTarget.
func NewJournalTarget(
logger log.Logger,
handler api.EntryHandler,
positions *positions.Positions,
jobName string,
relabelConfig []*relabel.Config,
targetConfig *scrape.JournalTargetConfig,
) (*JournalTarget, error) {
return journalTargetWithReader(
logger,
handler,
positions,
jobName,
relabelConfig,
targetConfig,
defaultJournalReaderFunc,
)
}
func journalTargetWithReader(
logger log.Logger,
handler api.EntryHandler,
positions *positions.Positions,
jobName string,
relabelConfig []*relabel.Config,
targetConfig *scrape.JournalTargetConfig,
readerFunc journalReaderFunc,
) (*JournalTarget, error) {
positionPath := fmt.Sprintf("journal-%s", jobName)
position := positions.GetString(positionPath)
if readerFunc == nil {
readerFunc = defaultJournalReaderFunc
}
until := make(chan time.Time)
t := &JournalTarget{
logger: logger,
handler: handler,
positions: positions,
positionPath: positionPath,
relabelConfig: relabelConfig,
labels: targetConfig.Labels,
config: targetConfig,
until: until,
}
// Default to system path if not defined. Passing an empty string to
// sdjournal is valid but forces reads from the journal to be from
// the local machine id only, which contradicts the default behavior
// of when a path is specified. To standardize, we manually default the
// path here.
journalPath := targetConfig.Path
if journalPath == "" {
journalPath = "/var/log/journal"
}
var err error
t.r, err = readerFunc(sdjournal.JournalReaderConfig{
Path: journalPath,
Cursor: position,
Formatter: t.formatter,
})
if err != nil {
return nil, errors.Wrap(err, "creating journal reader")
}
go func() {
err := t.r.Follow(until, ioutil.Discard)
if err != nil && err != sdjournal.ErrExpired {
level.Error(t.logger).Log("msg", "received error during sdjournal follow", "err", err.Error())
}
}()
return t, nil
}
func (t *JournalTarget) formatter(entry *sdjournal.JournalEntry) (string, error) {
ts := time.Unix(0, int64(entry.RealtimeTimestamp)*int64(time.Microsecond))
msg, ok := entry.Fields["MESSAGE"]
if !ok {
level.Debug(t.logger).Log("msg", "received journal entry with no MESSAGE field")
return journalEmptyStr, nil
}
entryLabels := makeJournalFields(entry.Fields)
// Add constant labels
for k, v := range t.labels {
entryLabels[string(k)] = string(v)
}
processedLabels := relabel.Process(labels.FromMap(entryLabels), t.relabelConfig...)
processedLabelsMap := processedLabels.Map()
labels := make(model.LabelSet, len(processedLabelsMap))
for k, v := range processedLabelsMap {
if k[0:2] == "__" {
continue
}
labels[model.LabelName(k)] = model.LabelValue(v)
}
if len(labels) == 0 {
// No labels, drop journal entry
return journalEmptyStr, nil
}
t.positions.PutString(t.positionPath, entry.Cursor)
err := t.handler.Handle(labels, ts, msg)
return journalEmptyStr, err
}
// Type returns JournalTargetType.
func (t *JournalTarget) Type() TargetType {
return JournalTargetType
}
// Ready indicates whether or not the journal is ready to be
// read from.
func (t *JournalTarget) Ready() bool {
return true
}
// Details returns target-specific details (currently nil).
func (t *JournalTarget) Details() interface{} {
return nil
}
// Stop shuts down the JournalTarget.
func (t *JournalTarget) Stop() error {
t.until <- time.Now()
return t.r.Close()
}
func makeJournalFields(fields map[string]string) map[string]string {
result := make(map[string]string, len(fields))
for k, v := range fields {
result[fmt.Sprintf("__journal_%s", strings.ToLower(k))] = v
}
return result
}