2015-07-21 18:26:52 -04:00
|
|
|
// Package gelf provides the log driver for forwarding server logs to
|
|
|
|
// endpoints that support the Graylog Extended Log Format.
|
2018-02-05 16:05:59 -05:00
|
|
|
package gelf // import "github.com/docker/docker/daemon/logger/gelf"
|
2015-05-29 17:00:46 -04:00
|
|
|
|
|
|
|
import (
|
2016-01-21 07:44:25 -05:00
|
|
|
"compress/flate"
|
|
|
|
"encoding/json"
|
2015-05-29 17:00:46 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2016-01-21 07:44:25 -05:00
|
|
|
"strconv"
|
2015-05-29 17:00:46 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Graylog2/go-gelf/gelf"
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
2015-08-06 18:50:44 -04:00
|
|
|
"github.com/docker/docker/daemon/logger/loggerutils"
|
2015-05-29 17:00:46 -04:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-29 17:00:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const name = "gelf"
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
type gelfLogger struct {
|
2017-09-05 13:42:18 -04:00
|
|
|
writer gelf.Writer
|
2016-11-26 00:08:34 -05:00
|
|
|
info logger.Info
|
2015-10-04 17:03:11 -04:00
|
|
|
hostname string
|
2016-01-21 07:44:25 -05:00
|
|
|
rawExtra json.RawMessage
|
2015-05-29 17:00:46 -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-05-29 17:00:46 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
// New creates a gelf logger using the configuration passed in on the
|
2016-05-09 22:04:09 -04:00
|
|
|
// context. The supported context configuration variable is gelf-address.
|
2016-11-26 00:08:34 -05:00
|
|
|
func New(info logger.Info) (logger.Logger, error) {
|
2015-05-29 17:00:46 -04:00
|
|
|
// parse gelf address
|
2016-11-26 00:08:34 -05:00
|
|
|
address, err := parseAddress(info.Config["gelf-address"])
|
2015-05-29 17:00:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect extra data for GELF message
|
2016-11-26 00:08:34 -05:00
|
|
|
hostname, err := info.Hostname()
|
2015-05-29 17:00:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gelf: cannot access hostname to set source field")
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:50:44 -04:00
|
|
|
// parse log tag
|
2016-11-26 00:08:34 -05:00
|
|
|
tag, err := loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate)
|
2015-08-06 18:50:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-04 17:03:11 -04:00
|
|
|
extra := map[string]interface{}{
|
2016-11-26 00:08:34 -05:00
|
|
|
"_container_id": info.ContainerID,
|
|
|
|
"_container_name": info.Name(),
|
|
|
|
"_image_id": info.ContainerImageID,
|
|
|
|
"_image_name": info.ContainerImageName,
|
|
|
|
"_command": info.Command(),
|
2015-10-04 17:03:11 -04:00
|
|
|
"_tag": tag,
|
2016-11-26 00:08:34 -05:00
|
|
|
"_created": info.ContainerCreated,
|
2015-10-04 17:03:11 -04:00
|
|
|
}
|
|
|
|
|
2016-11-08 19:34:47 -05:00
|
|
|
extraAttrs, err := info.ExtraAttributes(func(key string) string {
|
2015-10-04 17:03:11 -04:00
|
|
|
if key[0] == '_' {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
return "_" + key
|
|
|
|
})
|
2016-11-08 19:34:47 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-04 17:03:11 -04:00
|
|
|
for k, v := range extraAttrs {
|
|
|
|
extra[k] = v
|
2015-05-29 17:00:46 -04:00
|
|
|
}
|
|
|
|
|
2016-01-21 07:44:25 -05:00
|
|
|
rawExtra, err := json.Marshal(extra)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-06 17:45:26 -04:00
|
|
|
var gelfWriter gelf.Writer
|
|
|
|
if address.Scheme == "udp" {
|
|
|
|
gelfWriter, err = newGELFUDPWriter(address.Host, info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if address.Scheme == "tcp" {
|
|
|
|
gelfWriter, err = newGELFTCPWriter(address.Host, info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gelfLogger{
|
|
|
|
writer: gelfWriter,
|
|
|
|
info: info,
|
|
|
|
hostname: hostname,
|
|
|
|
rawExtra: rawExtra,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new TCP gelfWriter
|
|
|
|
func newGELFTCPWriter(address string, info logger.Info) (gelf.Writer, error) {
|
|
|
|
gelfWriter, err := gelf.NewTCPWriter(address)
|
2015-05-29 17:00:46 -04:00
|
|
|
if err != nil {
|
2017-09-06 17:45:26 -04:00
|
|
|
return nil, fmt.Errorf("gelf: cannot connect to GELF endpoint: %s %v", address, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := info.Config["gelf-tcp-max-reconnect"]; ok {
|
|
|
|
i, err := strconv.Atoi(v)
|
|
|
|
if err != nil || i < 0 {
|
|
|
|
return nil, fmt.Errorf("gelf-tcp-max-reconnect must be a positive integer")
|
|
|
|
}
|
|
|
|
gelfWriter.MaxReconnect = i
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := info.Config["gelf-tcp-reconnect-delay"]; ok {
|
|
|
|
i, err := strconv.Atoi(v)
|
|
|
|
if err != nil || i < 0 {
|
|
|
|
return nil, fmt.Errorf("gelf-tcp-reconnect-delay must be a positive integer")
|
|
|
|
}
|
|
|
|
gelfWriter.ReconnectDelay = time.Duration(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return gelfWriter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new UDP gelfWriter
|
|
|
|
func newGELFUDPWriter(address string, info logger.Info) (gelf.Writer, error) {
|
|
|
|
gelfWriter, err := gelf.NewUDPWriter(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gelf: cannot connect to GELF endpoint: %s %v", address, err)
|
2015-05-29 17:00:46 -04:00
|
|
|
}
|
|
|
|
|
2016-11-26 00:08:34 -05:00
|
|
|
if v, ok := info.Config["gelf-compression-type"]; ok {
|
2016-01-21 07:44:25 -05:00
|
|
|
switch v {
|
|
|
|
case "gzip":
|
|
|
|
gelfWriter.CompressionType = gelf.CompressGzip
|
|
|
|
case "zlib":
|
|
|
|
gelfWriter.CompressionType = gelf.CompressZlib
|
|
|
|
case "none":
|
|
|
|
gelfWriter.CompressionType = gelf.CompressNone
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("gelf: invalid compression type %q", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:08:34 -05:00
|
|
|
if v, ok := info.Config["gelf-compression-level"]; ok {
|
2016-01-21 07:44:25 -05:00
|
|
|
val, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gelf: invalid compression level %s, err %v", v, err)
|
|
|
|
}
|
|
|
|
gelfWriter.CompressionLevel = val
|
|
|
|
}
|
|
|
|
|
2017-09-06 17:45:26 -04:00
|
|
|
return gelfWriter, nil
|
2015-05-29 17:00:46 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
func (s *gelfLogger) Log(msg *logger.Message) error {
|
2015-05-29 17:00:46 -04:00
|
|
|
level := gelf.LOG_INFO
|
|
|
|
if msg.Source == "stderr" {
|
|
|
|
level = gelf.LOG_ERR
|
|
|
|
}
|
|
|
|
|
|
|
|
m := gelf.Message{
|
|
|
|
Version: "1.1",
|
2015-10-04 17:03:11 -04:00
|
|
|
Host: s.hostname,
|
2015-12-11 04:25:44 -05:00
|
|
|
Short: string(msg.Line),
|
2015-05-29 17:00:46 -04:00
|
|
|
TimeUnix: float64(msg.Timestamp.UnixNano()/int64(time.Millisecond)) / 1000.0,
|
2017-09-05 13:42:18 -04:00
|
|
|
Level: int32(level),
|
2016-01-21 07:44:25 -05:00
|
|
|
RawExtra: s.rawExtra,
|
2015-05-29 17:00:46 -04:00
|
|
|
}
|
2016-12-12 09:54:20 -05:00
|
|
|
logger.PutMessage(msg)
|
2015-05-29 17:00:46 -04:00
|
|
|
|
|
|
|
if err := s.writer.WriteMessage(&m); err != nil {
|
|
|
|
return fmt.Errorf("gelf: cannot send GELF message: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
func (s *gelfLogger) Close() error {
|
2015-05-29 17:00:46 -04:00
|
|
|
return s.writer.Close()
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
func (s *gelfLogger) Name() string {
|
2015-05-29 17:00:46 -04:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:04:09 -04:00
|
|
|
// ValidateLogOpt looks for gelf specific log option gelf-address.
|
2015-06-30 20:40:13 -04:00
|
|
|
func ValidateLogOpt(cfg map[string]string) error {
|
2017-09-05 17:17:59 -04:00
|
|
|
address, err := parseAddress(cfg["gelf-address"])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-21 07:44:25 -05:00
|
|
|
for key, val := range cfg {
|
2015-06-30 20:40:13 -04:00
|
|
|
switch key {
|
|
|
|
case "gelf-address":
|
2015-08-06 18:50:44 -04:00
|
|
|
case "tag":
|
2015-10-04 17:03:11 -04:00
|
|
|
case "labels":
|
|
|
|
case "env":
|
2016-11-08 19:34:47 -05:00
|
|
|
case "env-regex":
|
2016-01-21 07:44:25 -05:00
|
|
|
case "gelf-compression-level":
|
2017-09-05 17:17:59 -04:00
|
|
|
if address.Scheme != "udp" {
|
|
|
|
return fmt.Errorf("compression is only supported on UDP")
|
|
|
|
}
|
2016-01-21 07:44:25 -05:00
|
|
|
i, err := strconv.Atoi(val)
|
|
|
|
if err != nil || i < flate.DefaultCompression || i > flate.BestCompression {
|
|
|
|
return fmt.Errorf("unknown value %q for log opt %q for gelf log driver", val, key)
|
|
|
|
}
|
|
|
|
case "gelf-compression-type":
|
2017-09-05 17:17:59 -04:00
|
|
|
if address.Scheme != "udp" {
|
|
|
|
return fmt.Errorf("compression is only supported on UDP")
|
|
|
|
}
|
2016-01-21 07:44:25 -05:00
|
|
|
switch val {
|
|
|
|
case "gzip", "zlib", "none":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown value %q for log opt %q for gelf log driver", val, key)
|
|
|
|
}
|
2017-09-05 17:17:59 -04:00
|
|
|
case "gelf-tcp-max-reconnect", "gelf-tcp-reconnect-delay":
|
|
|
|
if address.Scheme != "tcp" {
|
|
|
|
return fmt.Errorf("%q is only valid for TCP", key)
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(val)
|
|
|
|
if err != nil || i < 0 {
|
|
|
|
return fmt.Errorf("%q must be a positive integer", key)
|
|
|
|
}
|
2015-06-30 20:40:13 -04:00
|
|
|
default:
|
2016-01-21 07:44:25 -05:00
|
|
|
return fmt.Errorf("unknown log opt %q for gelf log driver", key)
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
|
|
|
}
|
2015-09-20 07:03:09 -04:00
|
|
|
|
2017-09-05 17:17:59 -04:00
|
|
|
return nil
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
|
|
|
|
2017-09-05 17:17:59 -04:00
|
|
|
func parseAddress(address string) (*url.URL, error) {
|
2015-09-20 07:03:09 -04:00
|
|
|
if address == "" {
|
2017-09-05 17:17:59 -04:00
|
|
|
return nil, fmt.Errorf("gelf-address is a required parameter")
|
2015-09-20 07:03:09 -04:00
|
|
|
}
|
|
|
|
if !urlutil.IsTransportURL(address) {
|
2017-09-05 17:17:59 -04:00
|
|
|
return nil, fmt.Errorf("gelf-address should be in form proto://address, got %v", address)
|
2015-09-20 07:03:09 -04:00
|
|
|
}
|
|
|
|
url, err := url.Parse(address)
|
|
|
|
if err != nil {
|
2017-09-05 17:17:59 -04:00
|
|
|
return nil, err
|
2015-09-20 07:03:09 -04:00
|
|
|
}
|
2015-05-29 17:00:46 -04:00
|
|
|
|
2015-09-20 07:03:09 -04:00
|
|
|
// we support only udp
|
2017-09-05 17:17:59 -04:00
|
|
|
if url.Scheme != "udp" && url.Scheme != "tcp" {
|
|
|
|
return nil, fmt.Errorf("gelf: endpoint needs to be TCP or UDP")
|
2015-09-20 07:03:09 -04:00
|
|
|
}
|
2015-05-29 17:00:46 -04:00
|
|
|
|
2015-09-20 07:03:09 -04:00
|
|
|
// get host and port
|
|
|
|
if _, _, err = net.SplitHostPort(url.Host); err != nil {
|
2017-09-05 17:17:59 -04:00
|
|
|
return nil, fmt.Errorf("gelf: please provide gelf-address as proto://host:port")
|
2015-05-29 17:00:46 -04:00
|
|
|
}
|
|
|
|
|
2017-09-05 17:17:59 -04:00
|
|
|
return url, nil
|
2015-05-29 17:00:46 -04:00
|
|
|
}
|