/
metrics.go
203 lines (173 loc) · 6.55 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package handlers
import (
"context"
"encoding/json"
"net/url"
"strings"
"time"
"github.com/guptarohit/asciigraph"
"github.com/lawrencegripper/azbrowse/internal/pkg/style"
"github.com/lawrencegripper/azbrowse/pkg/armclient"
)
// HACK: To draw the graph this handler has to know how big the display area is...
// to work around this the ItemWidget sets these properties when it's created
// I don't like it but it works. Will hopefully replace with better mechanism in future
//
// ItemWidgetHeight tracks height of item widget
var ItemWidgetHeight int
// ItemWidgetWidth track width of item widget
var ItemWidgetWidth int
// MetricsExpander expands the data-plane aspects of the Microsoft.Insights RP
type MetricsExpander struct {
}
// Name returns the name of the expander
func (e *MetricsExpander) Name() string {
return "MetricsExpander"
}
// DoesExpand checks if this is a storage account
func (e *MetricsExpander) DoesExpand(ctx context.Context, currentItem *TreeNode) (bool, error) {
if currentItem.ItemType == ResourceType || strings.HasPrefix(currentItem.ItemType, "metrics.") {
return true, nil
}
return false, nil
}
// Expand adds items for metrics to the list
func (e *MetricsExpander) Expand(ctx context.Context, currentItem *TreeNode) ExpanderResult {
// We have a metric namespace lets lookup the metric definitions
if currentItem.ItemType == "metrics.metricdefinition" {
return expandMetricDefinition(ctx, currentItem)
}
// We have a metric definition lets draw the graph
if currentItem.ItemType == "metrics.graph" {
return expandGraph(ctx, currentItem)
}
// We're looking at a top level resource, lets see if it has a metric namespace
return expandMetricNamespace(ctx, currentItem)
}
func expandMetricNamespace(ctx context.Context, currentItem *TreeNode) ExpanderResult {
data, err := armclient.DoRequest(ctx, "GET", currentItem.ID+
"/providers/microsoft.insights/metricNamespaces?api-version=2017-12-01-preview")
if err != nil {
return ExpanderResult{
Err: err,
SourceDescription: "MetricsExpander request metricNamespaces",
}
}
newItems := []*TreeNode{}
var metricNamespaceResponse armclient.MetricNamespaceResponse
err = json.Unmarshal([]byte(data), &metricNamespaceResponse)
if err != nil {
return ExpanderResult{
Err: err,
SourceDescription: "MetricsExpander metricNamespace failed to deserialise",
}
}
for _, metricNamespace := range metricNamespaceResponse.Value {
newItems = append(newItems, &TreeNode{
Name: metricNamespace.Name,
Display: style.Subtle("[Metrics]") + "\n " + metricNamespace.Name,
ID: currentItem.ID + "/providers/microsoft.insights/metricdefinitions",
Parentid: currentItem.ID,
ExpandURL: currentItem.ID + "/providers/microsoft.insights/metricdefinitions?" +
"metricNamespace=" + url.QueryEscape(metricNamespace.Properties.MetricNamespaceName) +
"&api-version=2018-01-01",
ItemType: "metrics.metricdefinition",
SubscriptionID: currentItem.SubscriptionID,
Metadata: map[string]string{
"SuppressSwaggerExpand": "true",
"SuppressGenericExpand": "true",
"ResourceID": currentItem.ID,
},
})
}
return ExpanderResult{
IsPrimaryResponse: false,
Nodes: newItems,
SourceDescription: "MetricsExpander build response metric namespaces",
}
}
func expandMetricDefinition(ctx context.Context, currentItem *TreeNode) ExpanderResult {
data, err := armclient.DoRequest(ctx, "GET", currentItem.ExpandURL)
if err != nil {
return ExpanderResult{
Err: err,
SourceDescription: "MetricsExpander request metricDefinitions",
}
}
var metricsListResponse armclient.MetricsListResponse
err = json.Unmarshal([]byte(data), &metricsListResponse)
if err != nil {
return ExpanderResult{
Err: err,
SourceDescription: "MetricsExpander metricDefinitions failed to deserialise",
}
}
newItems := []*TreeNode{}
for _, metric := range metricsListResponse.Value {
newItems = append(newItems, &TreeNode{
Name: metric.Name.Value,
Display: metric.Name.Value + "\n " + style.Subtle("Unit: "+metric.Unit),
ID: currentItem.Metadata["ResourceID"] + "/providers/microsoft.Insights/metrics",
Parentid: currentItem.ID,
ExpandURL: currentItem.Metadata["ResourceID"] + "/providers/microsoft.Insights/metrics?timespan=" +
time.Now().UTC().Add(-4*time.Hour).Format("2006-01-02T15:04:05.000Z") + "/" +
time.Now().UTC().Format("2006-01-02T15:04:05.000Z") + "&interval=PT1M&metricnames=" +
url.QueryEscape(metric.Name.Value) + "&aggregation=" +
url.QueryEscape(metric.PrimaryAggregationType) +
"&metricNamespace=" + url.QueryEscape(metric.Namespace) +
"&autoadjusttimegrain=true&validatedimensions=false&api-version=2018-01-01",
ItemType: "metrics.graph",
SubscriptionID: currentItem.SubscriptionID,
Metadata: map[string]string{
"SuppressSwaggerExpand": "true",
"SuppressGenericExpand": "true",
"AggregationType": strings.ToLower(metric.PrimaryAggregationType),
"Units": strings.ToLower(metric.Unit),
},
})
}
return ExpanderResult{
Response: data,
IsPrimaryResponse: true,
Nodes: newItems,
SourceDescription: "MetricsExpander build response metric namespaces",
}
}
func expandGraph(ctx context.Context, currentItem *TreeNode) ExpanderResult {
data, err := armclient.DoRequest(ctx, "GET", currentItem.ExpandURL)
if err != nil {
return ExpanderResult{
Err: err,
SourceDescription: "MetricsExpander request metricDefinitions",
}
}
var metricResponse armclient.MetricResponse
err = json.Unmarshal([]byte(data), &metricResponse)
if err != nil {
return ExpanderResult{
Err: err,
SourceDescription: "MetricsExpander graphdata failed to deserialise",
}
}
caption := style.Title(currentItem.Name) +
style.Subtle(" (Aggregate: '"+currentItem.Metadata["AggregationType"]+"' Unit: '"+
currentItem.Metadata["Units"]+"')")
graphData := []float64{}
for _, datapoint := range metricResponse.Value[0].Timeseries[0].Data {
value, success := datapoint[currentItem.Metadata["AggregationType"]].(float64)
if success {
graphData = append(graphData, value)
} else {
graphData = append(graphData, float64(0))
}
}
graph := asciigraph.Plot(graphData,
asciigraph.Height(ItemWidgetHeight-6),
asciigraph.Width(ItemWidgetWidth-15),
asciigraph.Caption("time: 4hrs ago ----> now"))
return ExpanderResult{
Response: "\n\n" + caption + "\n\n" + style.Graph(graph),
IsPrimaryResponse: true,
SourceDescription: "MetricsExpander build graph",
}
}