Skip to content

Commit

Permalink
add subscriptionName as label
Browse files Browse the repository at this point in the history
fixes #26

Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Jul 2, 2023
1 parent fe2da83 commit cc63a1b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 78 deletions.
24 changes: 13 additions & 11 deletions metrics/insights.subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
"github.com/prometheus/client_golang/prometheus"
"github.com/webdevops/go-common/azuresdk/armclient"
stringsCommon "github.com/webdevops/go-common/strings"
Expand All @@ -14,8 +15,8 @@ type (
AzureInsightSubscriptionMetricsResult struct {
AzureInsightBaseMetricsResult

subscriptionID string
Result *armmonitor.MetricsClientListAtSubscriptionScopeResponse
subscription *armsubscriptions.Subscription
Result *armmonitor.MetricsClientListAtSubscriptionScopeResponse
}
)

Expand Down Expand Up @@ -53,15 +54,16 @@ func (r *AzureInsightSubscriptionMetricsResult) SendMetricToChannel(channel chan
}

metricLabels := prometheus.Labels{
"resourceID": strings.ToLower(resourceId),
"subscriptionID": azureResource.Subscription,
"resourceGroup": azureResource.ResourceGroup,
"resourceName": azureResource.ResourceName,
"metric": to.String(metric.Name.Value),
"unit": metricUnit,
"interval": to.String(r.prober.settings.Interval),
"timespan": r.prober.settings.Timespan,
"aggregation": "",
"resourceID": strings.ToLower(resourceId),
"subscriptionID": azureResource.Subscription,
"subscriptionName": to.String(r.subscription.DisplayName),
"resourceGroup": azureResource.ResourceGroup,
"resourceName": azureResource.ResourceName,
"metric": to.String(metric.Name.Value),
"unit": metricUnit,
"interval": to.String(r.prober.settings.Interval),
"timespan": r.prober.settings.Timespan,
"aggregation": "",
}

// add resource tags as labels
Expand Down
24 changes: 15 additions & 9 deletions metrics/insights.target.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,22 @@ func (r *AzureInsightMetricsResult) SendMetricToChannel(channel chan<- Prometheu
metricUnit = string(*metric.Unit)
}

subscriptionName := ""
if subscription, err := r.prober.AzureClient.GetCachedSubscription(r.prober.ctx, azureResource.Subscription); err == nil && subscription != nil {
subscriptionName = to.String(subscription.DisplayName)
}

metricLabels := prometheus.Labels{
"resourceID": strings.ToLower(resourceId),
"subscriptionID": azureResource.Subscription,
"resourceGroup": azureResource.ResourceGroup,
"resourceName": azureResource.ResourceName,
"metric": to.String(metric.Name.Value),
"unit": metricUnit,
"interval": to.String(r.prober.settings.Interval),
"timespan": r.prober.settings.Timespan,
"aggregation": "",
"resourceID": strings.ToLower(resourceId),
"subscriptionID": azureResource.Subscription,
"subscriptionName": subscriptionName,
"resourceGroup": azureResource.ResourceGroup,
"resourceName": azureResource.ResourceName,
"metric": to.String(metric.Name.Value),
"unit": metricUnit,
"interval": to.String(r.prober.settings.Interval),
"timespan": r.prober.settings.Timespan,
"aggregation": "",
}

// add resource tags as labels
Expand Down
118 changes: 60 additions & 58 deletions metrics/prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -176,77 +177,78 @@ func (p *MetricProber) RunOnSubscriptionScope() {
func (p *MetricProber) collectMetricsFromSubscriptions() {
metricsChannel := make(chan PrometheusMetricResult)

wgSubscription := sizedwaitgroup.New(p.Conf.Prober.ConcurrencySubscription)
subscriptionIterator := armclient.NewSubscriptionIterator(p.AzureClient, p.settings.Subscriptions...)
subscriptionIterator.SetConcurrency(p.Conf.Prober.ConcurrencySubscription)

go func() {
for _, subscriptionId := range p.settings.Subscriptions {

err := subscriptionIterator.ForEachAsync(p.logger, func(subscription *armsubscriptions.Subscription, logger *zap.SugaredLogger) {
for _, region := range p.settings.Regions {
wgSubscription.Add()
go func(subscriptionId, region string) {
defer wgSubscription.Done()
client, err := p.MetricsClient(*subscription.SubscriptionID)
if err != nil {
// FIXME: find a better way to report errors
p.logger.Error(err)
return
}

client, err := p.MetricsClient(subscriptionId)
if err != nil {
// FIXME: find a better way to report errors
p.logger.Error(err)
return
// request metrics in 20 metrics chunks (azure metric api limitation)
for i := 0; i < len(p.settings.Metrics); i += AzureMetricApiMaxMetricNumber {
end := i + AzureMetricApiMaxMetricNumber
if end > len(p.settings.Metrics) {
end = len(p.settings.Metrics)
}
metricList := p.settings.Metrics[i:end]

resultType := armmonitor.MetricResultTypeData
opts := armmonitor.MetricsClientListAtSubscriptionScopeOptions{
Interval: p.settings.Interval,
Timespan: to.StringPtr(p.settings.Timespan),
Metricnames: to.StringPtr(strings.Join(metricList, ",")),
Metricnamespace: to.StringPtr(p.settings.ResourceType),
Top: p.settings.MetricTop,
AutoAdjustTimegrain: to.BoolPtr(true),
ResultType: &resultType,
Filter: to.StringPtr(`Microsoft.ResourceId eq '*'`),
}

// request metrics in 20 metrics chunks (azure metric api limitation)
for i := 0; i < len(p.settings.Metrics); i += AzureMetricApiMaxMetricNumber {
end := i + AzureMetricApiMaxMetricNumber
if end > len(p.settings.Metrics) {
end = len(p.settings.Metrics)
}
metricList := p.settings.Metrics[i:end]

resultType := armmonitor.MetricResultTypeData
opts := armmonitor.MetricsClientListAtSubscriptionScopeOptions{
Interval: p.settings.Interval,
Timespan: to.StringPtr(p.settings.Timespan),
Metricnames: to.StringPtr(strings.Join(metricList, ",")),
Metricnamespace: to.StringPtr(p.settings.ResourceType),
Top: p.settings.MetricTop,
AutoAdjustTimegrain: to.BoolPtr(true),
ResultType: &resultType,
Filter: to.StringPtr(`Microsoft.ResourceId eq '*'`),
}

if len(p.settings.Aggregations) >= 1 {
opts.Aggregation = to.StringPtr(strings.Join(p.settings.Aggregations, ","))
}

if len(p.settings.MetricFilter) >= 1 {
opts.Filter = to.StringPtr(*opts.Filter + " and " + p.settings.MetricFilter)
}

if len(p.settings.MetricOrderBy) >= 1 {
opts.Orderby = to.StringPtr(p.settings.MetricOrderBy)
}
if len(p.settings.Aggregations) >= 1 {
opts.Aggregation = to.StringPtr(strings.Join(p.settings.Aggregations, ","))
}

response, err := client.ListAtSubscriptionScope(p.ctx, region, &opts)
if err != nil {
// FIXME: find a better way to report errors
p.logger.Error(err)
return
}
if len(p.settings.MetricFilter) >= 1 {
opts.Filter = to.StringPtr(*opts.Filter + " and " + p.settings.MetricFilter)
}

result := AzureInsightSubscriptionMetricsResult{
AzureInsightBaseMetricsResult: AzureInsightBaseMetricsResult{
prober: p,
},
subscriptionID: subscriptionId,
Result: &response}
result.SendMetricToChannel(metricsChannel)
if len(p.settings.MetricOrderBy) >= 1 {
opts.Orderby = to.StringPtr(p.settings.MetricOrderBy)
}

if p.callbackSubscriptionFishish != nil {
p.callbackSubscriptionFishish(subscriptionId)
response, err := client.ListAtSubscriptionScope(p.ctx, region, &opts)
if err != nil {
// FIXME: find a better way to report errors
p.logger.Error(err)
return
}
}(subscriptionId, region)

result := AzureInsightSubscriptionMetricsResult{
AzureInsightBaseMetricsResult: AzureInsightBaseMetricsResult{
prober: p,
},
subscription: subscription,
Result: &response}
result.SendMetricToChannel(metricsChannel)
}

if p.callbackSubscriptionFishish != nil {
p.callbackSubscriptionFishish(*subscription.SubscriptionID)
}
}
})
if err != nil {
// FIXME: find a better way to report errors
p.logger.Error(err)
}
wgSubscription.Wait()

close(metricsChannel)
}()

Expand Down

0 comments on commit cc63a1b

Please sign in to comment.