Skip to content

Commit

Permalink
Fix wrong quota values
Browse files Browse the repository at this point in the history
fixes #83

Azure quota names are not unique, so we use localized name as label everywhere

Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Feb 9, 2025
1 parent 21f5906 commit 53ce2ff
Showing 1 changed file with 21 additions and 77 deletions.
98 changes: 21 additions & 77 deletions metrics_azurerm_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (m *MetricsCollectorAzureRmQuota) Setup(collector *collector.Collector) {
"provider",
"scope",
"quota",
"quotaName",
},
)

Expand All @@ -74,6 +75,7 @@ func (m *MetricsCollectorAzureRmQuota) Setup(collector *collector.Collector) {
"provider",
"scope",
"quota",
"quotaName",
},
)

Expand All @@ -88,6 +90,7 @@ func (m *MetricsCollectorAzureRmQuota) Setup(collector *collector.Collector) {
"provider",
"scope",
"quota",
"quotaName",
},
)

Expand Down Expand Up @@ -190,29 +193,19 @@ func (m *MetricsCollectorAzureRmQuota) collectAuthorizationUsage(subscription *a
}{}

if err := runtime.UnmarshalAsJSON(resp, &result); err == nil {
quotaName := "RoleAssignments"
quotaNameLocalized := "Role Assignments"
currentValue := result.RoleAssignmentsCurrentCount
limitValue := result.RoleAssignmentsLimit

infoLabels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": "",
"provider": "microsoft.authorization",
"scope": "authorization",
"quota": quotaName,
"quotaName": quotaNameLocalized,
}

labels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": "",
"provider": "microsoft.authorization",
"scope": "authorization",
"quota": quotaName,
"quota": "RoleAssignments",
"quotaName": "Role Assignments",
}

quotaMetric.Add(infoLabels, 1)
quotaMetric.Add(labels, 1)
quotaCurrentMetric.Add(labels, currentValue)
quotaLimitMetric.Add(labels, limitValue)
if limitValue != 0 {
Expand Down Expand Up @@ -252,27 +245,18 @@ func (m *MetricsCollectorAzureRmQuota) collectAuthorizationUsage(subscription *a
// }
//
// for _, resourceUsage := range result.Value {
// quotaName := to.String(resourceUsage.Properties.Name.Value)
// quotaNameLocalized := to.String(resourceUsage.Properties.Name.LocalizedValue)
// currentValue := float64(to.Number(resourceUsage.Properties.Usages.Value))
// limitValue := float64(to.Number(resourceUsage.Properties.Usages.Limit))
//
// infoLabels := prometheus.Labels{
// "subscriptionID": to.StringLower(subscription.SubscriptionID),
// "location": strings.ToLower(location),
// "scope": provider,
// "quota": quotaName,
// "quotaName": quotaNameLocalized,
// }
//
// labels := prometheus.Labels{
// "subscriptionID": to.StringLower(subscription.SubscriptionID),
// "location": strings.ToLower(location),
// "scope": provider,
// "quota": quotaName,
// "quota": to.String(resourceUsage.Properties.Name.Value),
// "quotaName": to.String(resourceUsage.Properties.Name.LocalizedValue),
// }
//
// quotaMetric.Add(infoLabels, 1)
// quotaMetric.Add(labels, 1)
// quotaCurrentMetric.Add(labels, currentValue)
// quotaLimitMetric.Add(labels, limitValue)
// if limitValue != 0 {
Expand Down Expand Up @@ -309,29 +293,19 @@ func (m *MetricsCollectorAzureRmQuota) collectAzureComputeUsage(subscription *ar
}

for _, resourceUsage := range result.Value {
quotaName := to.String(resourceUsage.Name.Value)
quotaNameLocalized := to.String(resourceUsage.Name.LocalizedValue)
currentValue := float64(to.Number(resourceUsage.CurrentValue))
limitValue := float64(to.Number(resourceUsage.Limit))

infoLabels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": strings.ToLower(location),
"provider": "microsoft.compute",
"scope": "compute",
"quota": quotaName,
"quotaName": quotaNameLocalized,
}

labels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": strings.ToLower(location),
"provider": "microsoft.compute",
"scope": "compute",
"quota": quotaName,
"quota": to.String(resourceUsage.Name.Value),
"quotaName": to.String(resourceUsage.Name.LocalizedValue),
}

quotaMetric.Add(infoLabels, 1)
quotaMetric.Add(labels, 1)
quotaCurrentMetric.Add(labels, currentValue)
quotaLimitMetric.Add(labels, limitValue)
if limitValue != 0 {
Expand Down Expand Up @@ -368,29 +342,19 @@ func (m *MetricsCollectorAzureRmQuota) collectAzureNetworkUsage(subscription *ar
}

for _, resourceUsage := range result.Value {
quotaName := to.String(resourceUsage.Name.Value)
quotaNameLocalized := to.String(resourceUsage.Name.LocalizedValue)
currentValue := float64(to.Number(resourceUsage.CurrentValue))
limitValue := float64(to.Number(resourceUsage.Limit))

infoLabels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": strings.ToLower(location),
"provider": "microsoft.network",
"scope": "network",
"quota": quotaName,
"quotaName": quotaNameLocalized,
}

labels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": strings.ToLower(location),
"provider": "microsoft.network",
"scope": "network",
"quota": quotaName,
"quota": to.String(resourceUsage.Name.Value),
"quotaName": to.String(resourceUsage.Name.LocalizedValue),
}

quotaMetric.Add(infoLabels, 1)
quotaMetric.Add(labels, 1)
quotaCurrentMetric.Add(labels, currentValue)
quotaLimitMetric.Add(labels, limitValue)
if limitValue != 0 {
Expand Down Expand Up @@ -427,29 +391,19 @@ func (m *MetricsCollectorAzureRmQuota) collectAzureStorageUsage(subscription *ar
}

for _, resourceUsage := range result.Value {
quotaName := to.String(resourceUsage.Name.Value)
quotaNameLocalized := to.String(resourceUsage.Name.LocalizedValue)
currentValue := float64(to.Number(resourceUsage.CurrentValue))
limitValue := float64(to.Number(resourceUsage.Limit))

infoLabels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": strings.ToLower(location),
"provider": "microsoft.storage",
"scope": "storage",
"quota": quotaName,
"quotaName": quotaNameLocalized,
}

labels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": strings.ToLower(location),
"provider": "microsoft.storage",
"scope": "storage",
"quota": quotaName,
"quota": to.String(resourceUsage.Name.Value),
"quotaName": to.String(resourceUsage.Name.LocalizedValue),
}

quotaMetric.Add(infoLabels, 1)
quotaMetric.Add(labels, 1)
quotaCurrentMetric.Add(labels, currentValue)
quotaLimitMetric.Add(labels, limitValue)
if limitValue != 0 {
Expand Down Expand Up @@ -486,29 +440,19 @@ func (m *MetricsCollectorAzureRmQuota) collectAzureMachineLearningUsage(subscrip
}

for _, resourceUsage := range result.Value {
quotaName := to.String(resourceUsage.Name.Value)
quotaNameLocalized := to.String(resourceUsage.Name.LocalizedValue)
currentValue := float64(to.Number(resourceUsage.CurrentValue))
limitValue := float64(to.Number(resourceUsage.Limit))

infoLabels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": strings.ToLower(location),
"provider": "microsoft.machinelearningservices",
"scope": "machinelearningservices",
"quota": quotaName,
"quotaName": quotaNameLocalized,
}

labels := prometheus.Labels{
"subscriptionID": to.StringLower(subscription.SubscriptionID),
"location": strings.ToLower(location),
"provider": "microsoft.machinelearningservices",
"scope": "machinelearningservices",
"quota": quotaName,
"quota": to.String(resourceUsage.Name.Value),
"quotaName": to.String(resourceUsage.Name.LocalizedValue),
}

quotaMetric.Add(infoLabels, 1)
quotaMetric.Add(labels, 1)
quotaCurrentMetric.Add(labels, currentValue)
quotaLimitMetric.Add(labels, limitValue)
if limitValue != 0 {
Expand Down

0 comments on commit 53ce2ff

Please sign in to comment.