2016-05-12 10:18:08 -04:00
|
|
|
// Package logentries provides the log driver for forwarding server logs
|
|
|
|
// to logentries endpoints.
|
2018-02-05 16:05:59 -05:00
|
|
|
package logentries // import "github.com/docker/docker/daemon/logger/logentries"
|
2016-05-12 10:18:08 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-03-10 04:34:13 -05:00
|
|
|
"strconv"
|
2016-05-12 10:18:08 -04:00
|
|
|
|
|
|
|
"github.com/bsphere/le_go"
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
2017-03-10 04:34:13 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-05-12 10:18:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type logentries struct {
|
|
|
|
tag string
|
|
|
|
containerID string
|
|
|
|
containerName string
|
|
|
|
writer *le_go.Logger
|
|
|
|
extra map[string]string
|
2017-03-10 04:34:13 -05:00
|
|
|
lineOnly bool
|
2016-05-12 10:18:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2017-03-10 04:34:13 -05:00
|
|
|
name = "logentries"
|
|
|
|
token = "logentries-token"
|
|
|
|
lineonly = "line-only"
|
2016-05-12 10:18:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if err := logger.RegisterLogDriver(name, New); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a logentries logger using the configuration passed in on
|
|
|
|
// the context. The supported context configuration variable is
|
|
|
|
// logentries-token.
|
2016-11-26 00:08:34 -05:00
|
|
|
func New(info logger.Info) (logger.Logger, error) {
|
|
|
|
logrus.WithField("container", info.ContainerID).
|
|
|
|
WithField("token", info.Config[token]).
|
2017-03-10 04:34:13 -05:00
|
|
|
WithField("line-only", info.Config[lineonly]).
|
2016-05-12 10:18:08 -04:00
|
|
|
Debug("logging driver logentries configured")
|
|
|
|
|
2016-11-26 00:08:34 -05:00
|
|
|
log, err := le_go.Connect(info.Config[token])
|
2016-05-12 10:18:08 -04:00
|
|
|
if err != nil {
|
2017-03-10 04:34:13 -05:00
|
|
|
return nil, errors.Wrap(err, "error connecting to logentries")
|
|
|
|
}
|
|
|
|
var lineOnly bool
|
2017-11-30 08:03:16 -05:00
|
|
|
if info.Config[lineonly] != "" {
|
|
|
|
if lineOnly, err = strconv.ParseBool(info.Config[lineonly]); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing lineonly option")
|
|
|
|
}
|
2016-05-12 10:18:08 -04:00
|
|
|
}
|
|
|
|
return &logentries{
|
2016-11-26 00:08:34 -05:00
|
|
|
containerID: info.ContainerID,
|
|
|
|
containerName: info.ContainerName,
|
2016-05-12 10:18:08 -04:00
|
|
|
writer: log,
|
2017-03-10 04:34:13 -05:00
|
|
|
lineOnly: lineOnly,
|
2016-05-12 10:18:08 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *logentries) Log(msg *logger.Message) error {
|
2017-03-10 04:34:13 -05:00
|
|
|
if !f.lineOnly {
|
|
|
|
data := map[string]string{
|
|
|
|
"container_id": f.containerID,
|
|
|
|
"container_name": f.containerName,
|
|
|
|
"source": msg.Source,
|
|
|
|
"log": string(msg.Line),
|
|
|
|
}
|
|
|
|
for k, v := range f.extra {
|
|
|
|
data[k] = v
|
|
|
|
}
|
|
|
|
ts := msg.Timestamp
|
|
|
|
logger.PutMessage(msg)
|
|
|
|
f.writer.Println(f.tag, ts, data)
|
|
|
|
} else {
|
2017-11-27 09:26:53 -05:00
|
|
|
line := string(msg.Line)
|
2017-03-10 04:34:13 -05:00
|
|
|
logger.PutMessage(msg)
|
|
|
|
f.writer.Println(line)
|
2016-05-12 10:18:08 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *logentries) Close() error {
|
|
|
|
return f.writer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *logentries) Name() string {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateLogOpt looks for logentries specific log option logentries-address.
|
|
|
|
func ValidateLogOpt(cfg map[string]string) error {
|
|
|
|
for key := range cfg {
|
|
|
|
switch key {
|
|
|
|
case "env":
|
2016-11-08 19:34:47 -05:00
|
|
|
case "env-regex":
|
2016-05-12 10:18:08 -04:00
|
|
|
case "labels":
|
2019-01-16 16:52:22 -05:00
|
|
|
case "labels-regex":
|
2016-05-12 10:18:08 -04:00
|
|
|
case "tag":
|
|
|
|
case key:
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown log opt '%s' for logentries log driver", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg[token] == "" {
|
|
|
|
return fmt.Errorf("Missing logentries token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|