Skip to content

Commit

Permalink
Export ipfs_p2p metrics like Kubo does
Browse files Browse the repository at this point in the history
Copy-pasted some code from Kubo
  • Loading branch information
hsanjuan committed Oct 24, 2023
1 parent 5fa19c7 commit 70df2cd
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 @@ -287,6 +287,7 @@ share the same seed as long as the indexes are different.
}
fmt.Printf("PeerID: %s\n\n", pid)
registerVersionMetric(version)
registerIpfsNodeCollector(gnd)

Check warning on line 290 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L290

Added line #L290 was not covered by tests

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

Check warning on line 116 in metrics.go

View check run for this annotation

Codecov / codecov/patch

metrics.go#L115-L116

Added lines #L115 - L116 were not covered by tests
}

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

Check warning on line 127 in metrics.go

View check run for this annotation

Codecov / codecov/patch

metrics.go#L119-L127

Added lines #L119 - L127 were not covered by tests
}

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

Check warning on line 143 in metrics.go

View check run for this annotation

Codecov / codecov/patch

metrics.go#L130-L143

Added lines #L130 - L143 were not covered by tests
}
tr := ""
for _, proto := range conns[0].RemoteMultiaddr().Protocols() {
tr = tr + "/" + proto.Name
}
vals[tr] = vals[tr] + 1

Check warning on line 149 in metrics.go

View check run for this annotation

Codecov / codecov/patch

metrics.go#L145-L149

Added lines #L145 - L149 were not covered by tests
}
return vals

Check warning on line 151 in metrics.go

View check run for this annotation

Codecov / codecov/patch

metrics.go#L151

Added line #L151 was not covered by tests
}

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

Check warning on line 155 in metrics.go

View check run for this annotation

Codecov / codecov/patch

metrics.go#L154-L155

Added lines #L154 - L155 were not covered by tests
}

0 comments on commit 70df2cd

Please sign in to comment.