-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.go
176 lines (159 loc) · 3.91 KB
/
endpoint.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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"time"
"github.com/go-kit/kit/log"
)
type fetcher interface {
Get(context.Context, string) (*http.Response, error)
}
type httpFetcher struct{}
type endpoint struct {
url string
prefix string
failureMetric string
checkInterval int
timeout int
ignoreMetrics map[string]struct{}
renames map[string]string
graphiteServer submitable
fetcher fetcher
logger log.Logger
}
type metric struct {
Name string
Value float64
}
func newEndpoint(c endpointconfig, interval int, timeout int, ignoreMetrics []string, renameMetrics []renameConfig, g submitable, fetcher fetcher, logger log.Logger) *endpoint {
if c.CheckInterval != 0 {
interval = c.CheckInterval
}
if c.Timeout != 0 {
timeout = c.Timeout
}
if len(c.IgnoreMetrics) > 0 {
ignoreMetrics = c.IgnoreMetrics
}
if len(c.Renames) > 0 {
renameMetrics = c.Renames
}
ignoreMap := make(map[string]struct{})
for _, m := range ignoreMetrics {
ignoreMap[m] = struct{}{}
}
renameMap := make(map[string]string)
for _, r := range renameMetrics {
renameMap[r.From] = r.To
}
return &endpoint{
url: c.URL,
prefix: c.Prefix,
checkInterval: interval,
failureMetric: c.FailureMetric,
timeout: timeout,
ignoreMetrics: ignoreMap,
renames: renameMap,
graphiteServer: g,
fetcher: fetcher,
logger: logger,
}
}
func (h httpFetcher) Get(ctx context.Context, url string) (*http.Response, error) {
req, _ := http.NewRequest("GET", url, nil)
return http.DefaultClient.Do(req.WithContext(ctx))
}
func (e *endpoint) Fetch(ctx context.Context) (map[string]interface{}, error) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(e.timeout*int(time.Millisecond)))
defer cancel()
resp, err := e.fetcher.Get(ctx, e.url)
if err != nil {
return nil, err
}
if resp.Status != "200 OK" {
return nil, err
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
var f interface{}
err = json.Unmarshal(b, &f)
if err != nil {
return nil, err
}
return f.(map[string]interface{}), nil
}
func (e *endpoint) Gather(ctx context.Context) []metric {
var metrics []metric
m, err := e.Fetch(ctx)
if err != nil {
e.logger.Log("msg", "fetch failed", "error", err)
if e.failureMetric != "" {
// failure
f := metric{Name: e.failureMetric, Value: 1.0}
metrics = append(metrics, f)
}
return metrics
}
e.logger.Log("msg", "good fetch")
metrics = metricsFromMap(m, e.prefix, e.ignoreMetrics, e.renames)
// success
if e.failureMetric != "" {
s := metric{Name: e.failureMetric, Value: 0.0}
metrics = append(metrics, s)
}
return metrics
}
func metricsFromMap(m map[string]interface{}, prefix string, ignore map[string]struct{}, renames map[string]string) []metric {
var metrics []metric
for k, v := range m {
_, ok := ignore[k]
if ok {
continue
}
t, ok := renames[k]
if ok {
k = t
}
key := fmt.Sprintf("%s.%s", prefix, k)
switch vv := v.(type) {
case float64:
metrics = append(metrics, metric{key, vv})
case map[string]interface{}:
nmetrics := metricsFromMap(vv, key, ignore, renames)
for _, met := range nmetrics {
metrics = append(metrics, met)
}
}
// default: nothing to do
}
return metrics
}
func (e *endpoint) Submit(metrics []metric) error {
return e.graphiteServer.Submit(metrics)
}
func jitter(interval int) int {
if interval > 10 {
return rand.Intn(interval / 10)
}
return 0
}
func (e *endpoint) Run(ctx context.Context) {
e.logger.Log("msg", "endpoint starting")
for {
select {
case <-ctx.Done():
e.logger.Log("msg", "context cancelled. exiting")
return
case <-time.After(time.Duration(e.checkInterval+jitter(e.checkInterval)) * time.Millisecond):
metrics := e.Gather(ctx)
err := e.Submit(metrics)
if err != nil {
e.logger.Log("msg", "submission to graphite failed", "error", err)
}
}
}
}