-
Notifications
You must be signed in to change notification settings - Fork 3
/
jsonsource.go
137 lines (122 loc) · 2.88 KB
/
jsonsource.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
package jsonsource
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"github.com/Symantec/scotty/metrics"
"github.com/Symantec/scotty/sources"
"github.com/Symantec/tricorder/go/tricorder/messages"
"github.com/Symantec/tricorder/go/tricorder/types"
"net/http"
"reflect"
"time"
)
type connectorType int
var (
kConnector = connectorType(0)
)
func (c connectorType) Connect(
host string, port uint, config sources.Config) (sources.Poller, error) {
// TODO: Use TLS.
if config.IsTls {
url := fmt.Sprintf("https://%s:%d/metricsapi", host, port)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
response, err := client.Get(url)
if err != nil {
return nil, err
}
return &pollerType{response: response}, nil
} else {
url := fmt.Sprintf("http://%s:%d/metricsapi", host, port)
var client http.Client
response, err := client.Get(url)
if err != nil {
return nil, err
}
return &pollerType{response: response}, nil
}
}
func (c connectorType) Name() string {
return "json"
}
type pollerType struct {
response *http.Response
}
type genericMetric struct {
Value json.RawMessage `json:"value"`
messages.Metric
}
func (g *genericMetric) decodeValueAs(prototype interface{}) error {
valuePtr := reflect.New(reflect.TypeOf(prototype))
if err := json.Unmarshal(g.Value, valuePtr.Interface()); err != nil {
return err
}
g.Metric.Value = valuePtr.Elem().Interface()
return nil
}
func (g *genericMetric) decodeValue() error {
switch g.Kind {
case types.List:
nilSlice, err := g.SubType.SafeNilSlice()
if err != nil {
return err
}
return g.decodeValueAs(nilSlice)
default:
zeroVal, err := messages.ZeroValue(g.Kind)
if err != nil {
return err
}
return g.decodeValueAs(zeroVal)
}
}
func (g *genericMetric) ConvertToGoRPC() (err error) {
if err = g.decodeValue(); err != nil {
return err
}
if err = g.Metric.ConvertToGoRPC(); err != nil {
g.Metric.Value = nil
} else {
g.Value = nil
}
return
}
func (p *pollerType) Poll() (result metrics.List, err error) {
if p.response.StatusCode != 200 {
return nil, errors.New(p.response.Status)
}
decoder := json.NewDecoder(p.response.Body)
var values genericMetricList
if err = decoder.Decode(&values); err != nil {
return
}
for _, v := range values {
if err = v.ConvertToGoRPC(); err != nil {
return
}
}
result = values
return
}
func (p *pollerType) Close() error {
return p.response.Body.Close()
}
type genericMetricList []*genericMetric
func (l genericMetricList) Len() int {
return len(l)
}
func (l genericMetricList) Index(i int, result *metrics.Value) {
result.Path = l[i].Path
result.Description = l[i].Description
result.Unit = l[i].Unit
result.Value = l[i].Metric.Value
if l[i].TimeStamp == nil {
result.TimeStamp = time.Time{}
} else {
result.TimeStamp = l[i].TimeStamp.(time.Time)
}
result.GroupId = l[i].GroupId
}