forked from atlassian/gostatsd
/
http_receiver_v2.go
243 lines (210 loc) · 6.85 KB
/
http_receiver_v2.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
package web
import (
"context"
"io/ioutil"
"net/http"
"sync/atomic"
"time"
"github.com/golang/protobuf/proto"
"github.com/sirupsen/logrus"
"github.com/hligit/gostatsd"
"github.com/hligit/gostatsd/pb"
"github.com/hligit/gostatsd/pkg/stats"
)
type rawHttpHandlerV2 struct {
requestSuccess uint64 // atomic
requestFailureRead uint64 // atomic
requestFailureDecompress uint64 // atomic
requestFailureEncoding uint64 // atomic
requestFailureUnmarshal uint64 // atomic
metricsProcessed uint64 // atomic
eventsProcessed uint64 // atomic
logger logrus.FieldLogger
handler gostatsd.PipelineHandler
serverName string
}
func newRawHttpHandlerV2(logger logrus.FieldLogger, serverName string, handler gostatsd.PipelineHandler) *rawHttpHandlerV2 {
return &rawHttpHandlerV2{
logger: logger,
handler: handler,
serverName: serverName,
}
}
func (rhh *rawHttpHandlerV2) RunMetricsContext(ctx context.Context) {
statser := stats.FromContext(ctx).WithTags([]string{"server-name:" + rhh.serverName})
notify, cancel := statser.RegisterFlush()
defer cancel()
for {
select {
case <-notify:
rhh.emitMetrics(statser)
case <-ctx.Done():
return
}
}
}
func (rhh *rawHttpHandlerV2) emitMetrics(statser stats.Statser) {
requestSuccess := atomic.SwapUint64(&rhh.requestSuccess, 0)
requestFailureRead := atomic.SwapUint64(&rhh.requestFailureRead, 0)
requestFailureDecompress := atomic.SwapUint64(&rhh.requestFailureDecompress, 0)
requestFailureEncoding := atomic.SwapUint64(&rhh.requestFailureEncoding, 0)
requestFailureUnmarshal := atomic.SwapUint64(&rhh.requestFailureUnmarshal, 0)
metricsProcessed := atomic.SwapUint64(&rhh.metricsProcessed, 0)
eventsProcessed := atomic.SwapUint64(&rhh.eventsProcessed, 0)
statser.Count("http.incoming", float64(requestSuccess), []string{"result:success"})
statser.Count("http.incoming", float64(requestFailureRead), []string{"result:failure", "failure:read"})
statser.Count("http.incoming", float64(requestFailureDecompress), []string{"result:failure", "failure:decompress"})
statser.Count("http.incoming", float64(requestFailureEncoding), []string{"result:failure", "failure:encoding"})
statser.Count("http.incoming", float64(requestFailureUnmarshal), []string{"result:failure", "failure:unmarshal"})
statser.Count("http.incoming.metrics", float64(metricsProcessed), nil)
statser.Count("http.incoming.events", float64(eventsProcessed), nil)
}
func (rhh *rawHttpHandlerV2) readBody(req *http.Request) ([]byte, int) {
b, err := ioutil.ReadAll(req.Body)
if err != nil {
atomic.AddUint64(&rhh.requestFailureRead, 1)
rhh.logger.WithError(err).Info("failed reading body")
return nil, http.StatusInternalServerError
}
req.Body.Close()
encoding := req.Header.Get("Content-Encoding")
switch encoding {
case "deflate":
b, err = decompress(b)
if err != nil {
atomic.AddUint64(&rhh.requestFailureDecompress, 1)
rhh.logger.WithError(err).Info("failed decompressing body")
return nil, http.StatusBadRequest
}
case "identity", "":
// no action
default:
atomic.AddUint64(&rhh.requestFailureEncoding, 1)
if len(encoding) > 64 {
encoding = encoding[0:64]
}
rhh.logger.WithField("encoding", encoding).Info("invalid encoding")
return nil, http.StatusBadRequest
}
return b, 0
}
func (rhh *rawHttpHandlerV2) MetricHandler(w http.ResponseWriter, req *http.Request) {
b, errCode := rhh.readBody(req)
if errCode != 0 {
w.WriteHeader(errCode)
return
}
var msg pb.RawMessageV2
err := proto.Unmarshal(b, &msg)
if err != nil {
atomic.AddUint64(&rhh.requestFailureUnmarshal, 1)
rhh.logger.WithError(err).Error("failed to unmarshal")
w.WriteHeader(http.StatusBadRequest)
return
}
mm := translateFromProtobufV2(&msg)
rhh.handler.DispatchMetricMap(req.Context(), mm)
atomic.AddUint64(&rhh.requestSuccess, 1)
w.WriteHeader(http.StatusAccepted)
}
func (rhh *rawHttpHandlerV2) EventHandler(w http.ResponseWriter, req *http.Request) {
b, errCode := rhh.readBody(req)
if errCode != 0 {
w.WriteHeader(errCode)
return
}
var msg pb.EventV2
err := proto.Unmarshal(b, &msg)
if err != nil {
atomic.AddUint64(&rhh.requestFailureUnmarshal, 1)
rhh.logger.WithError(err).Error("failed to unmarshal")
w.WriteHeader(http.StatusBadRequest)
return
}
event := &gostatsd.Event{
Title: msg.Title,
Text: msg.Text,
DateHappened: msg.DateHappened,
Source: gostatsd.Source(msg.Hostname),
AggregationKey: msg.AggregationKey,
SourceTypeName: msg.SourceTypeName,
Tags: msg.Tags,
}
switch msg.Priority {
case pb.EventV2_Normal:
event.Priority = gostatsd.PriNormal
case pb.EventV2_Low:
event.Priority = gostatsd.PriLow
default:
event.Priority = gostatsd.PriNormal
}
switch msg.Type {
case pb.EventV2_Info:
event.AlertType = gostatsd.AlertInfo
case pb.EventV2_Warning:
event.AlertType = gostatsd.AlertWarning
case pb.EventV2_Error:
event.AlertType = gostatsd.AlertError
case pb.EventV2_Success:
event.AlertType = gostatsd.AlertSuccess
default:
event.AlertType = gostatsd.AlertInfo
}
rhh.handler.DispatchEvent(req.Context(), event)
atomic.AddUint64(&rhh.eventsProcessed, 1)
atomic.AddUint64(&rhh.requestSuccess, 1)
w.WriteHeader(http.StatusAccepted)
}
func translateFromProtobufV2(pbMetricMap *pb.RawMessageV2) *gostatsd.MetricMap {
now := gostatsd.Nanotime(time.Now().UnixNano())
mm := gostatsd.NewMetricMap()
for metricName, tagMap := range pbMetricMap.Gauges {
mm.Gauges[metricName] = map[string]gostatsd.Gauge{}
for tagsKey, gauge := range tagMap.TagMap {
mm.Gauges[metricName][tagsKey] = gostatsd.Gauge{
Value: gauge.Value,
Timestamp: now,
Source: gostatsd.Source(gauge.Hostname),
Tags: gauge.Tags,
}
}
}
for metricName, tagMap := range pbMetricMap.Counters {
mm.Counters[metricName] = map[string]gostatsd.Counter{}
for tagsKey, counter := range tagMap.TagMap {
mm.Counters[metricName][tagsKey] = gostatsd.Counter{
Value: counter.Value,
Timestamp: now,
Tags: counter.Tags,
Source: gostatsd.Source(counter.Hostname),
}
}
}
for metricName, tagMap := range pbMetricMap.Timers {
mm.Timers[metricName] = map[string]gostatsd.Timer{}
for tagsKey, timer := range tagMap.TagMap {
mm.Timers[metricName][tagsKey] = gostatsd.Timer{
Values: timer.Values,
Timestamp: now,
Tags: timer.Tags,
Source: gostatsd.Source(timer.Hostname),
SampledCount: timer.SampleCount,
}
}
}
for metricName, tagMap := range pbMetricMap.Sets {
mm.Sets[metricName] = map[string]gostatsd.Set{}
for tagsKey, set := range tagMap.TagMap {
mm.Sets[metricName][tagsKey] = gostatsd.Set{
Values: map[string]struct{}{},
Timestamp: now,
Tags: set.Tags,
Source: gostatsd.Source(set.Hostname),
}
for _, value := range set.Values {
mm.Sets[metricName][tagsKey].Values[value] = struct{}{}
}
}
}
return mm
}