Skip to content

Commit

Permalink
op-heartbeat: Update golang-lru to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmanMazdaee committed Aug 20, 2023
1 parent 3658b17 commit 06c196d
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions op-heartbeat/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync/atomic"
"time"

lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

Expand All @@ -31,7 +31,7 @@ type metrics struct {

// Groups heartbeats per unique IP, version and chain ID combination.
// string(IP ++ version ++ chainID) -> *heartbeatEntry
heartbeatUsers *lru.Cache
heartbeatUsers *lru.Cache[string, *heartbeatEntry]
}

type heartbeatEntry struct {
Expand All @@ -42,7 +42,7 @@ type heartbeatEntry struct {
}

func NewMetrics(r *prometheus.Registry) Metrics {
lruCache, _ := lru.New(UsersCacheSize)
lruCache, _ := lru.New[string, *heartbeatEntry](UsersCacheSize)
m := &metrics{
heartbeats: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
Namespace: MetricsNamespace,
Expand Down Expand Up @@ -89,15 +89,14 @@ func (m *metrics) RecordHeartbeat(payload heartbeat.Payload, ip string) {

key := fmt.Sprintf("%s;%s;%s", ip, version, chainID)
now := time.Now()
previous, ok, _ := m.heartbeatUsers.PeekOrAdd(key, &heartbeatEntry{Time: now, Count: 1})
entry, ok, _ := m.heartbeatUsers.PeekOrAdd(key, &heartbeatEntry{Time: now, Count: 1})
if !ok {
// if it's a new entry, observe it and exit.
m.sameIP.WithLabelValues(chainID, version).Observe(1)
m.heartbeats.WithLabelValues(chainID, version).Inc()
return
}

entry := previous.(*heartbeatEntry)
if now.Sub(entry.Time) < MinHeartbeatInterval {
// if the span is still going, then add it up
atomic.AddUint64(&entry.Count, 1)
Expand Down

0 comments on commit 06c196d

Please sign in to comment.