Skip to content

Commit

Permalink
fix: Fix concurrency issues with metrics (#3401)
Browse files Browse the repository at this point in the history
  • Loading branch information
simster7 committed Jul 7, 2020
1 parent 6b967d0 commit 6378a58
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion workflow/metrics/metrics.go
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"fmt"
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -34,6 +35,8 @@ type metric struct {
}

type Metrics struct {
// Ensures mutual exclusion in workflows map
workflowsMutex sync.Mutex
metricsConfig ServerConfig
telemetryConfig ServerConfig

Expand Down Expand Up @@ -89,6 +92,9 @@ func (m *Metrics) allMetrics() []prometheus.Metric {
}

func (m *Metrics) WorkflowAdded(key string, phase v1alpha1.NodePhase) {
m.workflowsMutex.Lock()
defer m.workflowsMutex.Unlock()

if m.workflows[key] {
return
}
Expand All @@ -99,14 +105,20 @@ func (m *Metrics) WorkflowAdded(key string, phase v1alpha1.NodePhase) {
}

func (m *Metrics) WorkflowUpdated(key string, fromPhase, toPhase v1alpha1.NodePhase) {
if fromPhase == toPhase || !m.workflows[key] {
m.workflowsMutex.Lock()
hasKey := m.workflows[key]
m.workflowsMutex.Unlock()
if fromPhase == toPhase || !hasKey {
return
}
m.WorkflowDeleted(key, fromPhase)
m.WorkflowAdded(key, toPhase)
}

func (m *Metrics) WorkflowDeleted(key string, phase v1alpha1.NodePhase) {
m.workflowsMutex.Lock()
defer m.workflowsMutex.Unlock()

if !m.workflows[key] {
return
}
Expand Down

0 comments on commit 6378a58

Please sign in to comment.