Skip to content

Commit

Permalink
fullrt: add a prometheus metric for the size of the routing table
Browse files Browse the repository at this point in the history
This is a bit hacky maybe we want to do this through DI ? Would be nice 
to have this as a global metric.
  • Loading branch information
Jorropo committed Jun 13, 2023
1 parent 0f65ba1 commit 71ce914
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion fullrt/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/core/routing"
swarm "github.com/libp2p/go-libp2p/p2p/net/swarm"
"github.com/libp2p/go-libp2p/p2p/net/swarm"

"github.com/gogo/protobuf/proto"
u "github.com/ipfs/boxo/util"
Expand All @@ -44,6 +44,8 @@ import (
"github.com/libp2p/go-libp2p-xor/kademlia"
kadkey "github.com/libp2p/go-libp2p-xor/key"
"github.com/libp2p/go-libp2p-xor/trie"

"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -95,6 +97,8 @@ type FullRT struct {
bulkSendParallelism int

self peer.ID

rtSizeGauge prometheus.Gauge
}

// NewFullRT creates a DHT client that tracks the full network. It takes a protocol prefix for the given network,
Expand Down Expand Up @@ -197,6 +201,18 @@ func NewFullRT(h host.Host, protocolPrefix protocol.ID, options ...Option) (*Ful
self: self,
}

counter := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "libp2p",
Subsystem: "dht_size",
Name: "routing table size",
Help: "The size of the full routing table.",
})
counter.Set(0)
if err := prometheus.Register(counter); err == nil {
// Only unregister if register is successfull
rt.rtSizeGauge = counter
}

rt.wg.Add(1)
go rt.runCrawler(ctx)

Expand Down Expand Up @@ -350,12 +366,19 @@ func (dht *FullRT) runCrawler(ctx context.Context) {
dht.rt = newRt
dht.lastCrawlTime = time.Now()
dht.rtLk.Unlock()

if dht.rtSizeGauge != nil {
dht.rtSizeGauge.Set(float64(len(m)))
}
}
}

func (dht *FullRT) Close() error {
dht.cancel()
err := dht.ProviderManager.Process().Close()
if dht.rtSizeGauge != nil {
prometheus.Unregister(dht.rtSizeGauge)
}
dht.wg.Wait()
return err
}
Expand Down

0 comments on commit 71ce914

Please sign in to comment.