Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix concurrency issues with metrics #3401

Merged
merged 1 commit into from Jul 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you simplify to use sync.Mutex for and defer. I think the benefit of simpler code would outweight the reduced efficiency.

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