forked from splunk/docker-logging-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
185 lines (160 loc) · 4.55 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
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright 2018 Splunk, Inc..
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package splunk provides the log driver for forwarding server logs to
// Splunk HTTP Event Collector endpoint.
package main
import (
"context"
"encoding/binary"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"syscall"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types/plugins/logdriver"
"github.com/docker/docker/daemon/logger"
"github.com/docker/docker/daemon/logger/jsonfilelog"
protoio "github.com/gogo/protobuf/io"
"github.com/pkg/errors"
"github.com/tonistiigi/fifo"
)
type driver struct {
mu sync.Mutex
logs map[string]*logPair // map for file and logger
idx map[string]*logPair // map for container_id and logger
logger logger.Logger
}
type logPair struct {
jsonl logger.Logger
splunkl logger.Logger
stream io.ReadCloser
info logger.Info
}
func (lf *logPair) Close() {
lf.stream.Close()
lf.splunkl.Close()
lf.jsonl.Close()
}
func newDriver() *driver {
return &driver{
logs: make(map[string]*logPair),
idx: make(map[string]*logPair),
}
}
func (d *driver) StartLogging(file string, logCtx logger.Info) error {
d.mu.Lock()
// check if the specific file is already attached to a logger
if _, exists := d.logs[file]; exists {
d.mu.Unlock()
return fmt.Errorf("logger for %q already exists", file)
}
d.mu.Unlock()
// if there isn't a logger for the file, create a logger hanlder
if logCtx.LogPath == "" {
logCtx.LogPath = filepath.Join("/var/log/docker", logCtx.ContainerID)
}
if err := os.MkdirAll(filepath.Dir(logCtx.LogPath), 0755); err != nil {
return errors.Wrap(err, "error setting up logger dir")
}
//create a json logger for the file
jsonl, err := jsonfilelog.New(logCtx)
if err != nil {
return errors.Wrap(err, "error creating jsonfile logger")
}
err = ValidateLogOpt(logCtx.Config)
if err != nil {
return errors.Wrapf(err, "error options logger splunk: %q", file)
}
//create a splunk logger for the file
splunkl, err := New(logCtx)
if err != nil {
return errors.Wrap(err, "error creating splunk logger")
}
logrus.WithField("id", logCtx.ContainerID).WithField("file", file).WithField("logpath", logCtx.LogPath).Debugf("Start logging")
// open the log file in the background with read only access
f, err := fifo.OpenFifo(context.Background(), file, syscall.O_RDONLY, 0700)
if err != nil {
return errors.Wrapf(err, "error opening logger fifo: %q", file)
}
d.mu.Lock()
lf := &logPair{jsonl, splunkl, f, logCtx}
// add the json logger, splunk logger, log file, and logCtx to the logging driver
d.logs[file] = lf
d.idx[logCtx.ContainerID] = lf
d.mu.Unlock()
// start to process the logs generated by docker
logrus.Debug("Start processing messages")
mg := &messageProcessor{
retryNumber: getAdvancedOptionInt(envVarReadFifoErrorRetryNumber, defaultReadFifoErrorRetryNumber),
}
go mg.process(lf)
return nil
}
func (d *driver) StopLogging(file string) error {
logrus.WithField("file", file).Debug("Stop logging")
d.mu.Lock()
lf, ok := d.logs[file]
if ok {
lf.Close()
delete(d.logs, file)
}
d.mu.Unlock()
return nil
}
func (d *driver) ReadLogs(info logger.Info, config logger.ReadConfig) (io.ReadCloser, error) {
d.mu.Lock()
lf, exists := d.idx[info.ContainerID]
d.mu.Unlock()
if !exists {
return nil, fmt.Errorf("logger does not exist for %s", info.ContainerID)
}
r, w := io.Pipe()
lr, ok := lf.jsonl.(logger.LogReader)
if !ok {
return nil, fmt.Errorf("logger does not support reading")
}
go func() {
watcher := lr.ReadLogs(config)
enc := protoio.NewUint32DelimitedWriter(w, binary.BigEndian)
defer enc.Close()
defer watcher.Close()
var buf logdriver.LogEntry
for {
select {
case msg, ok := <-watcher.Msg:
if !ok {
w.Close()
return
}
buf.Line = msg.Line
buf.Partial = msg.Partial
buf.TimeNano = msg.Timestamp.UnixNano()
buf.Source = msg.Source
if err := enc.WriteMsg(&buf); err != nil {
w.CloseWithError(err)
return
}
case err := <-watcher.Err:
w.CloseWithError(err)
return
}
buf.Reset()
}
}()
return r, nil
}