Skip to content
This repository was archived by the owner on May 9, 2024. It is now read-only.

Commit e55b5ce

Browse files
authored
fix: metric map concurrent write error (#344)
1 parent bf6ab00 commit e55b5ce

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

opentelemetry/metrics.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"math/big"
66
"net/url"
7+
"sync"
78
"time"
89

910
"github.com/ChainSafe/chainbridge-core/relayer/message"
@@ -68,6 +69,8 @@ type RelayerMetrics struct {
6869
ExecutionLatencyPerRoute metric.Int64Histogram
6970
BlockDelta metric.Int64ObservableGauge
7071
BlockDeltaMap map[uint8]*big.Int
72+
73+
lock sync.Mutex
7174
}
7275

7376
// NewRelayerMetrics initializes OpenTelemetry metrics
@@ -132,11 +135,17 @@ func NewRelayerMetrics(meter metric.Meter, attributes ...attribute.KeyValue) (*R
132135
// them to OpenTelemetry collector
133136
func (t *RelayerMetrics) TrackDepositMessage(m *message.Message) {
134137
t.DepositEventCount.Add(context.Background(), 1, t.Opts, api.WithAttributes(attribute.Int64("source", int64(m.Source))))
138+
139+
t.lock.Lock()
140+
defer t.lock.Unlock()
135141
t.MessageEventTime[m.ID()] = time.Now()
136142
}
137143

138144
func (t *RelayerMetrics) TrackExecutionError(m *message.Message) {
139145
t.ExecutionErrorCount.Add(context.Background(), 1, t.Opts, api.WithAttributes(attribute.Int64("destination", int64(m.Source))))
146+
147+
t.lock.Lock()
148+
defer t.lock.Unlock()
140149
delete(t.MessageEventTime, m.ID())
141150
}
142151

@@ -150,6 +159,9 @@ func (t *RelayerMetrics) TrackSuccessfulExecutionLatency(m *message.Message) {
150159
api.WithAttributes(attribute.Int64("source", int64(m.Source))),
151160
api.WithAttributes(attribute.Int64("destination", int64(m.Destination))),
152161
)
162+
163+
t.lock.Lock()
164+
defer t.lock.Unlock()
153165
delete(t.MessageEventTime, m.ID())
154166
}
155167

@@ -163,9 +175,15 @@ func (t *RelayerMetrics) TrackSuccessfulExecution(m *message.Message) {
163175
api.WithAttributes(attribute.Int64("source", int64(m.Source))),
164176
api.WithAttributes(attribute.Int64("destination", int64(m.Destination))),
165177
)
178+
179+
t.lock.Lock()
180+
defer t.lock.Unlock()
166181
delete(t.MessageEventTime, m.ID())
167182
}
168183

169184
func (t *RelayerMetrics) TrackBlockDelta(domainID uint8, head *big.Int, current *big.Int) {
185+
t.lock.Lock()
186+
defer t.lock.Unlock()
187+
170188
t.BlockDeltaMap[domainID] = new(big.Int).Sub(head, current)
171189
}

0 commit comments

Comments
 (0)