Commit 06c196d6 authored by Arman Mazdaee's avatar Arman Mazdaee

op-heartbeat: Update golang-lru to v2

parent 3658b171
...@@ -6,7 +6,7 @@ import ( ...@@ -6,7 +6,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "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"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
...@@ -31,7 +31,7 @@ type metrics struct { ...@@ -31,7 +31,7 @@ type metrics struct {
// Groups heartbeats per unique IP, version and chain ID combination. // Groups heartbeats per unique IP, version and chain ID combination.
// string(IP ++ version ++ chainID) -> *heartbeatEntry // string(IP ++ version ++ chainID) -> *heartbeatEntry
heartbeatUsers *lru.Cache heartbeatUsers *lru.Cache[string, *heartbeatEntry]
} }
type heartbeatEntry struct { type heartbeatEntry struct {
...@@ -42,7 +42,7 @@ type heartbeatEntry struct { ...@@ -42,7 +42,7 @@ type heartbeatEntry struct {
} }
func NewMetrics(r *prometheus.Registry) Metrics { func NewMetrics(r *prometheus.Registry) Metrics {
lruCache, _ := lru.New(UsersCacheSize) lruCache, _ := lru.New[string, *heartbeatEntry](UsersCacheSize)
m := &metrics{ m := &metrics{
heartbeats: promauto.With(r).NewCounterVec(prometheus.CounterOpts{ heartbeats: promauto.With(r).NewCounterVec(prometheus.CounterOpts{
Namespace: MetricsNamespace, Namespace: MetricsNamespace,
...@@ -89,7 +89,7 @@ func (m *metrics) RecordHeartbeat(payload heartbeat.Payload, ip string) { ...@@ -89,7 +89,7 @@ func (m *metrics) RecordHeartbeat(payload heartbeat.Payload, ip string) {
key := fmt.Sprintf("%s;%s;%s", ip, version, chainID) key := fmt.Sprintf("%s;%s;%s", ip, version, chainID)
now := time.Now() 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 !ok {
// if it's a new entry, observe it and exit. // if it's a new entry, observe it and exit.
m.sameIP.WithLabelValues(chainID, version).Observe(1) m.sameIP.WithLabelValues(chainID, version).Observe(1)
...@@ -97,7 +97,6 @@ func (m *metrics) RecordHeartbeat(payload heartbeat.Payload, ip string) { ...@@ -97,7 +97,6 @@ func (m *metrics) RecordHeartbeat(payload heartbeat.Payload, ip string) {
return return
} }
entry := previous.(*heartbeatEntry)
if now.Sub(entry.Time) < MinHeartbeatInterval { if now.Sub(entry.Time) < MinHeartbeatInterval {
// if the span is still going, then add it up // if the span is still going, then add it up
atomic.AddUint64(&entry.Count, 1) atomic.AddUint64(&entry.Count, 1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment