Skip to content

Commit ddc4a45

Browse files
committed
add lock for metrics
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 85c549d commit ddc4a45

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

auditor/auditor.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ type (
5757

5858
report map[string]*AzureAuditorReport
5959
reportUncommited map[string]*AzureAuditorReport
60-
reportLock *sync.Mutex
60+
reportLock *sync.RWMutex
61+
62+
metricsLock *sync.RWMutex
6163

6264
prometheus auditorPrometheus
6365
}
@@ -68,7 +70,8 @@ func NewAzureAuditor() *AzureAuditor {
6870
auditor.logger = log.WithFields(log.Fields{})
6971
auditor.report = map[string]*AzureAuditorReport{}
7072
auditor.reportUncommited = map[string]*AzureAuditorReport{}
71-
auditor.reportLock = &sync.Mutex{}
73+
auditor.reportLock = &sync.RWMutex{}
74+
auditor.metricsLock = &sync.RWMutex{}
7275
return &auditor
7376
}
7477

@@ -319,6 +322,8 @@ func (auditor *AzureAuditor) addCronjobBySubscription(name string, cronSpec stri
319322

320323
// apply/commit metrics (only if not dry run)
321324
if !auditor.Opts.DryRun {
325+
auditor.metricsLock.Lock()
326+
defer auditor.metricsLock.Unlock()
322327
finishCallback(ctx, contextLogger)
323328
for _, metricCallback := range metricCallbackList {
324329
metricCallback()
@@ -378,6 +383,14 @@ func (auditor *AzureAuditor) GetReport() map[string]*AzureAuditorReport {
378383
return auditor.report
379384
}
380385

386+
func (auditor *AzureAuditor) ReportLock() *sync.RWMutex {
387+
return auditor.reportLock
388+
}
389+
390+
func (auditor *AzureAuditor) MetricsLock() *sync.RWMutex {
391+
return auditor.metricsLock
392+
}
393+
381394
func (auditor *AzureAuditor) startReport(name string) *AzureAuditorReport {
382395
auditor.reportLock.Lock()
383396
defer auditor.reportLock.Unlock()

main.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var (
3535
argparser *flags.Parser
3636
opts config.Opts
3737

38-
audit *auditor.AzureAuditor
38+
azureAuditor *auditor.AzureAuditor
3939

4040
// Git version information
4141
gitCommit = "<unknown>"
@@ -50,11 +50,11 @@ func main() {
5050
log.Info(string(opts.GetJson()))
5151

5252
log.Infof("starting audit")
53-
audit = auditor.NewAzureAuditor()
54-
audit.Opts = opts
55-
audit.UserAgent = UserAgent + gitTag
56-
audit.ParseConfig(opts.Config...)
57-
audit.Run()
53+
azureAuditor = auditor.NewAzureAuditor()
54+
azureAuditor.Opts = opts
55+
azureAuditor.UserAgent = UserAgent + gitTag
56+
azureAuditor.ParseConfig(opts.Config...)
57+
azureAuditor.Run()
5858

5959
log.Infof("Starting http server on %s", opts.ServerBind)
6060
startHttpServer()
@@ -198,10 +198,10 @@ func startHttpServer() {
198198
RequestReport string
199199
}{
200200
Nonce: cspNonce,
201-
Config: audit.GetConfig(),
201+
Config: azureAuditor.GetConfig(),
202202
ReportTitle: opts.Report.Title,
203203
ReportConfig: nil,
204-
Reports: audit.GetReport(),
204+
Reports: azureAuditor.GetReport(),
205205
ServerPathReport: opts.ServerPathReport,
206206
RequestReport: "",
207207
}
@@ -273,7 +273,7 @@ func startHttpServer() {
273273
}
274274

275275
if reportName := r.URL.Query().Get("report"); reportName != "" {
276-
reportList := audit.GetReport()
276+
reportList := azureAuditor.GetReport()
277277
if report, ok := reportList[reportName]; ok {
278278

279279
if report.UpdateTime != nil {
@@ -366,7 +366,7 @@ func startHttpServer() {
366366
http.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) {
367367
w.Header().Add("Content-Type", "text/plain")
368368

369-
content, err := yaml.Marshal(audit.GetConfig())
369+
content, err := yaml.Marshal(azureAuditor.GetConfig())
370370
if err == nil {
371371
if _, writeErr := w.Write(content); writeErr != nil {
372372
log.Error(writeErr)
@@ -380,6 +380,13 @@ func startHttpServer() {
380380
}
381381
})
382382

383-
http.Handle("/metrics", azuretracing.RegisterAzureMetricAutoClean(promhttp.Handler()))
383+
http.Handle("/metrics", http.HandlerFunc(
384+
func(w http.ResponseWriter, r *http.Request) {
385+
azureAuditor.MetricsLock().RLock()
386+
defer azureAuditor.MetricsLock().RUnlock()
387+
azuretracing.RegisterAzureMetricAutoClean(promhttp.Handler()).ServeHTTP(w, r)
388+
},
389+
))
390+
384391
log.Error(http.ListenAndServe(opts.ServerBind, nil))
385392
}

0 commit comments

Comments
 (0)