44 "context"
55 "fmt"
66 "strings"
7- "sync"
87 "time"
98
109 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
@@ -13,7 +12,6 @@ import (
1312 "github.com/prometheus/client_golang/prometheus"
1413 "github.com/rickb777/date/period"
1514 "github.com/webdevops/go-common/azuresdk/armclient"
16- prometheusCommon "github.com/webdevops/go-common/prometheus"
1715 "github.com/webdevops/go-common/utils/to"
1816 "go.uber.org/zap"
1917
3735
3836 Prometheus struct {
3937 MetricDuration * prometheus.GaugeVec
38+ MetricDeployment * prometheus.GaugeVec
4039 MetricTtlResources * prometheus.GaugeVec
4140 MetricTtlRoleAssignments * prometheus.GaugeVec
4241 MetricDeletedResource * prometheus.CounterVec
@@ -83,80 +82,6 @@ func (j *Janitor) Init() {
8382 j .initAuzreApiVersions ()
8483}
8584
86- func (j * Janitor ) initPrometheus () {
87- var err error
88- j .Azure .ResourceTagManager , err = j .Azure .Client .TagManager .ParseTagConfig (j .Conf .Azure .ResourceTags )
89- if err != nil {
90- j .Logger .Fatal (`unable to parse resourceTag configuration "%s": %v"` , j .Conf .Azure .ResourceTags , err .Error ())
91- }
92-
93- j .Prometheus .MetricDuration = prometheus .NewGaugeVec (
94- prometheus.GaugeOpts {
95- Name : "azurejanitor_duration" ,
96- Help : "AzureJanitor cleanup duration" ,
97- },
98- []string {},
99- )
100- prometheus .MustRegister (j .Prometheus .MetricDuration )
101-
102- j .Prometheus .MetricTtlResources = prometheus .NewGaugeVec (
103- prometheus.GaugeOpts {
104- Name : "azurejanitor_resource_ttl" ,
105- Help : "AzureJanitor resources with expiry time" ,
106- },
107- j .Azure .ResourceTagManager .AddToPrometheusLabels (
108- []string {
109- "resourceID" ,
110- "subscriptionID" ,
111- "resourceGroup" ,
112- "resourceType" ,
113- },
114- ),
115- )
116- prometheus .MustRegister (j .Prometheus .MetricTtlResources )
117-
118- j .Prometheus .MetricTtlRoleAssignments = prometheus .NewGaugeVec (
119- prometheus.GaugeOpts {
120- Name : "azurejanitor_roleassignment_ttl" ,
121- Help : "AzureJanitor roleassignments with expiry time" ,
122- },
123- []string {
124- "roleAssignmentId" ,
125- "scope" ,
126- "principalId" ,
127- "principalType" ,
128- "roleDefinitionId" ,
129- "subscriptionID" ,
130- "resourceGroup" ,
131- },
132- )
133- prometheus .MustRegister (j .Prometheus .MetricTtlRoleAssignments )
134-
135- j .Prometheus .MetricDeletedResource = prometheus .NewCounterVec (
136- prometheus.CounterOpts {
137- Name : "azurejanitor_resource_deleted_count" ,
138- Help : "AzureJanitor deleted resources" ,
139- },
140- []string {
141- "subscriptionID" ,
142- "resourceType" ,
143- },
144- )
145- prometheus .MustRegister (j .Prometheus .MetricDeletedResource )
146-
147- j .Prometheus .MetricErrors = prometheus .NewCounterVec (
148- prometheus.CounterOpts {
149- Name : "azurejanitor_error_count" ,
150- Help : "AzureJanitor error counter" ,
151- },
152- []string {
153- "subscriptionID" ,
154- "resourceType" ,
155- },
156- )
157- prometheus .MustRegister (j .Prometheus .MetricErrors )
158- }
159-
16085func (j * Janitor ) Run () {
16186 ctx := context .Background ()
16287
@@ -166,10 +91,8 @@ func (j *Janitor) Run() {
16691
16792 startTime := time .Now ()
16893 runLogger .Infof ("start janitor run" )
169- var wgMetrics sync.WaitGroup
17094
171- callbackTtlResourcesMetrics := make (chan * prometheusCommon.MetricList )
172- callbackTtlRoleAssignmentsMetrics := make (chan * prometheusCommon.MetricList )
95+ callbackFuncs := make (chan func ())
17396
17497 // subscription processing
17598 go func () {
@@ -180,70 +103,44 @@ func (j *Janitor) Run() {
180103 )
181104
182105 if j .Conf .Janitor .Deployments .Enable {
183- j .runDeployments (ctx , contextLogger , subscription , callbackTtlResourcesMetrics )
106+ j .runDeployments (ctx , contextLogger , subscription , callbackFuncs )
184107 }
185108
186109 if j .Conf .Janitor .Resources .Enable {
187- j .runResources (ctx , contextLogger , subscription , j .Conf .Janitor .Resources .Filter , callbackTtlResourcesMetrics )
110+ j .runResources (ctx , contextLogger , subscription , j .Conf .Janitor .Resources .Filter , callbackFuncs )
188111 }
189112
190113 if j .Conf .Janitor .RoleAssignments .Enable {
191- j .runRoleAssignments (ctx , contextLogger , subscription , j .Conf .Janitor .RoleAssignments .Filter , callbackTtlRoleAssignmentsMetrics )
114+ j .runRoleAssignments (ctx , contextLogger , subscription , j .Conf .Janitor .RoleAssignments .Filter , callbackFuncs )
192115 }
193116
194117 if j .Conf .Janitor .ResourceGroups .Enable {
195- j .runResourceGroups (ctx , contextLogger , subscription , j .Conf .Janitor .ResourceGroups .Filter , callbackTtlResourcesMetrics )
118+ j .runResourceGroups (ctx , contextLogger , subscription , j .Conf .Janitor .ResourceGroups .Filter , callbackFuncs )
196119 }
197120 })
198121 if err != nil {
199122 runLogger .Panic (err )
200123 }
201124
202- close (callbackTtlResourcesMetrics )
203- close (callbackTtlRoleAssignmentsMetrics )
204- }()
205-
206- // channel collecting gofunc
207- wgMetrics .Add (1 )
208- go func () {
209- defer wgMetrics .Done ()
210-
211- // store metriclists from channel
212- ttlMetricListList := []prometheusCommon.MetricList {}
213- for ttlMetrics := range callbackTtlRoleAssignmentsMetrics {
214- if ttlMetrics != nil {
215- ttlMetricListList = append (ttlMetricListList , * ttlMetrics )
216- }
217- }
218-
219- // after channel is closed: reset metric and set them to the new state
220- j .Prometheus .MetricTtlRoleAssignments .Reset ()
221- for _ , ttlMetrics := range ttlMetricListList {
222- ttlMetrics .GaugeSet (j .Prometheus .MetricTtlRoleAssignments )
223- }
125+ close (callbackFuncs )
224126 }()
225127
226- wgMetrics .Add (1 )
227- go func () {
228- defer wgMetrics .Done ()
229-
230- // store metriclists from channel
231- ttlMetricListList := []prometheusCommon.MetricList {}
232- for ttlMetrics := range callbackTtlResourcesMetrics {
233- if ttlMetrics != nil {
234- ttlMetricListList = append (ttlMetricListList , * ttlMetrics )
235- }
128+ // store metriclists from channel
129+ callbackFuncList := []func (){}
130+ for callbackFunc := range callbackFuncs {
131+ if callbackFunc != nil {
132+ callbackFuncList = append (callbackFuncList , callbackFunc )
236133 }
134+ }
237135
238- // after channel is closed: reset metric and set them to the new state
239- j .Prometheus .MetricTtlResources .Reset ()
240- for _ , ttlMetrics := range ttlMetricListList {
241- ttlMetrics .GaugeSet (j .Prometheus .MetricTtlResources )
242- }
243- }()
136+ // after channel is closed: reset metric and set them to the new state
137+ j .Prometheus .MetricDeployment .Reset ()
138+ j .Prometheus .MetricTtlResources .Reset ()
139+ j .Prometheus .MetricTtlRoleAssignments .Reset ()
244140
245- // wait for metrics processing
246- wgMetrics .Wait ()
141+ for _ , callbackFunc := range callbackFuncList {
142+ callbackFunc ()
143+ }
247144
248145 duration := time .Since (startTime )
249146 j .Prometheus .MetricDuration .With (prometheus.Labels {}).Set (duration .Seconds ())
0 commit comments