Skip to content

Commit fa59685

Browse files
committed
Use thread safe Map
1 parent 6b52189 commit fa59685

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

extension/prometheus/prometheus.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package prometheus
22

33
import (
44
"fmt"
5-
"github.com/prometheus/client_golang/prometheus"
65
"strconv"
6+
"sync"
77
"time"
8+
9+
"github.com/prometheus/client_golang/prometheus"
810
)
911

1012
const namespace = "goengine"
@@ -14,7 +16,7 @@ type Metrics struct {
1416
notificationCounter *prometheus.CounterVec
1517
notificationQueueDuration *prometheus.HistogramVec
1618
notificationProcessingDuration *prometheus.HistogramVec
17-
notificationStartTimes map[string]time.Time
19+
notificationStartTimes sync.Map
1820
}
1921

2022
// NewMetrics instantiate and return an object of Metrics
@@ -50,9 +52,6 @@ func NewMetrics() *Metrics {
5052
},
5153
[]string{"success", "retry"},
5254
),
53-
54-
// notificationStartTimes holds start time for notification queue and processing
55-
notificationStartTimes: make(map[string]time.Time),
5655
}
5756
}
5857

@@ -80,22 +79,22 @@ func (m *Metrics) ReceivedNotification(isNotification bool) {
8079
// QueueNotification returns http handler for prometheus
8180
func (m *Metrics) QueueNotification(notification interface{}) {
8281
key := "q" + fmt.Sprintf("%p", notification)
83-
m.notificationStartTimes[key] = time.Now()
82+
m.notificationStartTimes.Store(key, time.Now())
8483
}
8584

8685
// StartNotificationProcessing is used to record start time of notification processing
8786
func (m *Metrics) StartNotificationProcessing(notification interface{}) {
8887
key := "p" + fmt.Sprintf("%p", notification)
89-
m.notificationStartTimes[key] = time.Now()
88+
m.notificationStartTimes.Store(key, time.Now())
9089
}
9190

9291
// FinishNotificationProcessing is used to observe end time of notification queue and processing time
9392
func (m *Metrics) FinishNotificationProcessing(notification interface{}, success bool, retry bool) {
9493
memAddress := fmt.Sprintf("%p", notification)
95-
queueStartTime := m.notificationStartTimes["q"+memAddress]
96-
processingStartTime := m.notificationStartTimes["p"+memAddress]
94+
queueStartTime, _ := m.notificationStartTimes.Load("q" + memAddress)
95+
processingStartTime, _ := m.notificationStartTimes.Load("p" + memAddress)
9796
labels := prometheus.Labels{"success": strconv.FormatBool(success), "retry": strconv.FormatBool(retry)}
9897

99-
m.notificationQueueDuration.With(labels).Observe(time.Since(queueStartTime).Seconds())
100-
m.notificationProcessingDuration.With(labels).Observe(time.Since(processingStartTime).Seconds())
98+
m.notificationQueueDuration.With(labels).Observe(time.Since(queueStartTime.(time.Time)).Seconds())
99+
m.notificationProcessingDuration.With(labels).Observe(time.Since(processingStartTime.(time.Time)).Seconds())
101100
}

0 commit comments

Comments
 (0)