Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(corehttp): adjust peer counting metrics #8577

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions core/corehttp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,18 @@ func (c IpfsNodeCollector) PeersTotalValues() map[string]float64 {
if c.Node.PeerHost == nil {
return vals
}
for _, conn := range c.Node.PeerHost.Network().Conns() {
for _, peerID := range c.Node.PeerHost.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.PeerHost.Network().ConnsToPeer(peerID)
if len(conns) == 0 {
continue
}
tr := ""
for _, proto := range conn.RemoteMultiaddr().Protocols() {
for _, proto := range conns[0].RemoteMultiaddr().Protocols() {
tr = tr + "/" + proto.Name
}
vals[tr] = vals[tr] + 1
Expand Down
12 changes: 7 additions & 5 deletions core/corehttp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ func TestPeersTotal(t *testing.T) {

node := &core.IpfsNode{PeerHost: hosts[0]}
collector := IpfsNodeCollector{Node: node}
actual := collector.PeersTotalValues()
if len(actual) != 1 {
t.Fatalf("expected 1 peers transport, got %d, transport map %v", len(actual), actual)
peersTransport := collector.PeersTotalValues()
if len(peersTransport) > 2 {
t.Fatalf("expected at most 2 peers transport (tcp and upd/quic), got %d, transport map %v",
len(peersTransport), peersTransport)
}
if actual["/ip4/tcp"] != float64(3) {
t.Fatalf("expected 3 peers, got %f", actual["/ip4/tcp"])
totalPeers := peersTransport["/ip4/tcp"] + peersTransport["/ip4/udp/quic"]
if totalPeers != 3 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test is fine. However, I'll just note that it still depends on internals of libp2p. This test is creating "contact" to 3 peers, but it does not control what type of connections (and how many) are actually created to each peer (this is libp2p's business). Yet, this test checks for specific types of connections. So, if libp2p were to change and e.g. drop the quic transport, this test will fail again. In any event, I think the test is fine as it is, since it is unlikely libp2p will drop tcp or quic anytime soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I tried to retain the spirit of the test since this metric provides a map that collects transports, not just peer counts, but I'm also fine removing this level of specificity.

t.Fatalf("expected 3 peers in either tcp or upd/quic transport, got %f", totalPeers)
}
}