-
Notifications
You must be signed in to change notification settings - Fork 26
/
log.go
139 lines (122 loc) · 3.26 KB
/
log.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
package beyond
import (
"context"
"crypto/sha1"
"encoding/json"
"flag"
"fmt"
"net/http"
"strings"
"time"
"github.com/olivere/elastic"
log "github.com/sirupsen/logrus"
)
var (
// import logrus helpers
Error = log.Error
WithError = log.WithError
WithField = log.WithField
WithFields = log.WithFields
logHTTP = flag.Bool("log-http", false, "enable HTTP logging to stdout")
logJSON = flag.Bool("log-json", false, "use json output (logrus)")
logXFF = flag.Bool("log-xff", true, "include X-Forwarded-For in logs")
logElastic = flag.String("log-elastic", "", "csv of elasticsearch servers")
logElasticD = flag.Duration("log-elastic-interval", time.Second, "how often to commit bulk updates")
logElasticP = flag.String("log-elastic-prefix", "beyond", "insert this on the front of elastic indexes")
logElasticW = flag.Int("log-elastic-workers", 3, "bulk commit workers")
logElasticCh = make(chan *elastic.BulkUpdateRequest, 10240)
)
func logSetup() error {
if *logJSON {
log.SetFormatter(&log.JSONFormatter{})
}
if *logElastic != "" {
return logElasticSetup(*logElastic)
}
return nil
}
func logRoundtrip(resp *http.Response) {
if !*logHTTP && *logElastic == "" {
return
}
d := map[string]interface{}{
"date": time.Now().Format(time.RFC3339),
"user": resp.Request.Header.Get(*headerPrefix + "-User"),
"useragent": resp.Request.UserAgent(),
"method": resp.Request.Method,
"host": resp.Request.Host,
"path": resp.Request.URL.Path,
"query": resp.Request.URL.RawQuery,
"origin": resp.Request.Header.Get("Origin"),
"code": resp.StatusCode,
"len": resp.ContentLength,
"location": resp.Header.Get("Location"),
"proto": resp.Proto,
"server": resp.Header.Get("Server"),
"type": resp.Header.Get("Content-Type"),
// "req.header": resp.Request.Header,
// "resp.header": resp.Header,
}
if *logXFF {
d["xff"] = resp.Request.Header.Get("X-Forwarded-For")
}
for k, v := range d {
if v == "" {
delete(d, k)
}
}
if *logHTTP {
WithFields(d).Info("HTTP")
}
if *logElastic != "" {
raw, _ := json.Marshal(d)
hash := sha1.New()
hash.Write(raw)
id := fmt.Sprintf("%x:%x", time.Now().Unix(), hash.Sum(nil))
elt := elastic.NewBulkUpdateRequest().Index(*logElasticP + "-" + id[:4]).Id(id).Type("http").Doc(d).DocAsUpsert(true)
logElasticPut(elt, logElasticCh)
}
}
func logElasticPut(elt *elastic.BulkUpdateRequest, sink chan *elastic.BulkUpdateRequest) {
select {
case sink <- elt:
default:
log.Println("overflow:", elt)
}
}
func logElasticSetup(elasticURLs string) error {
elasticSearch, err := elastic.NewSimpleClient(elastic.SetURL(strings.Split(elasticURLs, ",")...))
if err == nil {
for i := 0; i < *logElasticW; i++ {
go func() {
bulk := elasticSearch.Bulk()
logElasticWorker(bulk, *logElasticD)
}()
}
}
return nil
}
func logElasticWorker(bulk *elastic.BulkService, duration time.Duration) {
tick := time.NewTicker(duration)
for {
select {
case elt := <-logElasticCh:
bulk.Add(elt)
case <-tick.C:
if bulk.NumberOfActions() < 1 {
continue
}
r, err := bulk.Do(context.Background())
if err != nil {
log.Println(err)
}
if r == nil {
continue
}
fails := r.Failed()
if len(fails) > 0 {
log.Println(len(fails), fails[0].Error)
}
}
}
}