-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp.go
247 lines (207 loc) · 6.39 KB
/
http.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
package input
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"os"
"strings"
"sync"
"github.com/devopsext/events/common"
"github.com/devopsext/events/processor"
sreCommon "github.com/devopsext/sre/common"
"github.com/devopsext/utils"
)
type HttpInputOptions struct {
HealthcheckURL string
K8sURL string
KubeURL string
WinEventURL string
RancherURL string
AlertmanagerURL string
GitlabURL string
DataDogURL string
Site24x7URL string
CloudflareURL string
GoogleURL string
AWSURL string
ZabbixURL string
CustomJsonURL string
VCenterURL string
ObserviumEventURL string
TeamcityURL string
ServerName string
Listen string
Tls bool
Insecure bool
Cert string
Key string
Chain string
HeaderTraceID string
}
type HttpInput struct {
options HttpInputOptions
processors *common.Processors
tracer sreCommon.Tracer
logger sreCommon.Logger
meter sreCommon.Meter
requests sreCommon.Counter
errors sreCommon.Counter
}
type HttpProcessHandleFunc = func(w http.ResponseWriter, r *http.Request)
func (h *HttpInput) startSpanFromRequest(r *http.Request) sreCommon.TracerSpan {
traceID := r.Header.Get(h.options.HeaderTraceID)
if utils.IsEmpty(traceID) {
return h.tracer.StartSpan()
}
return h.tracer.StartSpanWithTraceID(traceID, "")
}
func (h *HttpInput) processURL(url string, mux *http.ServeMux, p common.HttpProcessor) {
urls := strings.Split(url, ",")
for _, url := range urls {
mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
span := h.startSpanFromRequest(r)
span.SetCarrier(r.Header)
span.SetTag("path", path)
defer span.Finish()
h.requests.Inc(path)
err := p.HandleHttpRequest(w, r)
if err != nil {
h.errors.Inc(path)
}
})
}
}
func (h *HttpInput) Start(wg *sync.WaitGroup, outputs *common.Outputs) {
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
h.logger.Info("Start http input...")
var caPool *x509.CertPool
var certificates []tls.Certificate
if h.options.Tls {
// load certififcate
var cert []byte
if _, err := os.Stat(h.options.Cert); err == nil {
cert, err = os.ReadFile(h.options.Cert)
if err != nil {
h.logger.Panic(err)
}
} else {
cert = []byte(h.options.Cert)
}
// load key
var key []byte
if _, err := os.Stat(h.options.Key); err == nil {
key, err = os.ReadFile(h.options.Key)
if err != nil {
h.logger.Panic(err)
}
} else {
key = []byte(h.options.Key)
}
// make pair from certificate and pair
pair, err := tls.X509KeyPair(cert, key)
if err != nil {
h.logger.Panic(err)
}
certificates = append(certificates, pair)
// load CA chain
var chain []byte
if _, err := os.Stat(h.options.Chain); err == nil {
chain, err = os.ReadFile(h.options.Chain)
if err != nil {
h.logger.Panic(err)
}
} else {
chain = []byte(h.options.Chain)
}
// make pool of chains
caPool = x509.NewCertPool()
if !caPool.AppendCertsFromPEM(chain) {
h.logger.Debug("CA chain is invalid")
}
}
mux := http.NewServeMux()
if !utils.IsEmpty(h.options.HealthcheckURL) {
mux.HandleFunc(h.options.HealthcheckURL, func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte("OK")); err != nil {
h.logger.Error("Can't write response: %v", err)
http.Error(w, fmt.Sprintf("could not write response: %v", err), http.StatusInternalServerError)
}
})
}
processors := h.getProcessors(h.processors, outputs)
for u, p := range processors {
h.processURL(u, mux, p)
}
listener, err := net.Listen("tcp", h.options.Listen)
if err != nil {
h.logger.Panic(err)
}
h.logger.Info("Http input is up. Listening...")
srv := &http.Server{
Handler: mux,
ErrorLog: nil,
}
if h.options.Tls {
srv.TLSConfig = &tls.Config{
Certificates: certificates,
RootCAs: caPool,
InsecureSkipVerify: h.options.Insecure,
ServerName: h.options.ServerName,
}
err = srv.ServeTLS(listener, "", "")
if err != nil {
h.logger.Panic(err)
}
} else {
err = srv.Serve(listener)
if err != nil {
h.logger.Panic(err)
}
}
}(wg)
}
func (h *HttpInput) setProcessor(m map[string]common.HttpProcessor, url string, t string) {
if !utils.IsEmpty(url) {
p := h.processors.FindHttpProcessor(common.AsEventType(t))
if p != nil {
m[url] = p
}
}
}
func (h *HttpInput) getProcessors(_ *common.Processors, _ *common.Outputs) map[string]common.HttpProcessor {
m := make(map[string]common.HttpProcessor)
h.setProcessor(m, h.options.K8sURL, processor.K8sProcessorType())
h.setProcessor(m, h.options.KubeURL, processor.KubeProcessorType())
h.setProcessor(m, h.options.WinEventURL, processor.WinEventProcessorType())
h.setProcessor(m, h.options.ObserviumEventURL, processor.ObserviumEventProcessorType())
h.setProcessor(m, h.options.AlertmanagerURL, processor.AlertmanagerProcessorType())
h.setProcessor(m, h.options.GitlabURL, processor.GitlabProcessorType())
h.setProcessor(m, h.options.RancherURL, processor.RancherProcessorType())
h.setProcessor(m, h.options.DataDogURL, processor.DataDogProcessorType())
h.setProcessor(m, h.options.Site24x7URL, processor.Site24x7ProcessorType())
h.setProcessor(m, h.options.CloudflareURL, processor.CloudflareProcessorType())
h.setProcessor(m, h.options.GoogleURL, processor.GoogleProcessorType())
h.setProcessor(m, h.options.AWSURL, processor.AWSProcessorType())
h.setProcessor(m, h.options.ZabbixURL, processor.ZabbixProcessorType())
h.setProcessor(m, h.options.VCenterURL, processor.VCenterProcessorType())
h.setProcessor(m, h.options.CustomJsonURL, processor.CustomJsonProcessorType())
h.setProcessor(m, h.options.TeamcityURL, processor.TeamcityProcessorType())
return m
}
func NewHttpInput(options HttpInputOptions, processors *common.Processors, observability *common.Observability) *HttpInput {
meter := observability.Metrics()
return &HttpInput{
options: options,
processors: processors,
tracer: observability.Traces(),
logger: observability.Logs(),
meter: meter,
requests: meter.Counter("requests", "Count of all http input requests", []string{"url"}, "http", "input"),
errors: meter.Counter("errors", "Count of all http input errors", []string{"url"}, "http", "input"),
}
}