-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
296 lines (258 loc) · 9.75 KB
/
api.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
package monibot
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"github.com/cvilsmeier/monibot-go/histogram"
"github.com/cvilsmeier/monibot-go/internal/sending"
)
// ApiOptions holds optional parameters for a Api.
type ApiOptions struct {
// Default is no logging. If you want debug logging: Bring your own logger.
Logger Logger
// Default is "https://monibot.io".
MonibotUrl string
// Default is 12 trials.
Trials int
// Default is 5s delay.
Delay time.Duration
// Default is time.After (this is only used in tests and hence not exported).
timeAfter sending.TimeAfterFunc
}
// An apiSender provides a Send method and can be overridden in unit tests.
type apiSender interface {
Send(ctx context.Context, method, path string, body []byte) ([]byte, error)
}
// Api provides access to the Monibot REST API.
// See https://monibot.io/docs/rest-api for authorization,
// rate-limits and usage.
type Api struct {
sender apiSender
}
// NewApi creates an Api that sends data to https://monibot.io
// and retries 12 times every 5s if an error occurs,
// and logs nothing.
func NewApi(apiKey string) *Api {
return NewApiWithOptions(apiKey, ApiOptions{})
}
// NewApiWithOptions creates an Api with custom options.
func NewApiWithOptions(apiKey string, opt ApiOptions) *Api {
logger := opt.Logger
if logger == nil {
logger = zeroLogger{}
}
monibotUrl := opt.MonibotUrl
if monibotUrl == "" {
monibotUrl = "http://monibot.io"
}
trials := opt.Trials
if trials == 0 {
trials = 12
}
delay := opt.Delay
if delay == 0 {
delay = 5 * time.Second
}
timeAfter := opt.timeAfter
if timeAfter == nil {
timeAfter = time.After
}
transport := sending.NewTransport(logger, Version, monibotUrl, apiKey)
sender := sending.NewSender(transport, logger, trials, delay, timeAfter)
return &Api{sender}
}
// GetPing is like GetPingWithContext using context.Background.
func (a *Api) GetPing() error {
return a.GetPingWithContext(context.Background())
}
// GetPingWithContext pings the API.
// It is used to ensure everything is set up correctly and the API is
// reachable. It returns nil on success or a non-nil error if
// something goes wrong.
func (a *Api) GetPingWithContext(ctx context.Context) error {
_, err := a.sender.Send(ctx, "GET", "ping", nil)
return err
}
// GetWatchdogs is like GetWatchdogsWithContext using context.Background.
func (a *Api) GetWatchdogs() ([]Watchdog, error) {
return a.GetWatchdogsWithContext(context.Background())
}
// GetWatchdogsWithContext fetches the list of watchdogs.
func (a *Api) GetWatchdogsWithContext(ctx context.Context) ([]Watchdog, error) {
data, err := a.sender.Send(ctx, "GET", "watchdogs", nil)
if err != nil {
return nil, err
}
var watchdogs []Watchdog
err = json.Unmarshal(data, &watchdogs)
return watchdogs, err
}
// GetWatchdog is like GetWatchdogWithContext using context.Background.
func (a *Api) GetWatchdog(watchdogId string) (Watchdog, error) {
return a.GetWatchdogWithContext(context.Background(), watchdogId)
}
// GetWatchdogWithContext fetches a watchdog by id.
func (a *Api) GetWatchdogWithContext(ctx context.Context, watchdogId string) (Watchdog, error) {
data, err := a.sender.Send(ctx, "GET", "watchdog/"+watchdogId, nil)
if err != nil {
return Watchdog{}, err
}
var w Watchdog
err = json.Unmarshal(data, &w)
return w, err
}
// PostWatchdogHeartbeat is like PostWatchdogHeartbeatWithContext using context.Background.
func (a *Api) PostWatchdogHeartbeat(watchdogId string) error {
return a.PostWatchdogHeartbeatWithContext(context.Background(), watchdogId)
}
// PostWatchdogHeartbeatWithContext sends a watchdog heartbeat.
func (a *Api) PostWatchdogHeartbeatWithContext(ctx context.Context, watchdogId string) error {
_, err := a.sender.Send(ctx, "POST", "watchdog/"+watchdogId+"/heartbeat", nil)
return err
}
// GetMachines is like GetMachinesWithContext using context.Background.
func (a *Api) GetMachines() ([]Machine, error) {
return a.GetMachinesWithContext(context.Background())
}
// GetMachinesWithContext fetches the list of machines.
func (a *Api) GetMachinesWithContext(ctx context.Context) ([]Machine, error) {
data, err := a.sender.Send(ctx, "GET", "machines", nil)
if err != nil {
return nil, err
}
var machines []Machine
err = json.Unmarshal(data, &machines)
return machines, err
}
// GetMachine is like GetMachineWithContext using context.Background.
func (a *Api) GetMachine(machineId string) (Machine, error) {
return a.GetMachineWithContext(context.Background(), machineId)
}
// GetMachineWithContext fetches a machine by id.
func (a *Api) GetMachineWithContext(ctx context.Context, machineId string) (Machine, error) {
data, err := a.sender.Send(ctx, "GET", "machine/"+machineId, nil)
if err != nil {
return Machine{}, err
}
var machine Machine
err = json.Unmarshal(data, &machine)
return machine, err
}
// PostMachineSample is like PostMachineSampleWithContext using context.Background.
func (a *Api) PostMachineSample(machineId string, sample MachineSample) error {
return a.PostMachineSampleWithContext(context.Background(), machineId, sample)
}
// PostMachineSampleWithContext uploads a machine sample to the API.
func (a *Api) PostMachineSampleWithContext(ctx context.Context, machineId string, sample MachineSample) error {
toks := []string{
fmt.Sprintf("tstamp=%d", sample.Tstamp),
fmt.Sprintf("load1=%.3f", sample.Load1),
fmt.Sprintf("load5=%.3f", sample.Load5),
fmt.Sprintf("load15=%.3f", sample.Load15),
fmt.Sprintf("cpu=%d", sample.CpuPercent),
fmt.Sprintf("mem=%d", sample.MemPercent),
fmt.Sprintf("disk=%d", sample.DiskPercent),
fmt.Sprintf("diskReads=%d", sample.DiskReads),
fmt.Sprintf("diskWrites=%d", sample.DiskWrites),
fmt.Sprintf("netRecv=%d", sample.NetRecv),
fmt.Sprintf("netSend=%d", sample.NetSend),
}
body := strings.Join(toks, "&")
_, err := a.sender.Send(ctx, "POST", "machine/"+machineId+"/sample", []byte(body))
return err
}
// PostMachineText is like PostMachineTextWithContext using context.Background.
func (a *Api) PostMachineText(machineId string, text string) error {
return a.PostMachineTextWithContext(context.Background(), machineId, text)
}
// PostMachineTextWithContext uploads a machine text to the API.
func (a *Api) PostMachineTextWithContext(ctx context.Context, machineId string, text string) error {
body := "text=" + url.QueryEscape(text)
_, err := a.sender.Send(ctx, "POST", "machine/"+machineId+"/text", []byte(body))
return err
}
// GetMetrics is like GetMetricsWithContext using context.Background.
func (a *Api) GetMetrics() ([]Metric, error) {
return a.GetMetricsWithContext(context.Background())
}
// GetMetricsWithContext fetches the list of metrics.
func (a *Api) GetMetricsWithContext(ctx context.Context) ([]Metric, error) {
data, err := a.sender.Send(ctx, "GET", "metrics", nil)
if err != nil {
return nil, err
}
var metrics []Metric
err = json.Unmarshal(data, &metrics)
return metrics, err
}
// GetMetric is like GetMetricWithContext using context.Background.
func (a *Api) GetMetric(metricId string) (Metric, error) {
return a.GetMetricWithContext(context.Background(), metricId)
}
// GetMetricWithContext fetches a metric by id.
func (a *Api) GetMetricWithContext(ctx context.Context, metricId string) (Metric, error) {
data, err := a.sender.Send(ctx, "GET", "metric/"+metricId, nil)
if err != nil {
return Metric{}, err
}
var metric Metric
err = json.Unmarshal(data, &metric)
return metric, err
}
// PostMetricInc is like PostMetricIncWithContext using context.Background.
func (a *Api) PostMetricInc(metricId string, value int64) error {
if value < 0 {
return fmt.Errorf("cannot send negative value %d", value)
}
return a.PostMetricIncWithContext(context.Background(), metricId, value)
}
// PostMetricIncWithContext uploads a counter metric increment value to the API.
// It is used to increment a counter metric.
// It is an error to try to increment a non-counter metric.
// The value is a non-negative int64 number.
func (a *Api) PostMetricIncWithContext(ctx context.Context, metricId string, value int64) error {
if value < 0 {
return fmt.Errorf("cannot send negative value %d", value)
}
body := fmt.Sprintf("value=%d", value)
_, err := a.sender.Send(ctx, "POST", "metric/"+metricId+"/inc", []byte(body))
return err
}
// PostMetricSet is like PostMetricSetWithContext using context.Background.
func (a *Api) PostMetricSet(metricId string, value int64) error {
return a.PostMetricSetWithContext(context.Background(), metricId, value)
}
// PostMetricSetWithContext uploads a gauge metric value to the API.
// It is used to set a gauge metric.
// It is an error to try to set a non-gauge metric.
// The value is a non-negative int64 number.
func (a *Api) PostMetricSetWithContext(ctx context.Context, metricId string, value int64) error {
if value < 0 {
return fmt.Errorf("cannot send negative value %d", value)
}
body := fmt.Sprintf("value=%d", value)
_, err := a.sender.Send(ctx, "POST", "metric/"+metricId+"/set", []byte(body))
return err
}
// PostMetricValues is like PostMetricValuesWithContext using context.Background.
func (a *Api) PostMetricValues(metricId string, values []int64) error {
return a.PostMetricValuesWithContext(context.Background(), metricId, values)
}
// PostMetricValuesWithContext uploads histogram metric values to the API.
// It is used to set a histogram metric.
// It is an error to try to send values to a non-histogram metric.
// Each values must be a non-negative 64-bit integer value.
func (a *Api) PostMetricValuesWithContext(ctx context.Context, metricId string, values []int64) error {
for _, value := range values {
if value < 0 {
return fmt.Errorf("cannot send negative value %d", value)
}
}
valuesStr := histogram.StringifyValues(values)
body := fmt.Sprintf("values=%s", url.QueryEscape(valuesStr))
_, err := a.sender.Send(ctx, "POST", "metric/"+metricId+"/values", []byte(body))
return err
}