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
133136func (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
138144func (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
169184func (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