Skip to content

Commit

Permalink
Merge pull request #12 from ecadlabs/peer-stats
Browse files Browse the repository at this point in the history
Add gauge to track peer stats
  • Loading branch information
juliusv authored Nov 30, 2018
2 parents 568ff88 + 0a706f2 commit b7522eb
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions collector/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ var (
[]string{"direction", "private"},
nil)

peersDesc = prometheus.NewDesc(
"tezos_node_peers",
"Stats about all peers this node ever met.",
[]string{"trusted", "state"},
nil)

pointsDesc = prometheus.NewDesc(
"tezos_node_points",
"Stats about known network points.",
Expand Down Expand Up @@ -155,6 +161,29 @@ func (c *NetworkCollector) getBootstrapped(ctx context.Context) (bool, error) {
return true, nil
}

func (c *NetworkCollector) getPeerStats(ctx context.Context) (map[string]map[string]int, error) {
peers, err := c.service.GetNetworkPeers(ctx, "")
if err != nil {
return nil, err
}

peerStats := map[string]map[string]int{
"false": map[string]int{},
"true": map[string]int{},
}

for _, peer := range peers {
trusted := "false"
if peer.Trusted {
trusted = "true"
}

peerStats[trusted][peer.State]++
}

return peerStats, nil
}

// Collect implements prometheus.Collector and is called by the Prometheus registry when collecting metrics.
func (c *NetworkCollector) Collect(ch chan<- prometheus.Metric) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
Expand All @@ -177,6 +206,16 @@ func (c *NetworkCollector) Collect(ch chan<- prometheus.Metric) {
}
}

peerStats, err := c.getPeerStats(ctx)
c.reportRPCResult("/network/peers", err, ch)
if err == nil {
for trusted, stats := range peerStats {
for state, count := range stats {
ch <- prometheus.MustNewConstMetric(peersDesc, prometheus.GaugeValue, float64(count), trusted, state)
}
}
}

pointStats, err := c.getPointStats(ctx)
c.reportRPCResult("/network/points", err, ch)
if err == nil {
Expand Down

0 comments on commit b7522eb

Please sign in to comment.