-
Notifications
You must be signed in to change notification settings - Fork 5
/
project.go
67 lines (54 loc) · 1.74 KB
/
project.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
package main
import (
"encoding/json"
"log"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cms"
)
// datapoint represents the member of Datapoints field from QueryMetricLastResponse
type datapoint struct {
Average float64 `json:"Average"`
Maximum float64 `json:"Maximum"`
Minimum float64 `json:"Minimum"`
Value float64 `json:"Value"`
InstanceId string `json:"instanceId"`
Timestamp int64 `json:"timestamp"`
UserId string `json:"userId"`
Port string `json:port`
Vip string `json:vip`
}
// GetResponseFunc returns a function to retrieve queryMetricLast
type GetResponseFunc func(client *cms.Client, request *cms.DescribeMetricLastRequest) (string, error)
// Project represents the dashborad from which metrics collected
type Project struct {
client *cms.Client
getResponse GetResponseFunc
Namespace string
}
func defaultGetResponseFunc(client *cms.Client, request *cms.DescribeMetricLastRequest) (string, error) {
response, err := client.DescribeMetricLast(request)
if err != nil {
return "", err
} else {
return response.Datapoints, nil
}
}
func retrieve(metric string, p Project) []datapoint {
request := cms.CreateDescribeMetricLastRequest()
request.Namespace = p.Namespace
request.MetricName = metric
requestsStats.Inc()
datapoints := make([]datapoint, 0)
getResponseFunc := p.getResponse
if getResponseFunc == nil {
getResponseFunc = defaultGetResponseFunc
}
source, err := getResponseFunc(p.client, request)
if err != nil {
responseError.Inc()
log.Println("Encounter response error from Aliyun:", err)
} else if err := json.Unmarshal([]byte(source), &datapoints); err != nil {
responseFormatError.Inc()
log.Println("Cannot decode json reponse:", err)
}
return datapoints
}