forked from go-graphite/carbonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipper.go
104 lines (77 loc) · 1.98 KB
/
zipper.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"github.com/dgryski/carbonapi/expr"
pb "github.com/dgryski/carbonzipper/carbonzipperpb"
)
var errNoMetrics = errors.New("no metrics")
type unmarshaler interface {
Unmarshal([]byte) error
}
type zipper struct {
z string
client *http.Client
}
func (z zipper) Find(metric string) (pb.GlobResponse, error) {
u, _ := url.Parse(z.z + "/metrics/find/")
u.RawQuery = url.Values{
"query": []string{metric},
"format": []string{"protobuf"},
}.Encode()
var pbresp pb.GlobResponse
err := z.get("Find", u, &pbresp)
return pbresp, err
}
func (z zipper) get(who string, u *url.URL, msg unmarshaler) error {
resp, err := z.client.Get(u.String())
if err != nil {
return fmt.Errorf("http.Get: %+v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("ioutil.ReadAll: %+v", err)
}
err = msg.Unmarshal(body)
if err != nil {
return fmt.Errorf("proto.Unmarshal: %+v", err)
}
return nil
}
func (z zipper) Passthrough(metric string) ([]byte, error) {
u, _ := url.Parse(z.z + metric)
resp, err := z.client.Get(u.String())
if err != nil {
return nil, fmt.Errorf("http.Get: %+v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadAll: %+v", err)
}
return body, nil
}
func (z zipper) Render(metric string, from, until int32) (expr.MetricData, error) {
u, _ := url.Parse(z.z + "/render/")
u.RawQuery = url.Values{
"target": []string{metric},
"format": []string{"protobuf"},
"from": []string{strconv.Itoa(int(from))},
"until": []string{strconv.Itoa(int(until))},
}.Encode()
var pbresp pb.MultiFetchResponse
err := z.get("Render", u, &pbresp)
if err != nil {
return expr.MetricData{}, err
}
if m := pbresp.Metrics; len(m) == 0 {
return expr.MetricData{}, errNoMetrics
}
mdata := expr.MetricData{FetchResponse: *pbresp.Metrics[0]}
return mdata, nil
}