-
Notifications
You must be signed in to change notification settings - Fork 17
/
metrics_azurerm_reservation.go
169 lines (146 loc) · 6.22 KB
/
metrics_azurerm_reservation.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
package main
import (
"time"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/consumption/armconsumption"
"github.com/prometheus/client_golang/prometheus"
"github.com/webdevops/go-common/prometheus/collector"
"github.com/webdevops/go-common/utils/to"
"go.uber.org/zap"
)
// Define MetricsCollectorAzureRmReservation struct
type MetricsCollectorAzureRmReservation struct {
collector.Processor
prometheus struct {
reservationInfo *prometheus.GaugeVec
reservationUsage *prometheus.GaugeVec
reservationMinUsage *prometheus.GaugeVec
reservationMaxUsage *prometheus.GaugeVec
reservationUsedHours *prometheus.GaugeVec
reservationReservedHours *prometheus.GaugeVec
reservationTotalReservedQuantity *prometheus.GaugeVec
}
}
// Setup method to initialize Prometheus metrics
func (m *MetricsCollectorAzureRmReservation) Setup(collector *collector.Collector) {
m.Processor.Setup(collector)
commonLabels := []string{
"scope",
"reservationOrderID",
"reservationID",
"skuName",
"kind",
"usageDate",
}
m.prometheus.reservationInfo = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_reservation_info",
Help: "Azure ResourceManager Reservation Information",
},
commonLabels,
)
m.Collector.RegisterMetricList("reservationInfo", m.prometheus.reservationInfo, true)
m.prometheus.reservationUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_reservation_utilization",
Help: "Azure ResourceManager Reservation Utilization",
},
commonLabels,
)
m.Collector.RegisterMetricList("reservationUsage", m.prometheus.reservationUsage, true)
m.prometheus.reservationMinUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_reservation_utilization_min",
Help: "Azure ResourceManager Reservation Min Utilization",
},
commonLabels,
)
m.Collector.RegisterMetricList("reservationMinUsage", m.prometheus.reservationMinUsage, true)
m.prometheus.reservationMaxUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_reservation_utilization_max",
Help: "Azure ResourceManager Reservation Max Utilization",
},
commonLabels,
)
m.Collector.RegisterMetricList("reservationMaxUsage", m.prometheus.reservationMaxUsage, true)
m.prometheus.reservationUsedHours = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_reservation_used_hours",
Help: "Azure ResourceManager Reservation Used Hours",
},
commonLabels,
)
m.Collector.RegisterMetricList("reservationUsedHours", m.prometheus.reservationUsedHours, true)
m.prometheus.reservationReservedHours = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_reservation_reserved_hours",
Help: "Azure ResourceManager Reservation Reserved Hours",
},
commonLabels,
)
m.Collector.RegisterMetricList("reservationReservedHours", m.prometheus.reservationReservedHours, true)
m.prometheus.reservationTotalReservedQuantity = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_reservation_total_reserved_quantity",
Help: "Azure ResourceManager Reservation Total Reserved Quantity",
},
commonLabels,
)
m.Collector.RegisterMetricList("reservationTotalReservedQuantity", m.prometheus.reservationTotalReservedQuantity, true)
}
func (m *MetricsCollectorAzureRmReservation) Reset() {}
func (m *MetricsCollectorAzureRmReservation) Collect(callback chan<- func()) {
for _, scope := range Config.Collectors.Reservation.Scopes {
m.collectReservationUsage(logger, scope, callback)
}
}
func (m *MetricsCollectorAzureRmReservation) collectReservationUsage(logger *zap.SugaredLogger, scope string, callback chan<- func()) {
reservationInfo := m.Collector.GetMetricList("reservationInfo")
reservationUsage := m.Collector.GetMetricList("reservationUsage")
reservationMinUsage := m.Collector.GetMetricList("reservationMinUsage")
reservationMaxUsage := m.Collector.GetMetricList("reservationMaxUsage")
reservationUsedHours := m.Collector.GetMetricList("reservationUsedHours")
reservationReservedHours := m.Collector.GetMetricList("reservationReservedHours")
reservationTotalReservedQuantity := m.Collector.GetMetricList("reservationTotalReservedQuantity")
days := Config.Collectors.Reservation.FromDays
granularity := Config.Collectors.Reservation.Granularity
now := time.Now()
startDate := now.AddDate(0, 0, -days).Format("2006-01-02")
endDate := now.Format("2006-01-02")
clientFactory, err := armconsumption.NewClientFactory("<subscription-id>", AzureClient.GetCred(), AzureClient.NewArmClientOptions())
if err != nil {
logger.Panic(err)
}
// Create a pager to retrieve daily booking summaries
pager := clientFactory.NewReservationsSummariesClient().NewListPager(scope, armconsumption.Datagrain(granularity), &armconsumption.ReservationsSummariesClientListOptions{
StartDate: to.Ptr(startDate),
EndDate: to.Ptr(endDate),
Filter: nil,
ReservationID: nil,
ReservationOrderID: nil,
})
// Collect and export metrics
for pager.More() {
page, err := pager.NextPage(m.Context())
if err != nil {
logger.Panic(err)
}
for _, reservationProperties := range page.Value {
labels := prometheus.Labels{
"scope": scope,
"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, 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)
}
}
}