Skip to content

Commit

Permalink
fix(zetaclient): distinguish between known and connected peers
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Nov 23, 2024
1 parent cda3398 commit 42c28ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
15 changes: 13 additions & 2 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
maddr "github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/spf13/cobra"
"gitlab.com/thorchain/tss/go-tss/conversion"

libp2p_network "github.com/libp2p/go-libp2p/core/network"
"github.com/zeta-chain/node/pkg/authz"
"github.com/zeta-chain/node/pkg/chains"
"github.com/zeta-chain/node/pkg/constant"
Expand Down Expand Up @@ -225,9 +227,18 @@ func Start(_ *cobra.Command, _ []string) error {
go func() {
for {
time.Sleep(30 * time.Second)
p2pHost := tssServer.GetP2PHost()
connectedPeers := lo.Map(p2pHost.Network().Conns(), func(conn libp2p_network.Conn, _ int) peer.AddrInfo {
return peer.AddrInfo{
ID: conn.RemotePeer(),
Addrs: []maddr.Multiaddr{conn.RemoteMultiaddr()},
}
})
telemetryServer.SetConnectedPeers(connectedPeers)
metrics.NumConnectedPeers.Set(float64(len(connectedPeers)))

ps := tssServer.GetKnownPeers()
metrics.NumConnectedPeers.Set(float64(len(ps)))
telemetryServer.SetConnectedPeers(ps)
telemetryServer.SetKnownPeers(ps)
}
}()
go func() {
Expand Down
29 changes: 28 additions & 1 deletion zetaclient/metrics/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type TelemetryServer struct {
status types.Status
ipAddress string
HotKeyBurnRate *BurnRate
knownPeers []peer.AddrInfo
connectedPeers []peer.AddrInfo
rtt map[peer.ID]int64
}
Expand All @@ -42,6 +43,7 @@ func NewTelemetryServer() *TelemetryServer {
lastScannedBlockNumber: make(map[int64]uint64),
lastStartTimestamp: time.Now(),
HotKeyBurnRate: NewBurnRate(100),
knownPeers: make([]peer.AddrInfo, 0),

Check warning on line 46 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L46

Added line #L46 was not covered by tests
connectedPeers: make([]peer.AddrInfo, 0),
rtt: make(map[peer.ID]int64),
}
Expand All @@ -67,6 +69,18 @@ func (t *TelemetryServer) GetPingRTT() map[peer.ID]int64 {
return t.rtt
}

func (t *TelemetryServer) SetKnownPeers(peers []peer.AddrInfo) {
t.mu.Lock()
defer t.mu.Unlock()
t.knownPeers = peers

Check warning on line 75 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L72-L75

Added lines #L72 - L75 were not covered by tests
}

func (t *TelemetryServer) GetKnownPeers() []peer.AddrInfo {
t.mu.Lock()
defer t.mu.Unlock()
return t.knownPeers

Check warning on line 81 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L78-L81

Added lines #L78 - L81 were not covered by tests
}

func (t *TelemetryServer) SetConnectedPeers(peers []peer.AddrInfo) {
t.mu.Lock()
defer t.mu.Unlock()
Expand Down Expand Up @@ -175,6 +189,7 @@ func (t *TelemetryServer) Handlers() http.Handler {
router.Handle("/ip", http.HandlerFunc(t.ipHandler)).Methods(http.MethodGet)
router.Handle("/hotkeyburnrate", http.HandlerFunc(t.hotKeyFeeBurnRate)).Methods(http.MethodGet)
router.Handle("/connectedpeers", http.HandlerFunc(t.connectedPeersHandler)).Methods(http.MethodGet)
router.Handle("/knownpeers", http.HandlerFunc(t.knownPeersHandler)).Methods(http.MethodGet)

Check warning on line 192 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L192

Added line #L192 was not covered by tests
router.Handle("/pingrtt", http.HandlerFunc(t.pingRTTHandler)).Methods(http.MethodGet)
router.Use(logMiddleware())

Expand Down Expand Up @@ -283,7 +298,19 @@ func (t *TelemetryServer) connectedPeersHandler(w http.ResponseWriter, _ *http.R
peers := t.GetConnectedPeers()
data, err := json.Marshal(peers)
if err != nil {
t.logger.Error().Err(err).Msg("Failed to marshal connected peers")
t.logger.Error().Err(err).Msg("Failed to marshal known peers")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s", string(data))

Check warning on line 305 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L301-L305

Added lines #L301 - L305 were not covered by tests
}

func (t *TelemetryServer) knownPeersHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
peers := t.GetKnownPeers()
data, err := json.Marshal(peers)
if err != nil {
t.logger.Error().Err(err).Msg("Failed to marshal known peers")

Check warning on line 313 in zetaclient/metrics/telemetry.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/metrics/telemetry.go#L308-L313

Added lines #L308 - L313 were not covered by tests
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand Down

0 comments on commit 42c28ca

Please sign in to comment.