-
Notifications
You must be signed in to change notification settings - Fork 0
/
jqm.go
121 lines (104 loc) · 2.62 KB
/
jqm.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
)
// Config file format
// all fields are required
type Config struct {
Jqm_config struct {
Url string `json:"url"`
ApiKey string `json:"api_key"`
Path string `json:"path"`
Prefix string `json:"prefix"`
Env string `json:"env"`
} `json:"config"`
}
// BMO's jobqueue server responds with a JSON object
// containing only total and errors
// {"total": <int>, "errors": <int>}
type JQResponse struct {
JQResponse struct {
Totals int `json:"total"`
Errors int `json:"errors"`
} `json:"response"`
}
func LoadConfig(filename string) (config Config, err error) {
configFile, err := os.Open(filename)
defer configFile.Close()
if err != nil {
log.Fatal(err)
}
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&config)
return config, err
}
func QueryEndpoint(endpoint string) (metrics []byte, err error) {
client := new(http.Client)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "curl/7.64.1")
req.Header.Set("Accept-Encoding", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
metrics, err = ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
}
return metrics, nil
}
func ParseMetrics(metrics []byte) (total int, errors int, err error) {
// https://stackoverflow.com/questions/40429296/converting-string-to-json-or-struct
m := []byte(metrics)
JQ := &JQResponse{}
if err := json.Unmarshal(m, JQ); err != nil {
return 0, 0, err
}
return JQ.JQResponse.Totals, JQ.JQResponse.Errors, nil
}
func SendMetrics(total int, errors int, prefix string, env string) (err error) {
conn, err := net.Dial("udp", "localhost:8125")
if err != nil {
return err
}
total_txt := []string{prefix, env, ".total:", strconv.Itoa(total), "|c"}
errors_txt := []string{prefix, env, ".errors:", strconv.Itoa(errors), "|c"}
defer conn.Close()
fmt.Fprintf(conn, (strings.Join(total_txt, "") + "\n"))
fmt.Fprintf(conn, (strings.Join(errors_txt, "") + "\n"))
return err
}
func main() {
conf, err := LoadConfig("jqm.json")
if err != nil {
log.Fatal(err)
}
completeUrl := (conf.Jqm_config.Url + conf.Jqm_config.Path + conf.Jqm_config.ApiKey)
returnedMetrics, err := QueryEndpoint(completeUrl)
if err != nil {
log.Fatal(err)
}
totals, errors, err := ParseMetrics(returnedMetrics)
if err != nil {
log.Fatal(err)
}
err = SendMetrics(totals, errors, conf.Jqm_config.Prefix, conf.Jqm_config.Env)
if err != nil {
log.Fatal(err)
}
}