2015-07-21 18:26:52 -04:00
|
|
|
// Package fluentd provides the log driver for forwarding server logs
|
|
|
|
// to fluentd endpoints.
|
2018-02-05 16:05:59 -05:00
|
|
|
package fluentd // import "github.com/docker/docker/daemon/logger/fluentd"
|
2015-06-20 00:07:50 -04:00
|
|
|
|
|
|
|
import (
|
2015-06-30 20:40:13 -04:00
|
|
|
"fmt"
|
2015-06-20 00:07:50 -04:00
|
|
|
"math"
|
|
|
|
"net"
|
2016-08-28 10:24:56 -04:00
|
|
|
"net/url"
|
2015-06-20 00:07:50 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-01-24 08:51:03 -05:00
|
|
|
"time"
|
2015-06-20 00:07:50 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
2015-08-06 18:50:44 -04:00
|
|
|
"github.com/docker/docker/daemon/logger/loggerutils"
|
2016-08-28 10:24:56 -04:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2016-01-24 08:51:03 -05:00
|
|
|
"github.com/docker/go-units"
|
2015-06-20 00:07:50 -04:00
|
|
|
"github.com/fluent/fluent-logger-golang/fluent"
|
2016-08-28 10:24:56 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-06-20 00:07:50 -04:00
|
|
|
)
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
type fluentd struct {
|
2015-06-20 00:07:50 -04:00
|
|
|
tag string
|
|
|
|
containerID string
|
|
|
|
containerName string
|
|
|
|
writer *fluent.Fluent
|
2015-10-04 17:05:43 -04:00
|
|
|
extra map[string]string
|
2015-06-20 00:07:50 -04:00
|
|
|
}
|
|
|
|
|
2016-08-28 10:24:56 -04:00
|
|
|
type location struct {
|
|
|
|
protocol string
|
|
|
|
host string
|
|
|
|
port int
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2015-06-20 00:07:50 -04:00
|
|
|
const (
|
2016-01-24 08:51:03 -05:00
|
|
|
name = "fluentd"
|
|
|
|
|
2016-08-28 10:24:56 -04:00
|
|
|
defaultProtocol = "tcp"
|
2016-01-24 08:51:03 -05:00
|
|
|
defaultHost = "127.0.0.1"
|
2016-03-16 22:15:14 -04:00
|
|
|
defaultPort = 24224
|
2016-01-24 08:51:03 -05:00
|
|
|
defaultBufferLimit = 1024 * 1024
|
|
|
|
|
|
|
|
// logger tries to reconnect 2**32 - 1 times
|
|
|
|
// failed (and panic) after 204 years [ 1.5 ** (2**32 - 1) - 1 seconds]
|
2016-11-02 07:02:43 -04:00
|
|
|
defaultRetryWait = 1000
|
|
|
|
defaultMaxRetries = math.MaxInt32
|
2016-01-24 08:51:03 -05:00
|
|
|
|
2017-11-16 13:17:35 -05:00
|
|
|
addressKey = "fluentd-address"
|
|
|
|
bufferLimitKey = "fluentd-buffer-limit"
|
|
|
|
retryWaitKey = "fluentd-retry-wait"
|
|
|
|
maxRetriesKey = "fluentd-max-retries"
|
|
|
|
asyncConnectKey = "fluentd-async-connect"
|
|
|
|
subSecondPrecisionKey = "fluentd-sub-second-precision"
|
2015-06-20 00:07:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if err := logger.RegisterLogDriver(name, New); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2015-06-30 20:40:13 -04:00
|
|
|
if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2015-06-20 00:07:50 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
// New creates a fluentd logger using the configuration passed in on
|
2016-05-09 22:04:09 -04:00
|
|
|
// the context. The supported context configuration variable is
|
|
|
|
// fluentd-address.
|
2016-11-26 00:08:34 -05:00
|
|
|
func New(info logger.Info) (logger.Logger, error) {
|
|
|
|
loc, err := parseAddress(info.Config[addressKey])
|
2015-09-20 07:03:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:08:34 -05:00
|
|
|
tag, err := loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate)
|
2015-06-20 00:07:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-24 08:51:03 -05:00
|
|
|
|
2016-11-08 19:34:47 -05:00
|
|
|
extra, err := info.ExtraAttributes(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-24 08:51:03 -05:00
|
|
|
|
|
|
|
bufferLimit := defaultBufferLimit
|
2016-11-26 00:08:34 -05:00
|
|
|
if info.Config[bufferLimitKey] != "" {
|
|
|
|
bl64, err := units.RAMInBytes(info.Config[bufferLimitKey])
|
2016-01-24 08:51:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bufferLimit = int(bl64)
|
|
|
|
}
|
|
|
|
|
|
|
|
retryWait := defaultRetryWait
|
2016-11-26 00:08:34 -05:00
|
|
|
if info.Config[retryWaitKey] != "" {
|
|
|
|
rwd, err := time.ParseDuration(info.Config[retryWaitKey])
|
2016-01-24 08:51:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
retryWait = int(rwd.Seconds() * 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxRetries := defaultMaxRetries
|
2016-11-26 00:08:34 -05:00
|
|
|
if info.Config[maxRetriesKey] != "" {
|
|
|
|
mr64, err := strconv.ParseUint(info.Config[maxRetriesKey], 10, strconv.IntSize)
|
2016-01-24 08:51:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
maxRetries = int(mr64)
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncConnect := false
|
2016-11-26 00:08:34 -05:00
|
|
|
if info.Config[asyncConnectKey] != "" {
|
|
|
|
if asyncConnect, err = strconv.ParseBool(info.Config[asyncConnectKey]); err != nil {
|
2016-01-24 08:51:03 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 13:17:35 -05:00
|
|
|
subSecondPrecision := false
|
|
|
|
if info.Config[subSecondPrecisionKey] != "" {
|
|
|
|
if subSecondPrecision, err = strconv.ParseBool(info.Config[subSecondPrecisionKey]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 08:51:03 -05:00
|
|
|
fluentConfig := fluent.Config{
|
2017-11-16 13:17:35 -05:00
|
|
|
FluentPort: loc.port,
|
|
|
|
FluentHost: loc.host,
|
|
|
|
FluentNetwork: loc.protocol,
|
|
|
|
FluentSocketPath: loc.path,
|
|
|
|
BufferLimit: bufferLimit,
|
|
|
|
RetryWait: retryWait,
|
|
|
|
MaxRetry: maxRetries,
|
2019-04-14 20:11:27 -04:00
|
|
|
Async: asyncConnect,
|
2017-11-16 13:17:35 -05:00
|
|
|
SubSecondPrecision: subSecondPrecision,
|
2016-01-24 08:51:03 -05:00
|
|
|
}
|
|
|
|
|
2016-11-26 00:08:34 -05:00
|
|
|
logrus.WithField("container", info.ContainerID).WithField("config", fluentConfig).
|
2016-01-24 08:51:03 -05:00
|
|
|
Debug("logging driver fluentd configured")
|
|
|
|
|
|
|
|
log, err := fluent.New(fluentConfig)
|
2015-06-20 00:07:50 -04:00
|
|
|
if err != nil {
|
2016-01-29 19:05:46 -05:00
|
|
|
return nil, err
|
2015-06-20 00:07:50 -04:00
|
|
|
}
|
2015-07-21 18:26:52 -04:00
|
|
|
return &fluentd{
|
2015-06-20 00:07:50 -04:00
|
|
|
tag: tag,
|
2016-11-26 00:08:34 -05:00
|
|
|
containerID: info.ContainerID,
|
|
|
|
containerName: info.ContainerName,
|
2015-06-20 00:07:50 -04:00
|
|
|
writer: log,
|
2015-10-04 17:05:43 -04:00
|
|
|
extra: extra,
|
2015-06-20 00:07:50 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
func (f *fluentd) Log(msg *logger.Message) error {
|
2015-06-20 00:07:50 -04:00
|
|
|
data := map[string]string{
|
|
|
|
"container_id": f.containerID,
|
|
|
|
"container_name": f.containerName,
|
|
|
|
"source": msg.Source,
|
|
|
|
"log": string(msg.Line),
|
|
|
|
}
|
2015-10-04 17:05:43 -04:00
|
|
|
for k, v := range f.extra {
|
|
|
|
data[k] = v
|
|
|
|
}
|
2018-05-09 19:55:27 -04:00
|
|
|
if msg.PLogMetaData != nil {
|
2018-01-30 03:56:30 -05:00
|
|
|
data["partial_message"] = "true"
|
2019-03-27 06:35:04 -04:00
|
|
|
data["partial_id"] = msg.PLogMetaData.ID
|
|
|
|
data["partial_ordinal"] = strconv.Itoa(msg.PLogMetaData.Ordinal)
|
|
|
|
data["partial_last"] = strconv.FormatBool(msg.PLogMetaData.Last)
|
2018-01-30 03:56:30 -05:00
|
|
|
}
|
2016-12-12 09:54:20 -05:00
|
|
|
|
|
|
|
ts := msg.Timestamp
|
|
|
|
logger.PutMessage(msg)
|
2015-06-20 00:07:50 -04:00
|
|
|
// fluent-logger-golang buffers logs from failures and disconnections,
|
|
|
|
// and these are transferred again automatically.
|
2016-12-12 09:54:20 -05:00
|
|
|
return f.writer.PostWithTime(f.tag, ts, data)
|
2015-06-20 00:07:50 -04:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:03:09 -04:00
|
|
|
func (f *fluentd) Close() error {
|
|
|
|
return f.writer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fluentd) Name() string {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:04:09 -04:00
|
|
|
// ValidateLogOpt looks for fluentd specific log option fluentd-address.
|
2015-06-30 20:40:13 -04:00
|
|
|
func ValidateLogOpt(cfg map[string]string) error {
|
|
|
|
for key := range cfg {
|
|
|
|
switch key {
|
2016-01-24 08:51:03 -05:00
|
|
|
case "env":
|
2016-11-08 19:34:47 -05:00
|
|
|
case "env-regex":
|
2015-10-04 17:05:43 -04:00
|
|
|
case "labels":
|
2019-01-16 16:52:22 -05:00
|
|
|
case "labels-regex":
|
2016-01-24 08:51:03 -05:00
|
|
|
case "tag":
|
|
|
|
case addressKey:
|
|
|
|
case bufferLimitKey:
|
|
|
|
case retryWaitKey:
|
|
|
|
case maxRetriesKey:
|
|
|
|
case asyncConnectKey:
|
2017-11-16 13:17:35 -05:00
|
|
|
case subSecondPrecisionKey:
|
2016-01-24 08:51:03 -05:00
|
|
|
// Accepted
|
2015-06-30 20:40:13 -04:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown log opt '%s' for fluentd log driver", key)
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 07:03:09 -04:00
|
|
|
|
2017-07-17 01:56:52 -04:00
|
|
|
_, err := parseAddress(cfg[addressKey])
|
2016-12-14 15:36:58 -05:00
|
|
|
return err
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
|
|
|
|
2016-08-28 10:24:56 -04:00
|
|
|
func parseAddress(address string) (*location, error) {
|
2015-09-20 07:03:09 -04:00
|
|
|
if address == "" {
|
2016-08-28 10:24:56 -04:00
|
|
|
return &location{
|
|
|
|
protocol: defaultProtocol,
|
|
|
|
host: defaultHost,
|
|
|
|
port: defaultPort,
|
|
|
|
path: "",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol := defaultProtocol
|
|
|
|
givenAddress := address
|
|
|
|
if urlutil.IsTransportURL(address) {
|
|
|
|
url, err := url.Parse(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "invalid fluentd-address %s", givenAddress)
|
|
|
|
}
|
|
|
|
// unix and unixgram socket
|
|
|
|
if url.Scheme == "unix" || url.Scheme == "unixgram" {
|
|
|
|
return &location{
|
|
|
|
protocol: url.Scheme,
|
|
|
|
host: "",
|
|
|
|
port: 0,
|
|
|
|
path: url.Path,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
// tcp|udp
|
|
|
|
protocol = url.Scheme
|
|
|
|
address = url.Host
|
2015-09-20 07:03:09 -04:00
|
|
|
}
|
2015-06-20 00:07:50 -04:00
|
|
|
|
2015-09-20 07:03:09 -04:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "missing port in address") {
|
2016-08-28 10:24:56 -04:00
|
|
|
return nil, errors.Wrapf(err, "invalid fluentd-address %s", givenAddress)
|
2015-09-20 07:03:09 -04:00
|
|
|
}
|
2016-08-28 10:24:56 -04:00
|
|
|
return &location{
|
|
|
|
protocol: protocol,
|
|
|
|
host: host,
|
|
|
|
port: defaultPort,
|
|
|
|
path: "",
|
|
|
|
}, nil
|
2015-09-20 07:03:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
portnum, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
2016-08-28 10:24:56 -04:00
|
|
|
return nil, errors.Wrapf(err, "invalid fluentd-address %s", givenAddress)
|
2015-09-20 07:03:09 -04:00
|
|
|
}
|
2016-08-28 10:24:56 -04:00
|
|
|
return &location{
|
|
|
|
protocol: protocol,
|
|
|
|
host: host,
|
|
|
|
port: portnum,
|
|
|
|
path: "",
|
|
|
|
}, nil
|
2015-06-20 00:07:50 -04:00
|
|
|
}
|