forked from signalfx/golib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpsink.go
334 lines (303 loc) · 9.85 KB
/
httpsink.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package sfxclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"runtime"
"strings"
"sync/atomic"
"time"
"unicode"
"github.com/golang/protobuf/proto"
"github.com/signalfx/com_signalfx_metrics_protobuf"
"github.com/signalfx/golib/datapoint"
"github.com/signalfx/golib/errors"
"github.com/signalfx/golib/event"
"golang.org/x/net/context"
)
// ClientVersion is the version of this library and is embedded into the user agent
const ClientVersion = "1.0"
// IngestEndpointV2 is the v2 version of the signalfx ingest endpoint
const IngestEndpointV2 = "https://ingest.signalfx.com/v2/datapoint"
// EventIngestEndpointV2 is the v2 version of the signalfx event endpoint
const EventIngestEndpointV2 = "https://ingest.signalfx.com/v2/event"
// DefaultUserAgent is the UserAgent string sent to signalfx
var DefaultUserAgent = fmt.Sprintf("golib-sfxclient/%s (gover %s)", ClientVersion, runtime.Version())
// DefaultTimeout is the default time to fail signalfx datapoint requests if they don't succeed
const DefaultTimeout = time.Second * 5
// HTTPSink -
type HTTPSink struct {
AuthToken string
UserAgent string
EventEndpoint string
DatapointEndpoint string
Client http.Client
protoMarshaler func(pb proto.Message) ([]byte, error)
stats struct {
readingBody int64
}
}
func (h *HTTPSink) handleResponse(resp *http.Response, respErr error) (err error) {
if respErr != nil {
return errors.Annotatef(respErr, "failed to send/recieve http request")
}
defer func() {
closeErr := errors.Annotate(resp.Body.Close(), "failed to close response body")
err = errors.NewMultiErr([]error{err, closeErr})
}()
atomic.AddInt64(&h.stats.readingBody, 1)
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Annotate(err, "cannot fully read response body")
}
if resp.StatusCode != http.StatusOK {
return errors.Errorf("invalid status code %d", resp.StatusCode)
}
var bodyStr string
err = json.Unmarshal(respBody, &bodyStr)
if err != nil {
return errors.Annotatef(err, "cannot unmarshal response body %s", respBody)
}
if bodyStr != "OK" {
return errors.Errorf("invalid response body %s", bodyStr)
}
return nil
}
var _ Sink = &HTTPSink{}
// TokenHeaderName is the header key for the auth token in the HTTP request
const TokenHeaderName = "X-Sf-Token"
// AddDatapoints forwards the datapoints to SignalFx.
func (h *HTTPSink) AddDatapoints(ctx context.Context, points []*datapoint.Datapoint) (err error) {
if len(points) == 0 {
return nil
}
if ctx.Err() != nil {
return errors.Annotate(ctx.Err(), "context already closed")
}
body, err := h.encodePostBodyProtobufV2(points)
if err != nil {
return errors.Annotate(err, "cannot encode datapoints into protocol buffers")
}
req, err := http.NewRequest("POST", h.DatapointEndpoint, bytes.NewReader(body))
if err != nil {
return errors.Annotatef(err, "cannot parse new HTTP request to %s", h.DatapointEndpoint)
}
req.Header.Set("Content-Type", "application/x-protobuf")
req.Header.Set(TokenHeaderName, h.AuthToken)
req.Header.Set("User-Agent", h.UserAgent)
req.Header.Set("Connection", "Keep-Alive")
return h.withCancel(ctx, req)
}
var toMTMap = map[datapoint.MetricType]com_signalfx_metrics_protobuf.MetricType{
datapoint.Counter: com_signalfx_metrics_protobuf.MetricType_CUMULATIVE_COUNTER,
datapoint.Count: com_signalfx_metrics_protobuf.MetricType_COUNTER,
datapoint.Enum: com_signalfx_metrics_protobuf.MetricType_GAUGE,
datapoint.Gauge: com_signalfx_metrics_protobuf.MetricType_GAUGE,
datapoint.Rate: com_signalfx_metrics_protobuf.MetricType_GAUGE,
datapoint.Timestamp: com_signalfx_metrics_protobuf.MetricType_GAUGE,
}
func toMT(mt datapoint.MetricType) com_signalfx_metrics_protobuf.MetricType {
ret, exists := toMTMap[mt]
if exists {
return ret
}
panic(fmt.Sprintf("Unknown metric type: %d\n", mt))
}
func toEC(ec event.Category) com_signalfx_metrics_protobuf.EventCategory {
// Check if the event.Category does not have a corresponding com_signalfx_metrics_protobuf.EventCategory
if _, ok := com_signalfx_metrics_protobuf.EventCategory_name[int32(ec)]; !ok {
panic(fmt.Sprintf("Unknown event category: %v\n", ec))
}
// Return the com_signalfx_metrics_protobuf.EventCategory
return com_signalfx_metrics_protobuf.EventCategory(int32(ec))
}
func datumForPoint(pv datapoint.Value) *com_signalfx_metrics_protobuf.Datum {
switch t := pv.(type) {
case datapoint.IntValue:
x := t.Int()
return &com_signalfx_metrics_protobuf.Datum{IntValue: &x}
case datapoint.FloatValue:
x := t.Float()
return &com_signalfx_metrics_protobuf.Datum{DoubleValue: &x}
default:
x := t.String()
return &com_signalfx_metrics_protobuf.Datum{StrValue: &x}
}
}
func mapToDimensions(dimensions map[string]string) []*com_signalfx_metrics_protobuf.Dimension {
ret := make([]*com_signalfx_metrics_protobuf.Dimension, 0, len(dimensions))
for k, v := range dimensions {
if k == "" || v == "" {
continue
}
// If someone knows a better way to do this, let me know. I can't just take the &
// of k and v because their content changes as the range iterates
copyOfK := filterSignalfxKey(string([]byte(k)))
copyOfV := (string([]byte(v)))
ret = append(ret, (&com_signalfx_metrics_protobuf.Dimension{
Key: ©OfK,
Value: ©OfV,
}))
}
return ret
}
func filterSignalfxKey(str string) string {
return strings.Map(runeFilterMap, str)
}
func runeFilterMap(r rune) rune {
if unicode.IsDigit(r) || unicode.IsLetter(r) || r == '_' {
return r
}
return '_'
}
func rawToProtobuf(raw interface{}) *com_signalfx_metrics_protobuf.PropertyValue {
switch t := raw.(type) {
case int64:
return &com_signalfx_metrics_protobuf.PropertyValue{
IntValue: &t,
}
case int:
return &com_signalfx_metrics_protobuf.PropertyValue{
IntValue: proto.Int64(int64(t)),
}
case float64:
return &com_signalfx_metrics_protobuf.PropertyValue{
DoubleValue: &t,
}
case bool:
return &com_signalfx_metrics_protobuf.PropertyValue{
BoolValue: &t,
}
case string:
return &com_signalfx_metrics_protobuf.PropertyValue{
StrValue: &t,
}
}
return nil
}
func (h *HTTPSink) coreDatapointToProtobuf(point *datapoint.Datapoint) *com_signalfx_metrics_protobuf.DataPoint {
m := point.Metric
var ts int64
if point.Timestamp.IsZero() {
ts = 0
} else {
ts = point.Timestamp.UnixNano() / time.Millisecond.Nanoseconds()
}
mt := toMT(point.MetricType)
dp := &com_signalfx_metrics_protobuf.DataPoint{
Metric: &m,
Timestamp: &ts,
Value: datumForPoint(point.Value),
MetricType: &mt,
Dimensions: mapToDimensions(point.Dimensions),
}
for k, v := range point.GetProperties() {
kv := k
pv := rawToProtobuf(v)
if pv != nil && k != "" {
dp.Properties = append(dp.Properties, &com_signalfx_metrics_protobuf.Property{
Key: &kv,
Value: pv,
})
}
}
return dp
}
func (h *HTTPSink) encodePostBodyProtobufV2(datapoints []*datapoint.Datapoint) ([]byte, error) {
dps := make([]*com_signalfx_metrics_protobuf.DataPoint, 0, len(datapoints))
for _, dp := range datapoints {
dps = append(dps, h.coreDatapointToProtobuf(dp))
}
msg := &com_signalfx_metrics_protobuf.DataPointUploadMessage{
Datapoints: dps,
}
body, err := h.protoMarshaler(msg)
if err != nil {
return nil, errors.Annotate(err, "protobuf marshal failed")
}
return body, nil
}
// AddEvents forwards the events to SignalFx.
func (h *HTTPSink) AddEvents(ctx context.Context, events []*event.Event) (err error) {
if len(events) == 0 {
return nil
}
if ctx.Err() != nil {
return errors.Annotate(ctx.Err(), "context already closed")
}
body, err := h.encodePostBodyProtobufV2Events(events)
if err != nil {
return errors.Annotate(err, "cannot encode events into protocol buffers")
}
req, err := http.NewRequest("POST", h.EventEndpoint, bytes.NewReader(body))
if err != nil {
return errors.Annotatef(err, "cannot parse new HTTP request to %s", h.EventEndpoint)
}
req.Header.Set("Content-Type", "application/x-protobuf")
req.Header.Set(TokenHeaderName, h.AuthToken)
req.Header.Set("User-Agent", h.UserAgent)
req.Header.Set("Connection", "Keep-Alive")
return h.withCancel(ctx, req)
}
func (h *HTTPSink) encodePostBodyProtobufV2Events(events []*event.Event) ([]byte, error) {
evs := make([]*com_signalfx_metrics_protobuf.Event, 0, len(events))
for _, ev := range events {
evs = append(evs, h.coreEventToProtobuf(ev))
}
msg := &com_signalfx_metrics_protobuf.EventUploadMessage{
Events: evs,
}
body, err := h.protoMarshaler(msg)
if err != nil {
return nil, errors.Annotate(err, "protobuf marshal failed")
}
return body, nil
}
func (h *HTTPSink) coreEventToProtobuf(event *event.Event) *com_signalfx_metrics_protobuf.Event {
var ts int64
if event.Timestamp.IsZero() {
ts = 0
} else {
ts = event.Timestamp.UnixNano() / time.Millisecond.Nanoseconds()
}
etype := event.EventType
ecat := toEC(event.Category)
ev := &com_signalfx_metrics_protobuf.Event{
EventType: &etype,
Category: &ecat,
Dimensions: mapToDimensions(event.Dimensions),
Properties: mapToProperties(event.Properties),
Timestamp: &ts,
}
return ev
}
func mapToProperties(properties map[string]interface{}) []*com_signalfx_metrics_protobuf.Property {
var response = make([]*com_signalfx_metrics_protobuf.Property, 0, len(properties))
for k, v := range properties {
kv := k
pv := rawToProtobuf(v)
if pv != nil && k != "" {
response = append(response, &com_signalfx_metrics_protobuf.Property{
Key: &kv,
Value: pv,
})
}
}
return response
}
// NewHTTPSink creates a default NewHTTPSink using package level constants as
// defaults, including an empty auth token. If sending directly to SiganlFx, you will be required
// to explicitly set the AuthToken
func NewHTTPSink() *HTTPSink {
return &HTTPSink{
EventEndpoint: EventIngestEndpointV2,
DatapointEndpoint: IngestEndpointV2,
UserAgent: DefaultUserAgent,
Client: http.Client{
Timeout: DefaultTimeout,
},
protoMarshaler: proto.Marshal,
}
}