-
Notifications
You must be signed in to change notification settings - Fork 3
/
postez.go
84 lines (72 loc) · 1.7 KB
/
postez.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
package stathat
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
// Kind indicates the type of stat posting
type Kind int
// Kinds
const (
KindValue Kind = iota + 1
KindCounter
)
// ErrKindMissing means the `Kind` was missing.
var ErrKindMissing = errors.New("kind missing")
// PostEZ posts the value to the stat using the EZ API.
// If you supply a non-nil time, it'll use that as the time value, otherwise it will not send a time value.
// This is probably what you want to do if posting stats.
func (s StatHat) PostEZ(name string, kind Kind, v float64, t *time.Time) error {
if s.noop {
return nil
}
u, _ := url.Parse(s.ezPrefix())
q := u.Query()
if len(s.ezkey) == 0 {
return ErrMissingEZKey
}
q.Add("ezkey", s.ezkey)
q.Add("stat", name)
if t != nil && !t.IsZero() {
q.Add("t", strconv.FormatInt(t.Unix(), 10))
}
if kind == KindValue {
q.Add("value", strconv.FormatFloat(v, 'g', -1, 64))
} else if kind == KindCounter {
q.Add("count", strconv.FormatFloat(v, 'g', -1, 64))
} else {
return ErrKindMissing
}
u.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodPost, u.String(), nil)
if err != nil {
return err
}
resp, err := httpDo(req)
if err != nil {
return err
}
defer resp.Body.Close()
// StatHat may return HTTP Status Code 204 to indicate success.
// See: https://blog.stathat.com/2017/05/05/bandwidth.html
if resp.StatusCode == http.StatusNoContent {
return nil
}
var respJSON struct {
// {"msg":"stat deleted."}
Msg string
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, &respJSON)
if respJSON.Msg != "ok" {
err = errors.New(respJSON.Msg)
}
return err
}