Skip to content

Commit

Permalink
feat: export ipfs_p2p metrics like Kubo does (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan authored Feb 12, 2024
1 parent 018e2c2 commit bf5686f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ share the same seed as long as the indexes are different.
}
fmt.Printf("PeerID: %s\n\n", pid)
registerVersionMetric(version)
registerIpfsNodeCollector(gnd)

tp, shutdown, err := newTracerProvider(cctx.Context)
if err != nil {
Expand Down
55 changes: 55 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,58 @@ func withHTTPMetrics(handler http.Handler, handlerName string) http.Handler {

return handler
}

var peersTotalMetric = prometheus.NewDesc(
prometheus.BuildFQName("ipfs", "p2p", "peers_total"),
"Number of connected peers",
[]string{"transport"},
nil,
)

// IpfsNodeCollector collects peer metrics
type IpfsNodeCollector struct {
Node *Node
}

func (IpfsNodeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- peersTotalMetric
}

func (c IpfsNodeCollector) Collect(ch chan<- prometheus.Metric) {
for tr, val := range c.PeersTotalValues() {
ch <- prometheus.MustNewConstMetric(
peersTotalMetric,
prometheus.GaugeValue,
val,
tr,
)
}
}

func (c IpfsNodeCollector) PeersTotalValues() map[string]float64 {
vals := make(map[string]float64)
if c.Node.host == nil {
return vals
}
for _, peerID := range c.Node.host.Network().Peers() {
// Each peer may have more than one connection (see for an explanation
// https://github.com/libp2p/go-libp2p-swarm/commit/0538806), so we grab
// only one, the first (an arbitrary and non-deterministic choice), which
// according to ConnsToPeer is the oldest connection in the list
// (https://github.com/libp2p/go-libp2p-swarm/blob/v0.2.6/swarm.go#L362-L364).
conns := c.Node.host.Network().ConnsToPeer(peerID)
if len(conns) == 0 {
continue
}
tr := ""
for _, proto := range conns[0].RemoteMultiaddr().Protocols() {
tr = tr + "/" + proto.Name
}
vals[tr] = vals[tr] + 1
}
return vals
}

func registerIpfsNodeCollector(nd *Node) {
prometheus.MustRegister(&IpfsNodeCollector{Node: nd})
}

0 comments on commit bf5686f

Please sign in to comment.