Skip to content

Commit

Permalink
refactoring and cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Feb 24, 2024
1 parent f8a6c9e commit e534352
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ require (
github.com/robfig/cron v1.2.0 // indirect
github.com/std-uritemplate/std-uritemplate/go v0.0.53 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/webdevops/go-common v0.0.0-20240224160459-39041cd1f800
github.com/webdevops/go-common v0.0.0-20240224162410-e2481e4d7353
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/webdevops/go-common v0.0.0-20240224160459-39041cd1f800 h1:d+jUvoJZneQuJRGm52oXx/F0KuTV+eMcf2vRqtt/74Q=
github.com/webdevops/go-common v0.0.0-20240224160459-39041cd1f800/go.mod h1:Ut443ThSsOcrZdT5T62ZhuG4lJSdGrcp3FCY5UA8ozI=
github.com/webdevops/go-common v0.0.0-20240224162410-e2481e4d7353 h1:QlrUI5PU4mc8Hd6F9Iq+n0UpOvynWpJp8rEG3xQic6w=
github.com/webdevops/go-common v0.0.0-20240224162410-e2481e4d7353/go.mod h1:Ut443ThSsOcrZdT5T62ZhuG4lJSdGrcp3FCY5UA8ozI=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
51 changes: 18 additions & 33 deletions metrics_azurerm_reservation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/consumption/armconsumption"
Expand Down Expand Up @@ -43,6 +42,7 @@ func (m *MetricsCollectorAzureRmReservation) Setup(collector *collector.Collecto
"usageDate",
},
)
m.Collector.RegisterMetricList("reservationInfo", m.prometheus.reservationInfo, true)

m.prometheus.reservationUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -57,6 +57,7 @@ func (m *MetricsCollectorAzureRmReservation) Setup(collector *collector.Collecto
"usageDate",
},
)
m.Collector.RegisterMetricList("reservationUsage", m.prometheus.reservationUsage, true)

m.prometheus.reservationMinUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -71,6 +72,7 @@ func (m *MetricsCollectorAzureRmReservation) Setup(collector *collector.Collecto
"usageDate",
},
)
m.Collector.RegisterMetricList("reservationMinUsage", m.prometheus.reservationMinUsage, true)

m.prometheus.reservationMaxUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -85,6 +87,7 @@ func (m *MetricsCollectorAzureRmReservation) Setup(collector *collector.Collecto
"usageDate",
},
)
m.Collector.RegisterMetricList("reservationMaxUsage", m.prometheus.reservationMaxUsage, true)

m.prometheus.reservationUsedHours = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -99,6 +102,7 @@ func (m *MetricsCollectorAzureRmReservation) Setup(collector *collector.Collecto
"usageDate",
},
)
m.Collector.RegisterMetricList("reservationUsedHours", m.prometheus.reservationUsedHours, true)

m.prometheus.reservationReservedHours = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -113,6 +117,7 @@ func (m *MetricsCollectorAzureRmReservation) Setup(collector *collector.Collecto
"usageDate",
},
)
m.Collector.RegisterMetricList("reservationReservedHours", m.prometheus.reservationReservedHours, true)

m.prometheus.reservationTotalReservedQuantity = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -127,13 +132,6 @@ func (m *MetricsCollectorAzureRmReservation) Setup(collector *collector.Collecto
"usageDate",
},
)

m.Collector.RegisterMetricList("reservationInfo", m.prometheus.reservationInfo, true)
m.Collector.RegisterMetricList("reservationUsage", m.prometheus.reservationUsage, true)
m.Collector.RegisterMetricList("reservationMinUsage", m.prometheus.reservationMinUsage, true)
m.Collector.RegisterMetricList("reservationMaxUsage", m.prometheus.reservationMaxUsage, true)
m.Collector.RegisterMetricList("reservationUsedHours", m.prometheus.reservationUsedHours, true)
m.Collector.RegisterMetricList("reservationReservedHours", m.prometheus.reservationReservedHours, true)
m.Collector.RegisterMetricList("reservationTotalReservedQuantity", m.prometheus.reservationTotalReservedQuantity, true)
}

Expand All @@ -152,7 +150,6 @@ func (m *MetricsCollectorAzureRmReservation) collectReservationUsage(logger *zap
reservationReservedHours := m.Collector.GetMetricList("reservationReservedHours")
reservationTotalReservedQuantity := m.Collector.GetMetricList("reservationTotalReservedQuantity")

ctx := context.Background()
days := Config.Collectors.Reservation.FromDays
resourceScope := Config.Collectors.Reservation.ResourceScope
granularity := Config.Collectors.Reservation.Granularity
Expand All @@ -178,39 +175,27 @@ func (m *MetricsCollectorAzureRmReservation) collectReservationUsage(logger *zap

// Collect and export metrics
for pager.More() {
page, err := pager.NextPage(ctx)
page, err := pager.NextPage(m.Context())
if err != nil {
logger.Panic(err)
}

for _, reservationProperties := range page.Value {
reservationOrderID := reservationProperties.Properties.ReservationOrderID
reservationID := reservationProperties.Properties.ReservationID
skuName := reservationProperties.Properties.SKUName
kind := reservationProperties.Properties.Kind
reservedHours := reservationProperties.Properties.ReservedHours
usageDate := reservationProperties.Properties.UsageDate.String()
usedHours := reservationProperties.Properties.UsedHours
minUtilizationPercentage := reservationProperties.Properties.MinUtilizationPercentage
avgUtilizationPercentage := reservationProperties.Properties.AvgUtilizationPercentage
maxUtilizationPercentage := reservationProperties.Properties.MaxUtilizationPercentage
totalReservedQuantity := reservationProperties.Properties.TotalReservedQuantity

labels := prometheus.Labels{
"reservationOrderID": to.String(reservationOrderID),
"reservationID": to.String(reservationID),
"skuName": to.String(skuName),
"kind": to.String(kind),
"usageDate": usageDate,
"reservationOrderID": to.String(reservationProperties.Properties.ReservationOrderID),
"reservationID": to.String(reservationProperties.Properties.ReservationID),
"skuName": to.String(reservationProperties.Properties.SKUName),
"kind": to.String(reservationProperties.Properties.Kind),
"usageDate": reservationProperties.Properties.UsageDate.String(),
}

reservationInfo.AddInfo(labels)
reservationUsage.AddIfNotNil(labels, avgUtilizationPercentage)
reservationMinUsage.AddIfNotNil(labels, minUtilizationPercentage)
reservationMaxUsage.AddIfNotNil(labels, maxUtilizationPercentage)
reservationUsedHours.AddIfNotNil(labels, usedHours)
reservationReservedHours.AddIfNotNil(labels, reservedHours)
reservationTotalReservedQuantity.AddIfNotNil(labels, totalReservedQuantity)
reservationUsage.AddIfNotNil(labels, reservationProperties.Properties.AvgUtilizationPercentage)
reservationMinUsage.AddIfNotNil(labels, reservationProperties.Properties.MinUtilizationPercentage)
reservationMaxUsage.AddIfNotNil(labels, reservationProperties.Properties.MaxUtilizationPercentage)
reservationUsedHours.AddIfNotNil(labels, reservationProperties.Properties.UsedHours)
reservationReservedHours.AddIfNotNil(labels, reservationProperties.Properties.ReservedHours)
reservationTotalReservedQuantity.AddIfNotNil(labels, reservationProperties.Properties.TotalReservedQuantity)
}
}
}

0 comments on commit e534352

Please sign in to comment.