This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
azure.go
198 lines (175 loc) · 5.25 KB
/
azure.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
package main
import (
"context"
"fmt"
"reflect"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2018-09-01/insights"
"github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2018-09-01/insights/insightsapi"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/Azure/go-autorest/autorest/to"
)
// FetchMetricDataInput is input parameters for FetchMetricData
type FetchMetricDataInput struct {
subscriptionID string
resourceGroup string
namespace string
resource string
metricNames []string
aggregation string
startTime time.Time
endTime time.Time
interval int
filter string
}
// FetchMetricDefinitionsInput is input parameters for FetchMetricDefinitions
type FetchMetricDefinitionsInput struct {
subscriptionID string
resourceGroup string
namespace string
resource string
resourceURI string
metricnamespace string
}
// Client is an API Client for Azure
type Client struct {
MetricsClient insightsapi.MetricsClientAPI
MetricDefinitionsClient insightsapi.MetricDefinitionsClientAPI
}
// NewClient returns *Client with setting Authorizer
func NewClient(subscriptionID string) (*Client, error) {
a, err := auth.NewAuthorizerFromFile(azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
return &Client{}, err
}
metricsClient := insights.NewMetricsClient(subscriptionID)
metricsClient.Authorizer = a
metricDefinitionsClient := insights.NewMetricDefinitionsClient(subscriptionID)
metricDefinitionsClient.Authorizer = a
return &Client{
MetricsClient: metricsClient,
MetricDefinitionsClient: metricDefinitionsClient,
}, nil
}
type metricDefinitionsListInput struct {
subscriptionID string
resourceURI string
metricnamespace string
}
type metricsListInput struct {
subscriptionID string
resourceURI string
timespan string
interval *string
metricnames string
aggregation string
top *int32
orderby string
filter string
resultType insights.ResultType
metricnamespace string
}
func (c *Client) metricDefinitionsList(ctx context.Context, params *metricDefinitionsListInput) (insights.MetricDefinitionCollection, error) {
return c.MetricDefinitionsClient.List(
ctx,
params.resourceURI,
params.metricnamespace,
)
}
func (c *Client) metricsList(ctx context.Context, params *metricsListInput) (insights.Response, error) {
return c.MetricsClient.List(
ctx,
params.resourceURI,
params.timespan,
params.interval,
params.metricnames,
params.aggregation,
params.top,
params.orderby,
params.filter,
params.resultType,
params.metricnamespace,
)
}
// FetchMetricDefinitions returns metric definitions
func FetchMetricDefinitions(ctx context.Context, c *Client, params FetchMetricDefinitionsInput) (*[]insights.MetricDefinition, error) {
input := &metricDefinitionsListInput{
subscriptionID: params.subscriptionID,
resourceURI: fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/%s/%s",
params.subscriptionID,
params.resourceGroup,
params.namespace,
params.resource,
),
metricnamespace: params.metricnamespace,
}
res, err := c.metricDefinitionsList(ctx, input)
if err != nil {
return nil, err
}
return res.Value, nil
}
// FetchMetricData fetches metric data and returns latest value with metric name as hash key
func FetchMetricData(ctx context.Context, c *Client, params FetchMetricDataInput) (map[string]*insights.MetricValue, error) {
timespan := fmt.Sprintf("%s/%s", params.startTime.Format(time.RFC3339), params.endTime.Format(time.RFC3339))
var metricNames []string
const metricsCountLimitPerRequest int = 20
for {
if len(params.metricNames) <= metricsCountLimitPerRequest {
metricNames = append(metricNames, strings.Join(params.metricNames, ","))
break
}
metricNames = append(metricNames, strings.Join(params.metricNames[:metricsCountLimitPerRequest], ","))
params.metricNames = params.metricNames[metricsCountLimitPerRequest:]
}
metrics := make(map[string]*insights.MetricValue)
for _, m := range metricNames {
input := &metricsListInput{
subscriptionID: params.subscriptionID,
resourceURI: fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/%s/%s",
params.subscriptionID,
params.resourceGroup,
params.namespace,
params.resource,
),
timespan: timespan,
interval: to.StringPtr(fmt.Sprintf("PT%dS", params.interval)),
aggregation: params.aggregation,
metricnames: m,
resultType: insights.Data,
filter: params.filter,
}
res, err := c.metricsList(ctx, input)
if err != nil {
return nil, err
}
for _, v := range *res.Value {
for _, elem := range *v.Timeseries {
var latestData insights.MetricValue
for _, d := range *elem.Data {
rv := reflect.ValueOf(d)
av := rv.FieldByName(params.aggregation)
if av.IsNil() {
continue
}
if d.TimeStamp == nil {
continue
}
if latestData.TimeStamp == nil {
latestData = d
continue
}
if d.TimeStamp.After(latestData.TimeStamp.Time) {
latestData = d
}
}
if latestData.TimeStamp != nil {
metrics[*v.Name.Value] = &latestData
}
}
}
}
return metrics, nil
}