2015-07-21 18:26:52 -04:00
|
|
|
// Package fluentd provides the log driver for forwarding server logs
|
|
|
|
// to fluentd endpoints.
|
2015-06-20 00:07:50 -04:00
|
|
|
package fluentd
|
|
|
|
|
|
|
|
import (
|
2015-06-30 20:40:13 -04:00
|
|
|
"fmt"
|
2015-06-20 00:07:50 -04:00
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-01-24 08:51:03 -05:00
|
|
|
"time"
|
2015-06-20 00:07:50 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
2015-08-06 18:50:44 -04:00
|
|
|
"github.com/docker/docker/daemon/logger/loggerutils"
|
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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2016-01-24 08:51:03 -05:00
|
|
|
name = "fluentd"
|
|
|
|
|
|
|
|
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
|
2016-01-29 19:05:46 -05:00
|
|
|
defaultTagPrefix = "docker"
|
2016-01-24 08:51:03 -05:00
|
|
|
|
|
|
|
// logger tries to reconnect 2**32 - 1 times
|
|
|
|
// failed (and panic) after 204 years [ 1.5 ** (2**32 - 1) - 1 seconds]
|
|
|
|
defaultRetryWait = 1000
|
|
|
|
defaultTimeout = 3 * time.Second
|
|
|
|
defaultMaxRetries = math.MaxInt32
|
|
|
|
defaultReconnectWaitIncreRate = 1.5
|
|
|
|
|
|
|
|
addressKey = "fluentd-address"
|
|
|
|
bufferLimitKey = "fluentd-buffer-limit"
|
|
|
|
retryWaitKey = "fluentd-retry-wait"
|
|
|
|
maxRetriesKey = "fluentd-max-retries"
|
|
|
|
asyncConnectKey = "fluentd-async-connect"
|
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
|
|
|
|
// the context. Supported context configuration variables are
|
|
|
|
// fluentd-address & fluentd-tag.
|
2015-06-20 00:07:50 -04:00
|
|
|
func New(ctx logger.Context) (logger.Logger, error) {
|
2016-01-24 08:51:03 -05:00
|
|
|
host, port, err := parseAddress(ctx.Config[addressKey])
|
2015-09-20 07:03:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tag, err := loggerutils.ParseLogTag(ctx, "docker.{{.ID}}")
|
2015-06-20 00:07:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-24 08:51:03 -05:00
|
|
|
|
2015-10-04 17:05:43 -04:00
|
|
|
extra := ctx.ExtraAttributes(nil)
|
2016-01-24 08:51:03 -05:00
|
|
|
|
|
|
|
bufferLimit := defaultBufferLimit
|
|
|
|
if ctx.Config[bufferLimitKey] != "" {
|
|
|
|
bl64, err := units.RAMInBytes(ctx.Config[bufferLimitKey])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bufferLimit = int(bl64)
|
|
|
|
}
|
|
|
|
|
|
|
|
retryWait := defaultRetryWait
|
|
|
|
if ctx.Config[retryWaitKey] != "" {
|
|
|
|
rwd, err := time.ParseDuration(ctx.Config[retryWaitKey])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
retryWait = int(rwd.Seconds() * 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxRetries := defaultMaxRetries
|
|
|
|
if ctx.Config[maxRetriesKey] != "" {
|
|
|
|
mr64, err := strconv.ParseUint(ctx.Config[maxRetriesKey], 10, strconv.IntSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
maxRetries = int(mr64)
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncConnect := false
|
|
|
|
if ctx.Config[asyncConnectKey] != "" {
|
|
|
|
if asyncConnect, err = strconv.ParseBool(ctx.Config[asyncConnectKey]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fluentConfig := fluent.Config{
|
|
|
|
FluentPort: port,
|
|
|
|
FluentHost: host,
|
|
|
|
BufferLimit: bufferLimit,
|
|
|
|
RetryWait: retryWait,
|
|
|
|
MaxRetry: maxRetries,
|
|
|
|
AsyncConnect: asyncConnect,
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.WithField("container", ctx.ContainerID).WithField("config", fluentConfig).
|
|
|
|
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,
|
|
|
|
containerID: ctx.ContainerID,
|
|
|
|
containerName: ctx.ContainerName,
|
|
|
|
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
|
|
|
|
}
|
2015-06-20 00:07:50 -04:00
|
|
|
// fluent-logger-golang buffers logs from failures and disconnections,
|
|
|
|
// and these are transferred again automatically.
|
|
|
|
return f.writer.PostWithTime(f.tag, msg.Timestamp, data)
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:03:09 -04:00
|
|
|
func (f *fluentd) Close() error {
|
|
|
|
return f.writer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fluentd) Name() string {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
// ValidateLogOpt looks for fluentd specific log options fluentd-address & fluentd-tag.
|
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":
|
2015-06-30 20:40:13 -04:00
|
|
|
case "fluentd-tag":
|
2015-10-04 17:05:43 -04:00
|
|
|
case "labels":
|
2016-01-24 08:51:03 -05:00
|
|
|
case "tag":
|
|
|
|
case addressKey:
|
|
|
|
case bufferLimitKey:
|
|
|
|
case retryWaitKey:
|
|
|
|
case maxRetriesKey:
|
|
|
|
case asyncConnectKey:
|
|
|
|
// 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
|
|
|
|
|
|
|
if _, _, err := parseAddress(cfg["fluentd-address"]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:40:13 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:03:09 -04:00
|
|
|
func parseAddress(address string) (string, int, error) {
|
|
|
|
if address == "" {
|
2016-01-24 08:51:03 -05:00
|
|
|
return defaultHost, defaultPort, nil
|
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") {
|
|
|
|
return "", 0, fmt.Errorf("invalid fluentd-address %s: %s", address, err)
|
|
|
|
}
|
|
|
|
return host, defaultPort, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
portnum, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, fmt.Errorf("invalid fluentd-address %s: %s", address, err)
|
|
|
|
}
|
|
|
|
return host, portnum, nil
|
2015-06-20 00:07:50 -04:00
|
|
|
}
|