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

fix: metric map concurrent write error #344

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions opentelemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"math/big"
"net/url"
"sync"
"time"

"github.com/ChainSafe/chainbridge-core/relayer/message"
Expand Down Expand Up @@ -68,6 +69,8 @@ type RelayerMetrics struct {
ExecutionLatencyPerRoute metric.Int64Histogram
BlockDelta metric.Int64ObservableGauge
BlockDeltaMap map[uint8]*big.Int

lock sync.Mutex
}

// NewRelayerMetrics initializes OpenTelemetry metrics
Expand Down Expand Up @@ -132,11 +135,17 @@ func NewRelayerMetrics(meter metric.Meter, attributes ...attribute.KeyValue) (*R
// them to OpenTelemetry collector
func (t *RelayerMetrics) TrackDepositMessage(m *message.Message) {
t.DepositEventCount.Add(context.Background(), 1, t.Opts, api.WithAttributes(attribute.Int64("source", int64(m.Source))))

t.lock.Lock()
defer t.lock.Unlock()
t.MessageEventTime[m.ID()] = time.Now()
}

func (t *RelayerMetrics) TrackExecutionError(m *message.Message) {
t.ExecutionErrorCount.Add(context.Background(), 1, t.Opts, api.WithAttributes(attribute.Int64("destination", int64(m.Source))))

t.lock.Lock()
defer t.lock.Unlock()
delete(t.MessageEventTime, m.ID())
}

Expand All @@ -150,6 +159,9 @@ func (t *RelayerMetrics) TrackSuccessfulExecutionLatency(m *message.Message) {
api.WithAttributes(attribute.Int64("source", int64(m.Source))),
api.WithAttributes(attribute.Int64("destination", int64(m.Destination))),
)

t.lock.Lock()
defer t.lock.Unlock()
delete(t.MessageEventTime, m.ID())
}

Expand All @@ -163,9 +175,15 @@ func (t *RelayerMetrics) TrackSuccessfulExecution(m *message.Message) {
api.WithAttributes(attribute.Int64("source", int64(m.Source))),
api.WithAttributes(attribute.Int64("destination", int64(m.Destination))),
)

t.lock.Lock()
defer t.lock.Unlock()
delete(t.MessageEventTime, m.ID())
}

func (t *RelayerMetrics) TrackBlockDelta(domainID uint8, head *big.Int, current *big.Int) {
t.lock.Lock()
defer t.lock.Unlock()

t.BlockDeltaMap[domainID] = new(big.Int).Sub(head, current)
}
Loading