-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
136 lines (104 loc) · 2.6 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"github.com/gorilla/mux"
)
var indexer *Indexer
func main() {
var c Configuration
err := envconfig.Process("ESPROXY", &c)
if err != nil {
log.Fatal(err.Error())
}
FlushInterval = time.Duration(c.FlushInterval) * time.Second
logrus.SetFormatter(&logrus.JSONFormatter{})
if c.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
srv := &http.Server{
Addr: c.ListenAddress,
WriteTimeout: 5 * time.Minute,
ReadTimeout: 5 * time.Minute,
}
tu, err := url.Parse(c.ESAddress)
if err != nil {
logrus.Fatal(err)
}
logrus.Debugf("es address: %s", c.ESAddress)
proxy := httputil.NewSingleHostReverseProxy(tu)
indexer, err = NewIndexer(c.ESAddress)
if err != nil {
logrus.Fatal(err)
}
prometheus.DefaultRegisterer.MustRegister(newIndexerMetricsCollector(indexer))
if c.Debug {
indexer.Debug()
}
r := mux.NewRouter()
r.Methods("POST").Path("/_bulk").HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json; charset=UTF-8")
_, err := writer.Write([]byte("{\"errors\": false, \"items\": []}"))
if err != nil {
logrus.Warning(err)
}
err = processBulk(request.Body)
if err != nil {
logrus.Error(err)
}
indexerRequestsCounter.With(prometheus.Labels{"method": request.Method}).Inc()
logrus.WithFields(logrus.Fields{
"method": request.Method,
"url": request.RequestURI,
}).Debug()
})
r.PathPrefix("/").HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
proxy.ServeHTTP(writer, request)
proxyRequestsCounter.With(prometheus.Labels{"method": request.Method}).Inc()
logrus.WithFields(logrus.Fields{
"method": request.Method,
"url": request.RequestURI,
}).Debug()
})
srv.Handler = r
go serveMetrics()
logrus.Infof("listening %s", c.ListenAddress)
err = srv.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
func processBulk(r io.Reader) error {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
md := scanner.Bytes()
var meta bulkMetadata
err := json.Unmarshal(md, &meta)
if err != nil {
return err
}
var doc []byte
if meta.Action() != "delete" {
// Read record
if valid := scanner.Scan(); !valid {
return errors.New("corrupted data")
}
doc = scanner.Bytes()
}
err = indexer.Add(&meta, doc)
if err != nil {
logrus.WithField("meta", meta).Error(err)
}
}
return nil
}