2015-05-29 17:00:46 -04:00
|
|
|
// +build linux
|
|
|
|
|
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.
|
2015-05-29 17:00:46 -04:00
|
|
|
package gelf
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:00:46 -04:00
|
|
|
// create new gelfWriter
|
2017-09-05 13:42:18 -04:00
|
|
|
gelfWriter, err := gelf.NewUDPWriter(address)
|
2015-05-29 17:00:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gelf: cannot connect to GELF endpoint: %s %v", address, err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
return &gelfLogger{
|
2015-10-04 17:03:11 -04:00
|
|
|
writer: gelfWriter,
|
2016-11-26 00:08:34 -05:00
|
|
|
info: info,
|
2015-10-04 17:03:11 -04:00
|
|
|
hostname: hostname,
|
2016-01-21 07:44:25 -05:00
|
|
|
rawExtra: rawExtra,
|
2015-05-29 17:00:46 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
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":
|
|
|
|
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":
|
|
|
|
switch val {
|
|
|
|
case "gzip", "zlib", "none":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown value %q for log opt %q for gelf log driver", val, 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
|
|
|
|
2016-12-14 15:36:58 -05:00
|
|
|
_, err := parseAddress(cfg["gelf-address"])
|
|
|
|
return err
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 17:00:46 -04:00
|
|
|
func parseAddress(address string) (string, error) {
|
2015-09-20 07:03:09 -04:00
|
|
|
if address == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
if !urlutil.IsTransportURL(address) {
|
|
|
|
return "", fmt.Errorf("gelf-address should be in form proto://address, got %v", address)
|
|
|
|
}
|
|
|
|
url, err := url.Parse(address)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-05-29 17:00:46 -04:00
|
|
|
|
2015-09-20 07:03:09 -04:00
|
|
|
// we support only udp
|
|
|
|
if url.Scheme != "udp" {
|
|
|
|
return "", fmt.Errorf("gelf: endpoint needs to be UDP")
|
|
|
|
}
|
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 {
|
|
|
|
return "", fmt.Errorf("gelf: please provide gelf-address as udp://host:port")
|
2015-05-29 17:00:46 -04:00
|
|
|
}
|
|
|
|
|
2015-09-20 07:03:09 -04:00
|
|
|
return url.Host, nil
|
2015-05-29 17:00:46 -04:00
|
|
|
}
|