forked from looplab/logspout-logstash
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogstash.go
184 lines (151 loc) · 4.42 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
package logstash
import (
"encoding/json"
"errors"
"log"
"net"
"os"
"strings"
"time"
"github.com/fsouza/go-dockerclient"
"github.com/gliderlabs/logspout/router"
)
func init() {
router.AdapterFactories.Register(NewAdapter, "logstash")
}
// Adapter is an adapter that streams UDP JSON to Logstash.
type Adapter struct {
conn net.Conn
route *router.Route
containerTags map[string][]string
logstashFields map[string]map[string]string
}
// NewAdapter creates a LogstashAdapter with UDP as the default transport.
func NewAdapter(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 &Adapter{
route: route,
conn: conn,
containerTags: make(map[string][]string),
logstashFields: make(map[string]map[string]string),
}, nil
}
if os.Getenv("RETRY_STARTUP") == "" {
return nil, err
}
log.Println("Retrying:", err)
time.Sleep(2 * time.Second)
}
}
// GetContainerTags returns the list of configured tags (fetched from the LOGSTASH_TAGS environment variable)
func GetContainerTags(c *docker.Container, a *Adapter) []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
}
// GetLogstashFields returns the list of configured fields (fetched from the LOGSTASH_FIELDS environment variable)
func GetLogstashFields(c *docker.Container, a *Adapter) 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
}
// Stream implements the router.LogAdapter interface.
func (a *Adapter) 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: m.Container.Config.Hostname,
}
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 err = json.Unmarshal([]byte(m.Data), &data); err != nil || data == nil {
data = make(map[string]interface{})
data["message"] = m.Data
}
for k, v := range fields {
data[k] = v
}
data["docker"] = map[string]interface{}{
"container": dockerInfo,
"logtime": m.Time,
}
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.Fatalln("logstash: could not write:", err)
} else {
log.Println("logstash: could not write:", err)
time.Sleep(2 * time.Second)
}
}
}
}
// DockerInfo contains some of the exported information from the Docker daemon
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"`
}