@@ -94,10 +94,13 @@ type PrometheusMetrics struct {
9494 workerRequestCount * prometheus.CounterVec
9595 workerQueueDepth * prometheus.GaugeVec
9696 queueDepth prometheus.Gauge
97- mu sync.Mutex
97+ mu sync.RWMutex
9898}
9999
100100func (m * PrometheusMetrics ) StartWorker (name string ) {
101+ m .mu .RLock ()
102+ defer m .mu .RUnlock ()
103+
101104 m .busyThreads .Inc ()
102105
103106 // tests do not register workers before starting them
@@ -109,6 +112,9 @@ func (m *PrometheusMetrics) StartWorker(name string) {
109112}
110113
111114func (m * PrometheusMetrics ) ReadyWorker (name string ) {
115+ m .mu .RLock ()
116+ defer m .mu .RUnlock ()
117+
112118 if m .totalWorkers == nil {
113119 return
114120 }
@@ -117,6 +123,9 @@ func (m *PrometheusMetrics) ReadyWorker(name string) {
117123}
118124
119125func (m * PrometheusMetrics ) StopWorker (name string , reason StopReason ) {
126+ m .mu .RLock ()
127+ defer m .mu .RUnlock ()
128+
120129 m .busyThreads .Dec ()
121130
122131 // tests do not register workers before starting them
@@ -246,18 +255,30 @@ func (m *PrometheusMetrics) TotalWorkers(string, int) {
246255}
247256
248257func (m * PrometheusMetrics ) TotalThreads (num int ) {
258+ m .mu .RLock ()
259+ defer m .mu .RUnlock ()
260+
249261 m .totalThreads .Add (float64 (num ))
250262}
251263
252264func (m * PrometheusMetrics ) StartRequest () {
265+ m .mu .RLock ()
266+ defer m .mu .RUnlock ()
267+
253268 m .busyThreads .Inc ()
254269}
255270
256271func (m * PrometheusMetrics ) StopRequest () {
272+ m .mu .RLock ()
273+ defer m .mu .RUnlock ()
274+
257275 m .busyThreads .Dec ()
258276}
259277
260278func (m * PrometheusMetrics ) StopWorkerRequest (name string , duration time.Duration ) {
279+ m .mu .RLock ()
280+ defer m .mu .RUnlock ()
281+
261282 if m .workerRequestTime == nil {
262283 return
263284 }
@@ -268,35 +289,53 @@ func (m *PrometheusMetrics) StopWorkerRequest(name string, duration time.Duratio
268289}
269290
270291func (m * PrometheusMetrics ) StartWorkerRequest (name string ) {
292+ m .mu .RLock ()
293+ defer m .mu .RUnlock ()
294+
271295 if m .busyWorkers == nil {
272296 return
273297 }
274298 m .busyWorkers .WithLabelValues (name ).Inc ()
275299}
276300
277301func (m * PrometheusMetrics ) QueuedWorkerRequest (name string ) {
302+ m .mu .RLock ()
303+ defer m .mu .RUnlock ()
304+
278305 if m .workerQueueDepth == nil {
279306 return
280307 }
281308 m .workerQueueDepth .WithLabelValues (name ).Inc ()
282309}
283310
284311func (m * PrometheusMetrics ) DequeuedWorkerRequest (name string ) {
312+ m .mu .RLock ()
313+ defer m .mu .RUnlock ()
314+
285315 if m .workerQueueDepth == nil {
286316 return
287317 }
288318 m .workerQueueDepth .WithLabelValues (name ).Dec ()
289319}
290320
291321func (m * PrometheusMetrics ) QueuedRequest () {
322+ m .mu .RLock ()
323+ defer m .mu .RUnlock ()
324+
292325 m .queueDepth .Inc ()
293326}
294327
295328func (m * PrometheusMetrics ) DequeuedRequest () {
329+ m .mu .RLock ()
330+ defer m .mu .RUnlock ()
331+
296332 m .queueDepth .Dec ()
297333}
298334
299335func (m * PrometheusMetrics ) Shutdown () {
336+ m .mu .Lock ()
337+ defer m .mu .Unlock ()
338+
300339 m .registry .Unregister (m .totalThreads )
301340 m .registry .Unregister (m .busyThreads )
302341 m .registry .Unregister (m .queueDepth )
0 commit comments