forked from mihasya/go-metrics-librato
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
107 lines (87 loc) · 2.2 KB
/
client.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
package appoptics
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const Operations = "operations"
const OperationsShort = "ops"
type AppOpticsClient struct {
Token string
}
// property strings
const (
// display attributes
Color = "color"
DisplayMax = "display_max"
DisplayMin = "display_min"
DisplayUnitsLong = "display_units_long"
DisplayUnitsShort = "display_units_short"
DisplayStacked = "display_stacked"
DisplayTransform = "display_transform"
// special gauge display attributes
SummarizeFunction = "summarize_function"
Aggregate = "aggregate"
// metric keys
Name = "name"
Period = "period"
Description = "description"
DisplayName = "display_name"
Attributes = "attributes"
// measurement keys
Time = "time"
Tags = "tags"
Value = "value"
// special gauge keys
Count = "count"
Sum = "sum"
Max = "max"
Min = "min"
StdDev = "stddev"
// batch keys
Measurements = "measurements"
MetricsPostUrl = "https://api.appoptics.com/v1/measurements"
)
type Measurement map[string]interface{}
type Metric map[string]interface{}
type Batch struct {
Measurements []Measurement `json:"measurements,omitempty"`
Time int64 `json:"time"`
Tags map[string]string `json:"tags"`
}
var client = http.DefaultClient
func SetHTTPClient(c *http.Client) {
client = c
}
func (self *AppOpticsClient) PostMetrics(batch Batch) (err error) {
var (
js []byte
req *http.Request
resp *http.Response
)
if len(batch.Measurements) == 0 {
return nil
}
if js, err = json.Marshal(batch); err != nil {
return
}
if req, err = http.NewRequest("POST", MetricsPostUrl, bytes.NewBuffer(js)); err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(self.Token, "")
if resp, err = client.Do(req); err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
var body []byte
if body, err = ioutil.ReadAll(resp.Body); err != nil {
body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err))
}
err = fmt.Errorf("Unable to post to AppOptics: %d %s %s", resp.StatusCode, resp.Status, string(body))
}
return
}