-
Notifications
You must be signed in to change notification settings - Fork 95
/
target.go
300 lines (264 loc) · 8.22 KB
/
target.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package dockertarget
// NOTE: This code is adapted from Promtail (90a1d4593e2d690b37333386383870865fe177bf).
// The dockertarget package is used to configure and run the targets that can
// read logs from Docker containers and forward them to other loki components.
import (
"bufio"
"context"
"fmt"
"io"
"strconv"
"strings"
"sync"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/go-kit/log"
"github.com/grafana/alloy/internal/alloy/logging/level"
"github.com/grafana/alloy/internal/component/common/loki"
"github.com/grafana/alloy/internal/component/common/loki/positions"
"github.com/grafana/loki/pkg/logproto"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"go.uber.org/atomic"
)
const (
// See github.com/prometheus/prometheus/discovery/moby
dockerLabel = model.MetaLabelPrefix + "docker_"
dockerLabelContainerPrefix = dockerLabel + "container_"
dockerLabelLogStream = dockerLabelContainerPrefix + "log_stream"
)
// Target enables reading Docker container logs.
type Target struct {
logger log.Logger
handler loki.EntryHandler
since int64
positions positions.Positions
containerName string
labels model.LabelSet
labelsStr string
relabelConfig []*relabel.Config
metrics *Metrics
cancel context.CancelFunc
client client.APIClient
wg sync.WaitGroup
running *atomic.Bool
err error
}
// NewTarget starts a new target to read logs from a given container ID.
func NewTarget(metrics *Metrics, logger log.Logger, handler loki.EntryHandler, position positions.Positions, containerID string, labels model.LabelSet, relabelConfig []*relabel.Config, client client.APIClient) (*Target, error) {
labelsStr := labels.String()
pos, err := position.Get(positions.CursorKey(containerID), labelsStr)
if err != nil {
return nil, err
}
var since int64
if pos != 0 {
since = pos
}
t := &Target{
logger: logger,
handler: handler,
since: since,
positions: position,
containerName: containerID,
labels: labels,
labelsStr: labelsStr,
relabelConfig: relabelConfig,
metrics: metrics,
client: client,
running: atomic.NewBool(false),
}
// NOTE (@tpaschalis) The original Promtail implementation would call
// t.StartIfNotRunning() right here to start tailing.
// We manage targets from a task's Run method.
return t, nil
}
func (t *Target) processLoop(ctx context.Context) {
t.running.Store(true)
defer t.running.Store(false)
t.wg.Add(1)
defer t.wg.Done()
opts := container.LogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
Timestamps: true,
Since: strconv.FormatInt(t.since, 10),
}
inspectInfo, err := t.client.ContainerInspect(ctx, t.containerName)
if err != nil {
level.Error(t.logger).Log("msg", "could not inspect container info", "container", t.containerName, "err", err)
t.err = err
return
}
logs, err := t.client.ContainerLogs(ctx, t.containerName, opts)
if err != nil {
level.Error(t.logger).Log("msg", "could not fetch logs for container", "container", t.containerName, "err", err)
t.err = err
return
}
// Start transferring
rstdout, wstdout := io.Pipe()
rstderr, wstderr := io.Pipe()
t.wg.Add(1)
go func() {
defer func() {
t.wg.Done()
wstdout.Close()
wstderr.Close()
t.Stop()
}()
var written int64
var err error
if inspectInfo.Config.Tty {
written, err = io.Copy(wstdout, logs)
} else {
written, err = stdcopy.StdCopy(wstdout, wstderr, logs)
}
if err != nil {
level.Warn(t.logger).Log("msg", "could not transfer logs", "written", written, "container", t.containerName, "err", err)
} else {
level.Info(t.logger).Log("msg", "finished transferring logs", "written", written, "container", t.containerName)
}
}()
// Start processing
t.wg.Add(2)
go t.process(rstdout, t.getStreamLabels("stdout"))
go t.process(rstderr, t.getStreamLabels("stderr"))
// Wait until done
<-ctx.Done()
logs.Close()
level.Debug(t.logger).Log("msg", "done processing Docker logs", "container", t.containerName)
}
// extractTs tries for read the timestamp from the beginning of the log line.
// It's expected to follow the format 2006-01-02T15:04:05.999999999Z07:00.
func extractTs(line string) (time.Time, string, error) {
pair := strings.SplitN(line, " ", 2)
if len(pair) != 2 {
return time.Now(), line, fmt.Errorf("Could not find timestamp in '%s'", line)
}
ts, err := time.Parse("2006-01-02T15:04:05.999999999Z07:00", pair[0])
if err != nil {
return time.Now(), line, fmt.Errorf("Could not parse timestamp from '%s': %w", pair[0], err)
}
return ts, pair[1], nil
}
// https://devmarkpro.com/working-big-files-golang
func readLine(r *bufio.Reader) (string, error) {
var (
isPrefix = true
err error
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
func (t *Target) process(r io.Reader, logStreamLset model.LabelSet) {
defer func() {
t.wg.Done()
}()
reader := bufio.NewReader(r)
for {
line, err := readLine(reader)
if err != nil {
if err == io.EOF {
break
}
level.Error(t.logger).Log("msg", "error reading docker log line, skipping line", "err", err)
t.metrics.dockerErrors.Inc()
}
ts, line, err := extractTs(line)
if err != nil {
level.Error(t.logger).Log("msg", "could not extract timestamp, skipping line", "err", err)
t.metrics.dockerErrors.Inc()
continue
}
t.handler.Chan() <- loki.Entry{
Labels: logStreamLset,
Entry: logproto.Entry{
Timestamp: ts,
Line: line,
},
}
t.metrics.dockerEntries.Inc()
// NOTE(@tpaschalis) We don't save the positions entry with the
// filtered labels, but with the default label set, as this is the one
// used to find the original read offset from the client. This might be
// problematic if we have the same container with a different set of
// labels (e.g. duplicated and relabeled), but this shouldn't be the
// case anyway.
t.positions.Put(positions.CursorKey(t.containerName), t.labelsStr, ts.Unix())
t.since = ts.Unix()
}
}
// StartIfNotRunning starts processing container logs. The operation is idempotent , i.e. the processing cannot be started twice.
func (t *Target) StartIfNotRunning() {
if t.running.CompareAndSwap(false, true) {
level.Debug(t.logger).Log("msg", "starting process loop", "container", t.containerName)
ctx, cancel := context.WithCancel(context.Background())
t.cancel = cancel
go t.processLoop(ctx)
}
}
// Stop shuts down the target.
func (t *Target) Stop() {
t.cancel()
t.wg.Wait()
level.Debug(t.logger).Log("msg", "stopped Docker target", "container", t.containerName)
}
// Ready reports whether the target is running.
func (t *Target) Ready() bool {
return t.running.Load()
}
// LabelsStr returns the target's original labels string representation.
func (t *Target) LabelsStr() string {
return t.labelsStr
}
// Name reports the container name.
func (t *Target) Name() string {
return t.containerName
}
// Hash is used when comparing targets in tasks.
func (t *Target) Hash() uint64 {
return uint64(t.labels.Fingerprint())
}
// Path returns the target's container name.
func (t *Target) Path() string {
return t.containerName
}
// Details returns target-specific details.
func (t *Target) Details() map[string]string {
var errMsg string
if t.err != nil {
errMsg = t.err.Error()
}
return map[string]string{
"id": t.containerName,
"error": errMsg,
"position": t.positions.GetString(positions.CursorKey(t.containerName), t.labelsStr),
"running": strconv.FormatBool(t.running.Load()),
}
}
func (t *Target) getStreamLabels(logStream string) model.LabelSet {
// Add all labels from the config, relabel and filter them.
lb := labels.NewBuilder(nil)
for k, v := range t.labels {
lb.Set(string(k), string(v))
}
lb.Set(dockerLabelLogStream, logStream)
processed, _ := relabel.Process(lb.Labels(), t.relabelConfig...)
filtered := make(model.LabelSet)
for _, lbl := range processed {
if strings.HasPrefix(lbl.Name, "__") {
continue
}
filtered[model.LabelName(lbl.Name)] = model.LabelValue(lbl.Value)
}
return filtered
}