-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphiteClient.go
More file actions
55 lines (42 loc) · 1.12 KB
/
GraphiteClient.go
File metadata and controls
55 lines (42 loc) · 1.12 KB
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
// GraphiteQuery.go
package graphitenotifier
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type graphiteQueryResult struct {
Items []struct {
Target string `json:"target"`
Datapoints [][]float64 `json:"datapoints"`
}
}
type graphiteConf struct {
Host string `json:"host"`
Username string `json:"username"`
Password string `json:"password"`
}
type graphiteClient struct {
conf *graphiteConf
httpClient *http.Client
}
func newGraphiteClient(conf *graphiteConf) *graphiteClient {
return &graphiteClient{
conf: conf,
httpClient: &http.Client{},
}
}
func (gc *graphiteClient) Query(query string) (*graphiteQueryResult, error) {
var result graphiteQueryResult
var err error
req, _ := http.NewRequest("GET", gc.conf.Host+"/render?target="+query+"&format=json", nil)
req.SetBasicAuth(gc.conf.Username, gc.conf.Password)
log.Println("Perfoming Query : ", req.URL)
resp, err := gc.httpClient.Do(req)
if err == nil {
bytes, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(bytes, &result.Items)
}
return &result, err
}