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 (
|
|
|
|
"bytes"
|
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/Sirupsen/logrus"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const name = "gelf"
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
type gelfLogger struct {
|
2015-10-04 17:03:11 -04:00
|
|
|
writer *gelf.Writer
|
|
|
|
ctx logger.Context
|
|
|
|
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.
|
2015-05-29 17:00:46 -04:00
|
|
|
func New(ctx logger.Context) (logger.Logger, error) {
|
|
|
|
// parse gelf address
|
|
|
|
address, err := parseAddress(ctx.Config["gelf-address"])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect extra data for GELF message
|
|
|
|
hostname, err := ctx.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gelf: cannot access hostname to set source field")
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove trailing slash from container name
|
|
|
|
containerName := bytes.TrimLeft([]byte(ctx.ContainerName), "/")
|
|
|
|
|
2015-08-06 18:50:44 -04:00
|
|
|
// parse log tag
|
|
|
|
tag, err := loggerutils.ParseLogTag(ctx, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-04 17:03:11 -04:00
|
|
|
extra := map[string]interface{}{
|
|
|
|
"_container_id": ctx.ContainerID,
|
|
|
|
"_container_name": string(containerName),
|
|
|
|
"_image_id": ctx.ContainerImageID,
|
|
|
|
"_image_name": ctx.ContainerImageName,
|
|
|
|
"_command": ctx.Command(),
|
|
|
|
"_tag": tag,
|
|
|
|
"_created": ctx.ContainerCreated,
|
|
|
|
}
|
|
|
|
|
|
|
|
extraAttrs := ctx.ExtraAttributes(func(key string) string {
|
|
|
|
if key[0] == '_' {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
return "_" + key
|
|
|
|
})
|
|
|
|
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
|
|
|
|
gelfWriter, err := gelf.NewWriter(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gelf: cannot connect to GELF endpoint: %s %v", address, err)
|
|
|
|
}
|
|
|
|
|
2016-01-21 07:44:25 -05:00
|
|
|
if v, ok := ctx.Config["gelf-compression-type"]; ok {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := ctx.Config["gelf-compression-level"]; ok {
|
|
|
|
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,
|
|
|
|
ctx: ctx,
|
|
|
|
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,
|
|
|
|
Level: level,
|
2016-01-21 07:44:25 -05:00
|
|
|
RawExtra: s.rawExtra,
|
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-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
|
|
|
|
|
|
|
if _, err := parseAddress(cfg["gelf-address"]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:40:13 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|