|
7 | 7 | "time"
|
8 | 8 |
|
9 | 9 | "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
|
| 10 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions" |
10 | 11 | "github.com/Azure/go-autorest/autorest/azure"
|
11 | 12 | "github.com/patrickmn/go-cache"
|
12 | 13 | "github.com/prometheus/client_golang/prometheus"
|
@@ -176,77 +177,78 @@ func (p *MetricProber) RunOnSubscriptionScope() {
|
176 | 177 | func (p *MetricProber) collectMetricsFromSubscriptions() {
|
177 | 178 | metricsChannel := make(chan PrometheusMetricResult)
|
178 | 179 |
|
179 |
| - wgSubscription := sizedwaitgroup.New(p.Conf.Prober.ConcurrencySubscription) |
| 180 | + subscriptionIterator := armclient.NewSubscriptionIterator(p.AzureClient, p.settings.Subscriptions...) |
| 181 | + subscriptionIterator.SetConcurrency(p.Conf.Prober.ConcurrencySubscription) |
180 | 182 |
|
181 | 183 | go func() {
|
182 |
| - for _, subscriptionId := range p.settings.Subscriptions { |
| 184 | + |
| 185 | + err := subscriptionIterator.ForEachAsync(p.logger, func(subscription *armsubscriptions.Subscription, logger *zap.SugaredLogger) { |
183 | 186 | for _, region := range p.settings.Regions {
|
184 |
| - wgSubscription.Add() |
185 |
| - go func(subscriptionId, region string) { |
186 |
| - defer wgSubscription.Done() |
| 187 | + client, err := p.MetricsClient(*subscription.SubscriptionID) |
| 188 | + if err != nil { |
| 189 | + // FIXME: find a better way to report errors |
| 190 | + p.logger.Error(err) |
| 191 | + return |
| 192 | + } |
187 | 193 |
|
188 |
| - client, err := p.MetricsClient(subscriptionId) |
189 |
| - if err != nil { |
190 |
| - // FIXME: find a better way to report errors |
191 |
| - p.logger.Error(err) |
192 |
| - return |
| 194 | + // request metrics in 20 metrics chunks (azure metric api limitation) |
| 195 | + for i := 0; i < len(p.settings.Metrics); i += AzureMetricApiMaxMetricNumber { |
| 196 | + end := i + AzureMetricApiMaxMetricNumber |
| 197 | + if end > len(p.settings.Metrics) { |
| 198 | + end = len(p.settings.Metrics) |
| 199 | + } |
| 200 | + metricList := p.settings.Metrics[i:end] |
| 201 | + |
| 202 | + resultType := armmonitor.MetricResultTypeData |
| 203 | + opts := armmonitor.MetricsClientListAtSubscriptionScopeOptions{ |
| 204 | + Interval: p.settings.Interval, |
| 205 | + Timespan: to.StringPtr(p.settings.Timespan), |
| 206 | + Metricnames: to.StringPtr(strings.Join(metricList, ",")), |
| 207 | + Metricnamespace: to.StringPtr(p.settings.ResourceType), |
| 208 | + Top: p.settings.MetricTop, |
| 209 | + AutoAdjustTimegrain: to.BoolPtr(true), |
| 210 | + ResultType: &resultType, |
| 211 | + Filter: to.StringPtr(`Microsoft.ResourceId eq '*'`), |
193 | 212 | }
|
194 | 213 |
|
195 |
| - // request metrics in 20 metrics chunks (azure metric api limitation) |
196 |
| - for i := 0; i < len(p.settings.Metrics); i += AzureMetricApiMaxMetricNumber { |
197 |
| - end := i + AzureMetricApiMaxMetricNumber |
198 |
| - if end > len(p.settings.Metrics) { |
199 |
| - end = len(p.settings.Metrics) |
200 |
| - } |
201 |
| - metricList := p.settings.Metrics[i:end] |
202 |
| - |
203 |
| - resultType := armmonitor.MetricResultTypeData |
204 |
| - opts := armmonitor.MetricsClientListAtSubscriptionScopeOptions{ |
205 |
| - Interval: p.settings.Interval, |
206 |
| - Timespan: to.StringPtr(p.settings.Timespan), |
207 |
| - Metricnames: to.StringPtr(strings.Join(metricList, ",")), |
208 |
| - Metricnamespace: to.StringPtr(p.settings.ResourceType), |
209 |
| - Top: p.settings.MetricTop, |
210 |
| - AutoAdjustTimegrain: to.BoolPtr(true), |
211 |
| - ResultType: &resultType, |
212 |
| - Filter: to.StringPtr(`Microsoft.ResourceId eq '*'`), |
213 |
| - } |
214 |
| - |
215 |
| - if len(p.settings.Aggregations) >= 1 { |
216 |
| - opts.Aggregation = to.StringPtr(strings.Join(p.settings.Aggregations, ",")) |
217 |
| - } |
218 |
| - |
219 |
| - if len(p.settings.MetricFilter) >= 1 { |
220 |
| - opts.Filter = to.StringPtr(*opts.Filter + " and " + p.settings.MetricFilter) |
221 |
| - } |
222 |
| - |
223 |
| - if len(p.settings.MetricOrderBy) >= 1 { |
224 |
| - opts.Orderby = to.StringPtr(p.settings.MetricOrderBy) |
225 |
| - } |
| 214 | + if len(p.settings.Aggregations) >= 1 { |
| 215 | + opts.Aggregation = to.StringPtr(strings.Join(p.settings.Aggregations, ",")) |
| 216 | + } |
226 | 217 |
|
227 |
| - response, err := client.ListAtSubscriptionScope(p.ctx, region, &opts) |
228 |
| - if err != nil { |
229 |
| - // FIXME: find a better way to report errors |
230 |
| - p.logger.Error(err) |
231 |
| - return |
232 |
| - } |
| 218 | + if len(p.settings.MetricFilter) >= 1 { |
| 219 | + opts.Filter = to.StringPtr(*opts.Filter + " and " + p.settings.MetricFilter) |
| 220 | + } |
233 | 221 |
|
234 |
| - result := AzureInsightSubscriptionMetricsResult{ |
235 |
| - AzureInsightBaseMetricsResult: AzureInsightBaseMetricsResult{ |
236 |
| - prober: p, |
237 |
| - }, |
238 |
| - subscriptionID: subscriptionId, |
239 |
| - Result: &response} |
240 |
| - result.SendMetricToChannel(metricsChannel) |
| 222 | + if len(p.settings.MetricOrderBy) >= 1 { |
| 223 | + opts.Orderby = to.StringPtr(p.settings.MetricOrderBy) |
241 | 224 | }
|
242 | 225 |
|
243 |
| - if p.callbackSubscriptionFishish != nil { |
244 |
| - p.callbackSubscriptionFishish(subscriptionId) |
| 226 | + response, err := client.ListAtSubscriptionScope(p.ctx, region, &opts) |
| 227 | + if err != nil { |
| 228 | + // FIXME: find a better way to report errors |
| 229 | + p.logger.Error(err) |
| 230 | + return |
245 | 231 | }
|
246 |
| - }(subscriptionId, region) |
| 232 | + |
| 233 | + result := AzureInsightSubscriptionMetricsResult{ |
| 234 | + AzureInsightBaseMetricsResult: AzureInsightBaseMetricsResult{ |
| 235 | + prober: p, |
| 236 | + }, |
| 237 | + subscription: subscription, |
| 238 | + Result: &response} |
| 239 | + result.SendMetricToChannel(metricsChannel) |
| 240 | + } |
| 241 | + |
| 242 | + if p.callbackSubscriptionFishish != nil { |
| 243 | + p.callbackSubscriptionFishish(*subscription.SubscriptionID) |
| 244 | + } |
247 | 245 | }
|
| 246 | + }) |
| 247 | + if err != nil { |
| 248 | + // FIXME: find a better way to report errors |
| 249 | + p.logger.Error(err) |
248 | 250 | }
|
249 |
| - wgSubscription.Wait() |
| 251 | + |
250 | 252 | close(metricsChannel)
|
251 | 253 | }()
|
252 | 254 |
|
|
0 commit comments