forked from buzztaiki/docker-logging-plugin-tee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
172 lines (149 loc) · 3.62 KB
/
driver.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
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"syscall"
"time"
"github.com/containerd/fifo"
"github.com/docker/docker/api/types/plugins/logdriver"
"github.com/docker/docker/daemon/logger"
"github.com/pkg/errors"
)
type driver struct {
loggers map[string]logger.Logger
cancels map[string]context.CancelFunc
mu sync.Mutex
}
func newDriver() *driver {
return &driver{
loggers: map[string]logger.Logger{},
cancels: map[string]context.CancelFunc{},
}
}
func (d *driver) startLogging(file string, info logger.Info) error {
if info.LogPath == "" {
logDir := "/var/log/docker"
if err := os.MkdirAll(logDir, 0700); err != nil {
return errors.Wrap(err, "failed to create log dir")
}
info.LogPath = filepath.Join(logDir, info.ContainerID)
}
l, err := newTeeLogger(info)
if err != nil {
return errors.Wrap(err, "failed to create logger")
}
r, err := openFifo(file)
if err != nil {
return errors.Wrap(err, "failed to open fifo")
}
ctx, cancel := context.WithCancel(context.Background())
d.mu.Lock()
d.loggers[info.ContainerID] = l
d.cancels[file] = cancel
d.mu.Unlock()
go doLog(ctx, r, l)
return nil
}
func (d *driver) stopLogging(file string) error {
d.mu.Lock()
cancel, ok := d.cancels[file]
d.mu.Unlock()
if !ok {
return fmt.Errorf("cancel for %s not found", file)
}
cancel()
return nil
}
func (d *driver) readLogs(ctx context.Context, info logger.Info, config logger.ReadConfig) (io.ReadCloser, error) {
d.mu.Lock()
l, ok := d.loggers[info.ContainerID]
d.mu.Unlock()
if !ok {
return nil, fmt.Errorf("logger for %s not found", info.ContainerID)
}
lr, ok := l.(logger.LogReader)
if !ok {
return nil, fmt.Errorf("logger for %s not readable", info.ContainerID)
}
r, w := io.Pipe()
go doReadLogs(ctx, lr, config, w)
return r, nil
}
func (d *driver) capabilities() logger.Capability {
return logger.Capability{ReadLogs: true}
}
func openFifo(file string) (io.ReadCloser, error) {
return fifo.OpenFifo(context.Background(), file, syscall.O_RDONLY, 0)
}
func doLog(ctx context.Context, r io.ReadCloser, l logger.Logger) {
defer r.Close()
defer l.Close()
dec := logdriver.NewLogEntryDecoder(r)
var buf logdriver.LogEntry
for {
select {
case <-ctx.Done():
log.Info("done doLog")
return
default:
if err := dec.Decode(&buf); err != nil {
if err == io.EOF {
log.Info("stop doLog")
return
}
log.WithError(err).Error("failed to write log")
return
}
msg := logger.NewMessage()
msg.Timestamp = time.Unix(0, buf.TimeNano)
msg.Line = buf.Line
msg.Source = buf.Source
l.Log(msg)
}
}
}
func doReadLogs(ctx context.Context, lr logger.LogReader, config logger.ReadConfig, w io.WriteCloser) {
defer w.Close()
watcher := lr.ReadLogs(config)
defer watcher.ConsumerGone()
enc := logdriver.NewLogEntryEncoder(w)
var buf logdriver.LogEntry
for {
select {
case <-ctx.Done():
log.Info("done reading")
return
case msg, ok := <-watcher.Msg:
if !ok {
log.Info("stop reading")
return
}
buf.Line = msg.Line
buf.TimeNano = msg.Timestamp.UnixNano()
if msg.PLogMetaData != nil {
buf.Partial = true
buf.PartialLogMetadata = &logdriver.PartialLogEntryMetadata{
Id: msg.PLogMetaData.ID,
Last: msg.PLogMetaData.Last,
Ordinal: int32(msg.PLogMetaData.Ordinal),
}
}
buf.Source = msg.Source
if err := enc.Encode(&buf); err != nil {
log.WithError(err).Error("encode error")
return
}
buf.Reset()
case err := <-watcher.Err:
log.WithError(err).Error("watcher error")
return
case <-watcher.WatchProducerGone():
log.Info("producer gone")
return
}
}
}