/
metrics.go
120 lines (107 loc) · 2.64 KB
/
metrics.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
package apis
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/ghchinoy/rwctl/control"
)
const (
// GetMetricsFormat is the golang fmt format string for http://docs.akana.com/cm/api/apis/m_apis_getMetrics.htm
GetMetricsFormat = "/api/apis/versions/%s/metrics"
)
// MetricsResponse is the json object that holds metrics
type MetricsResponse struct {
StartTime string
EndTime string
Interval []MetricCollection `json:"Interval"`
}
// MetricCollection in the MetricsResponse
type MetricCollection struct {
StartTime string
Metrics []MetricNameValue `json:"Metric"`
}
// MetricNameValueCollection is a collection of MetricNameValue pairs
type MetricNameValueCollection struct {
Metric []MetricNameValue
}
// MetricNameValue is a name:value pair
type MetricNameValue struct {
Name string
Value int
}
// Metric is a convenience struct
type Metric struct {
AvgResponseTime int
MinResponseTime int
MaxResponseTime int
TotalCount int
SuccessCount int
FaultCount int
}
// APIMetrics lists metrics of an API
func APIMetrics(apiID string, config control.Configuration, debug bool) error {
var metrics MetricsResponse
if debug {
log.Println("Getting metrics for", apiID)
}
endpoint := fmt.Sprintf(GetMetricsFormat, apiID)
url := fmt.Sprintf("%s%s", config.URL, endpoint)
client, _, err := control.LoginToCM(config, debug)
if err != nil {
log.Fatalln(err)
return err
}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if debug {
log.Printf("%s", bodyBytes)
}
err = json.Unmarshal(bodyBytes, &metrics)
if err != nil {
return err
}
fmt.Println("Metrics for API ", apiID)
format := "%-20s %-5v %-5v %-5v %-5v %-5v %-5v\n"
fmt.Printf(format, "start", "avg", "min", "max", "tot", "succ", "fault")
for _, v := range metrics.Interval {
m := mapMetrics(v.Metrics)
fmt.Printf(format,
v.StartTime,
m.AvgResponseTime,
m.MinResponseTime,
m.MaxResponseTime,
m.TotalCount,
m.SuccessCount,
m.FaultCount)
}
return nil
}
// mapMetrics turns a metric name/value pair into a Metric object
func mapMetrics(mc []MetricNameValue) Metric {
var m Metric
for _, v := range mc {
switch v.Name {
case "avgResponseTime":
m.AvgResponseTime = v.Value
case "minResponseTime":
m.MinResponseTime = v.Value
case "maxResponseTime":
m.MaxResponseTime = v.Value
case "totalCount":
m.TotalCount = v.Value
case "successCount":
m.SuccessCount = v.Value
case "faultCount":
m.FaultCount = v.Value
}
}
return m
}