-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathlogstash.go
249 lines (206 loc) · 6.26 KB
/
logstash.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package logstash
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net"
"os"
"regexp"
"strings"
"time"
docker "github.com/fsouza/go-dockerclient"
"github.com/gliderlabs/logspout/router"
)
func init() {
router.AdapterFactories.Register(NewLogstashAdapter, "logstash")
}
// LogstashAdapter is an adapter that streams UDP JSON to Logstash.
type LogstashAdapter struct {
conn net.Conn
route *router.Route
containerTags map[string][]string
logstashFields map[string]map[string]string
decodeJsonLogs map[string]bool
}
// NewLogstashAdapter creates a LogstashAdapter with UDP as the default transport.
func NewLogstashAdapter(route *router.Route) (router.LogAdapter, error) {
transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp"))
if !found {
return nil, errors.New("unable to find adapter: " + route.Adapter)
}
for {
conn, err := transport.Dial(route.Address, route.Options)
if err == nil {
return &LogstashAdapter{
route: route,
conn: conn,
containerTags: make(map[string][]string),
logstashFields: make(map[string]map[string]string),
decodeJsonLogs: make(map[string]bool),
}, nil
}
if os.Getenv("RETRY_STARTUP") == "" {
return nil, err
}
log.Println("Retrying:", err)
time.Sleep(2 * time.Second)
}
}
// Get container tags configured with the environment variable LOGSTASH_TAGS
func GetContainerTags(c *docker.Container, a *LogstashAdapter) []string {
if tags, ok := a.containerTags[c.ID]; ok {
return tags
}
tags := []string{}
tagsStr := os.Getenv("LOGSTASH_TAGS")
for _, e := range c.Config.Env {
if strings.HasPrefix(e, "LOGSTASH_TAGS=") {
tagsStr = strings.TrimPrefix(e, "LOGSTASH_TAGS=")
break
}
}
if len(tagsStr) > 0 {
tags = strings.Split(tagsStr, ",")
}
a.containerTags[c.ID] = tags
return tags
}
// Get logstash fields configured with the environment variable LOGSTASH_FIELDS
func GetLogstashFields(c *docker.Container, a *LogstashAdapter) map[string]string {
if fields, ok := a.logstashFields[c.ID]; ok {
return fields
}
fieldsStr := os.Getenv("LOGSTASH_FIELDS")
fields := map[string]string{}
for _, e := range c.Config.Env {
if strings.HasPrefix(e, "LOGSTASH_FIELDS=") {
fieldsStr = strings.TrimPrefix(e, "LOGSTASH_FIELDS=")
}
}
if len(fieldsStr) > 0 {
for _, f := range strings.Split(fieldsStr, ",") {
sp := strings.Split(f, "=")
k, v := sp[0], sp[1]
fields[k] = v
}
}
a.logstashFields[c.ID] = fields
return fields
}
// Get boolean indicating whether json logs should be decoded (or added as message),
// configured with the environment variable DECODE_JSON_LOGS
func IsDecodeJsonLogs(c *docker.Container, a *LogstashAdapter) bool {
if decodeJsonLogs, ok := a.decodeJsonLogs[c.ID]; ok {
return decodeJsonLogs
}
decodeJsonLogsStr := os.Getenv("DECODE_JSON_LOGS")
for _, e := range c.Config.Env {
if strings.HasPrefix(e, "DECODE_JSON_LOGS=") {
decodeJsonLogsStr = strings.TrimPrefix(e, "DECODE_JSON_LOGS=")
}
}
decodeJsonLogs := decodeJsonLogsStr != "false"
a.decodeJsonLogs[c.ID] = decodeJsonLogs
return decodeJsonLogs
}
// Get hostname of container, searching first for /etc/host_hostname, otherwise
// using the hostname assigned to the container (typically container ID).
func GetContainerHostname(c *docker.Container) string {
content, err := ioutil.ReadFile("/etc/host_hostname")
if err == nil && len(content) > 0 {
return strings.Trim(string(content), "\r\n")
}
return c.Config.Hostname
}
// Stream implements the router.LogAdapter interface.
func (a *LogstashAdapter) Stream(logstream chan *router.Message) {
for m := range logstream {
dockerInfo := DockerInfo{
Name: m.Container.Name,
ID: m.Container.ID,
Image: m.Container.Config.Image,
Hostname: GetContainerHostname(m.Container),
}
// Check if we are sending logs for this container
if !containerIncluded(dockerInfo.Name) {
continue
}
if os.Getenv("DOCKER_LABELS") != "" {
dockerInfo.Labels = make(map[string]string)
for label, value := range m.Container.Config.Labels {
dockerInfo.Labels[strings.Replace(label, ".", "_", -1)] = value
}
}
tags := GetContainerTags(m.Container, a)
fields := GetLogstashFields(m.Container, a)
var js []byte
var data map[string]interface{}
var err error
// Try to parse JSON-encoded m.Data. If it wasn't JSON, create an empty object
// and use the original data as the message.
if IsDecodeJsonLogs(m.Container, a) {
err = json.Unmarshal([]byte(m.Data), &data)
}
if err != nil || data == nil {
data = make(map[string]interface{})
data["message"] = m.Data
}
for k, v := range fields {
data[k] = v
}
data["docker"] = dockerInfo
data["stream"] = m.Source
data["tags"] = tags
// Return the JSON encoding
if js, err = json.Marshal(data); err != nil {
// Log error message and continue parsing next line, if marshalling fails
log.Println("logstash: could not marshal JSON:", err)
continue
}
// To work with tls and tcp transports via json_lines codec
js = append(js, byte('\n'))
for {
_, err := a.conn.Write(js)
if err == nil {
break
}
if os.Getenv("RETRY_SEND") == "" {
log.Fatal("logstash: could not write:", err)
} else {
time.Sleep(2 * time.Second)
}
}
}
}
// containerIncluded Returns true if this container is in INCLUDE_CONTAINERS, or not env var is set
func containerIncluded(inputContainerName string) bool {
if includeContainers := os.Getenv("INCLUDE_CONTAINERS"); includeContainers != "" {
for _, containerName := range strings.Split(includeContainers, ",") {
if inputContainerName == containerName {
// This contain is included, send this log
return true
}
}
return false
} else if includeContainers := os.Getenv("INCLUDE_CONTAINERS_REGEX"); includeContainers != "" {
compiled, err := regexp.Compile(includeContainers)
if err != nil {
// Return true by default
return true
}
if compiled.MatchString(inputContainerName) {
// This contain is included, send this log
return true
}
return false
}
return true
}
type DockerInfo struct {
Name string `json:"name"`
ID string `json:"id"`
Image string `json:"image"`
Hostname string `json:"hostname"`
Labels map[string]string `json:"labels"`
}