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"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"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
|
|
|
|
extra map[string]interface{}
|
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
|
|
|
|
// context. Supported context configuration variables are
|
|
|
|
// gelf-address, & gelf-tag.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
return &gelfLogger{
|
2015-10-04 17:03:11 -04:00
|
|
|
writer: gelfWriter,
|
|
|
|
ctx: ctx,
|
|
|
|
hostname: hostname,
|
|
|
|
extra: extra,
|
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,
|
2015-10-04 17:03:11 -04:00
|
|
|
Extra: s.extra,
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:26:52 -04:00
|
|
|
// ValidateLogOpt looks for gelf specific log options gelf-address, &
|
|
|
|
// gelf-tag.
|
2015-06-30 20:40:13 -04:00
|
|
|
func ValidateLogOpt(cfg map[string]string) error {
|
|
|
|
for key := range cfg {
|
|
|
|
switch key {
|
|
|
|
case "gelf-address":
|
|
|
|
case "gelf-tag":
|
2015-08-06 18:50:44 -04:00
|
|
|
case "tag":
|
2015-10-04 17:03:11 -04:00
|
|
|
case "labels":
|
|
|
|
case "env":
|
2015-06-30 20:40:13 -04:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown log opt '%s' for gelf log driver", key)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|