forked from docker-archive/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gelf.go
159 lines (134 loc) · 3.44 KB
/
gelf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// +build linux
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"
"github.com/docker/docker/pkg/urlutil"
)
const name = "gelf"
type GelfLogger struct {
writer *gelf.Writer
ctx logger.Context
fields GelfFields
}
type GelfFields struct {
hostname string
containerId string
containerName string
imageId string
imageName string
command string
tag string
created time.Time
}
func init() {
if err := logger.RegisterLogDriver(name, New); err != nil {
logrus.Fatal(err)
}
if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
logrus.Fatal(err)
}
}
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), "/")
fields := GelfFields{
hostname: hostname,
containerId: ctx.ContainerID,
containerName: string(containerName),
imageId: ctx.ContainerImageID,
imageName: ctx.ContainerImageName,
command: ctx.Command(),
tag: ctx.Config["gelf-tag"],
created: ctx.ContainerCreated,
}
// 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)
}
return &GelfLogger{
writer: gelfWriter,
ctx: ctx,
fields: fields,
}, nil
}
func (s *GelfLogger) Log(msg *logger.Message) error {
// remove trailing and leading whitespace
short := bytes.TrimSpace([]byte(msg.Line))
level := gelf.LOG_INFO
if msg.Source == "stderr" {
level = gelf.LOG_ERR
}
m := gelf.Message{
Version: "1.1",
Host: s.fields.hostname,
Short: string(short),
TimeUnix: float64(msg.Timestamp.UnixNano()/int64(time.Millisecond)) / 1000.0,
Level: level,
Extra: map[string]interface{}{
"_container_id": s.fields.containerId,
"_container_name": s.fields.containerName,
"_image_id": s.fields.imageId,
"_image_name": s.fields.imageName,
"_command": s.fields.command,
"_tag": s.fields.tag,
"_created": s.fields.created,
},
}
if err := s.writer.WriteMessage(&m); err != nil {
return fmt.Errorf("gelf: cannot send GELF message: %v", err)
}
return nil
}
func (s *GelfLogger) Close() error {
return s.writer.Close()
}
func (s *GelfLogger) Name() string {
return name
}
func ValidateLogOpt(cfg map[string]string) error {
for key := range cfg {
switch key {
case "gelf-address":
case "gelf-tag":
default:
return fmt.Errorf("unknown log opt '%s' for gelf log driver", key)
}
}
return nil
}
func parseAddress(address string) (string, error) {
if urlutil.IsTransportURL(address) {
url, err := url.Parse(address)
if err != nil {
return "", err
}
// we support only udp
if url.Scheme != "udp" {
return "", fmt.Errorf("gelf: endpoint needs to be UDP")
}
// get host and port
if _, _, err = net.SplitHostPort(url.Host); err != nil {
return "", fmt.Errorf("gelf: please provide gelf-address as udp://host:port")
}
return url.Host, nil
}
return "", nil
}