-
Notifications
You must be signed in to change notification settings - Fork 1
/
elastic.go
215 lines (183 loc) · 5.13 KB
/
elastic.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/docker/docker/daemon/logger"
"github.com/sirupsen/logrus"
)
type elasticBulkWriter struct {
esHost string
esIndex string
esType string
esDateSuffix string
bulkSize int
buffer []string
logInfo logger.Info
mu *sync.Mutex
ticker *time.Ticker
tickerDone chan bool
}
func newElasticBulkWriter(logInfo logger.Info) (*elasticBulkWriter, error) {
var esHost string
if h, ok := logInfo.Config["host"]; !ok {
esHost = os.Getenv("HOST")
if esHost == "" {
logrus.WithField("id", logInfo.ContainerID).Error("HOST is not defined so --log-opt host is mandatory")
return nil, fmt.Errorf("HOST is not defined so --log-opt host is mandatory")
}
} else {
esHost = h
}
u, err := url.Parse(esHost)
if err != nil {
logrus.WithField("id", logInfo.ContainerID).Error(err.Error())
return nil, fmt.Errorf("%w. Missing scheme?", err)
}
if u.Scheme == "" {
logrus.WithField("id", logInfo.ContainerID).Error("Invalid host. Missing scheme")
return nil, fmt.Errorf("Invalid host. Missing scheme")
}
esHost = fmt.Sprintf("%s://%s", u.Scheme, u.Host)
esGCTimer := os.Getenv("GCTIMER")
if esGCTimer == "" {
esGCTimer = "1m"
}
if _, ok := logInfo.Config["index"]; !ok {
logrus.WithField("id", logInfo.ContainerID).Error("--log-opt index is mandatory")
return nil, fmt.Errorf("--log-opt index is mandatory")
}
esType := "log"
if t, ok := logInfo.Config["type"]; ok {
esType = t
}
bulkSize := 10
if bs, ok := logInfo.Config["bulksize"]; ok {
bulkSize, _ = strconv.Atoi(bs)
} else if bs := os.Getenv("bulksize"); bs != "" {
bulkSize, _ = strconv.Atoi(bs)
}
tickDuration, err := time.ParseDuration(esGCTimer)
if err != nil {
return nil, err
}
es := &elasticBulkWriter{
esHost: esHost,
esIndex: logInfo.Config["index"],
esType: esType,
bulkSize: bulkSize,
esDateSuffix: os.Getenv("DATESUFFIX"),
logInfo: logInfo,
mu: &sync.Mutex{},
ticker: time.NewTicker(tickDuration),
tickerDone: make(chan bool),
}
go es.gc()
return es, nil
}
func (es *elasticBulkWriter) write(line string) {
es.mu.Lock()
if len(es.buffer) < es.bulkSize {
es.buffer = append(es.buffer, line)
es.mu.Unlock()
return
}
fullBuffer := es.buffer
es.buffer = nil
es.buffer = append(es.buffer, line)
es.mu.Unlock()
logrus.WithField("id", es.logInfo.ContainerID).
WithField("container", es.logInfo.ContainerName).
WithField("elasticHost", es.esHost).
WithField("bulksize", es.bulkSize).
Info("Sending bulk to elastic")
go es.send(fullBuffer)
}
func (es *elasticBulkWriter) send(buffer []string) {
payload := strings.Builder{}
indexDate := time.Now().Format(es.esDateSuffix)
indexName := fmt.Sprintf("%s_%s", es.esIndex, indexDate)
for _, l := range buffer {
//payload.WriteString(fmt.Sprintf(`{"index": {"_index":"%s", "_type":"%s"} }`, es.esIndex, es.esType))
payload.WriteString(fmt.Sprintf(`{"index": {"_index":"%s", "_type":"%s"} }`, indexName, es.esType))
payload.WriteString("\n")
payload.WriteString(l)
payload.WriteString("\n")
}
body := payload.String()
url := fmt.Sprintf("%s/_bulk", es.esHost)
req, err := http.NewRequest("POST", url, strings.NewReader(body))
if err != nil {
logrus.WithField("id", es.logInfo.ContainerID).
WithField("container", es.logInfo.ContainerName).
WithField("elastichost", es.esHost).
Error(err.Error())
return
}
req.Header.Set("Content-Type", "application/x-ndjson")
if u := es.config("USER"); u != "" {
if p := es.config("PASSWORD"); p != "" {
req.SetBasicAuth(u, p)
}
}
r, err := http.DefaultClient.Do(req)
//r, err := http.Post(url, "application/x-ndjson", strings.NewReader(body))
if err != nil {
logrus.WithField("id", es.logInfo.ContainerID).
WithField("container", es.logInfo.ContainerName).
WithField("elasticHost", es.esHost).
Error(err.Error())
return
}
defer r.Body.Close()
if r.StatusCode != 200 {
rBody, _ := ioutil.ReadAll(r.Body)
logrus.WithField("id", es.logInfo.ContainerID).
WithField("container", es.logInfo.ContainerName).
WithField("elastichost", es.esHost).
WithField("StatusCode", r.StatusCode).
Error(string(rBody))
}
}
func (es *elasticBulkWriter) gc() {
logrus.WithField("id", es.logInfo.ContainerID).
Debug("GC: goroutine possible leak")
for {
select {
case _ = <-es.ticker.C:
es.mu.Lock()
if len(es.buffer) > 0 {
fullBuffer := es.buffer
es.buffer = nil
logrus.WithField("id", es.logInfo.ContainerID).
WithField("container", es.logInfo.ContainerName).
WithField("elasticHost", es.esHost).
WithField("bulksize", len(fullBuffer)).
Info("GC: Sending bulk to elastic")
go es.send(fullBuffer)
}
es.mu.Unlock()
case _ = <-es.tickerDone:
logrus.WithField("id", es.logInfo.ContainerID).
Debug("GC: goroutine not a leak")
return
}
}
}
func (es *elasticBulkWriter) Stop() {
es.ticker.Stop()
es.tickerDone <- true
}
func (es *elasticBulkWriter) config(key string) string {
u := os.Getenv(key)
if v, ok := es.logInfo.Config[key]; ok {
u = v
}
return u
}