forked from webdevops/azure-devops-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_resourceusage.go
148 lines (117 loc) · 4.28 KB
/
metrics_resourceusage.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
package main
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"github.com/webdevops/go-common/prometheus/collector"
"go.uber.org/zap"
)
type MetricsCollectorResourceUsage struct {
collector.Processor
prometheus struct {
resourceUsageBuild *prometheus.GaugeVec
resourceUsageLicense *prometheus.GaugeVec
}
}
func (m *MetricsCollectorResourceUsage) Setup(collector *collector.Collector) {
m.Processor.Setup(collector)
m.prometheus.resourceUsageBuild = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azure_devops_resourceusage_build",
Help: "Azure DevOps resource usage for build",
},
[]string{
"name",
},
)
m.Collector.RegisterMetricList("resourceUsageBuild", m.prometheus.resourceUsageBuild, true)
m.prometheus.resourceUsageLicense = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azure_devops_resourceusage_license",
Help: "Azure DevOps resource usage for license informations",
},
[]string{
"name",
},
)
m.Collector.RegisterMetricList("resourceUsageLicense", m.prometheus.resourceUsageLicense, true)
}
func (m *MetricsCollectorResourceUsage) Reset() {}
func (m *MetricsCollectorResourceUsage) Collect(callback chan<- func()) {
ctx := m.Context()
logger := m.Logger()
m.collectResourceUsageBuild(ctx, logger, callback)
m.collectResourceUsageAgent(ctx, logger, callback)
}
func (m *MetricsCollectorResourceUsage) collectResourceUsageAgent(ctx context.Context, logger *zap.SugaredLogger, callback chan<- func()) {
resourceUsage, err := AzureDevopsClient.GetResourceUsageAgent()
if err != nil {
logger.Error(err)
return
}
resourceUsageMetric := m.Collector.GetMetricList("resourceUsageLicense")
licenseDetails := resourceUsage.Data.Provider.TaskHubLicenseDetails
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "FreeLicenseCount",
}, licenseDetails.FreeLicenseCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "FreeHostedLicenseCount",
}, licenseDetails.FreeHostedLicenseCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "EnterpriseUsersCount",
}, licenseDetails.EnterpriseUsersCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "EnterpriseUsersCount",
}, licenseDetails.EnterpriseUsersCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "PurchasedHostedLicenseCount",
}, licenseDetails.PurchasedHostedLicenseCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "PurchasedHostedLicenseCount",
}, licenseDetails.PurchasedHostedLicenseCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "TotalLicenseCount",
}, licenseDetails.TotalLicenseCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "MsdnUsersCount",
}, licenseDetails.MsdnUsersCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "HostedAgentMinutesFreeCount",
}, licenseDetails.HostedAgentMinutesFreeCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "HostedAgentMinutesUsedCount",
}, licenseDetails.HostedAgentMinutesUsedCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "TotalPrivateLicenseCount",
}, licenseDetails.TotalPrivateLicenseCount)
resourceUsageMetric.AddIfNotNil(prometheus.Labels{
"name": "TotalHostedLicenseCount",
}, licenseDetails.TotalHostedLicenseCount)
}
func (m *MetricsCollectorResourceUsage) collectResourceUsageBuild(ctx context.Context, logger *zap.SugaredLogger, callback chan<- func()) {
resourceUsage, err := AzureDevopsClient.GetResourceUsageBuild()
if err != nil {
logger.Error(err)
return
}
resourceUsageMetric := m.Collector.GetMetricList("resourceUsageBuild")
if resourceUsage.DistributedTaskAgents != nil {
resourceUsageMetric.Add(prometheus.Labels{
"name": "DistributedTaskAgents",
}, float64(*resourceUsage.DistributedTaskAgents))
}
if resourceUsage.PaidPrivateAgentSlots != nil {
resourceUsageMetric.Add(prometheus.Labels{
"name": "PaidPrivateAgentSlots",
}, float64(*resourceUsage.PaidPrivateAgentSlots))
}
if resourceUsage.TotalUsage != nil {
resourceUsageMetric.Add(prometheus.Labels{
"name": "TotalUsage",
}, float64(*resourceUsage.TotalUsage))
}
if resourceUsage.XamlControllers != nil {
resourceUsageMetric.Add(prometheus.Labels{
"name": "XamlControllers",
}, float64(*resourceUsage.XamlControllers))
}
}