Skip to content

Commit

Permalink
go/p2p: collect Prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Jul 19, 2023
1 parent c234001 commit d84698b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 deletions.
13 changes: 13 additions & 0 deletions .changelog/5327.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
go/p2p: implement various metrics collection

The following Prometheus metrics were added:

- `oasis_p2p_peers`
- `oasis_p2p_blocked_peers`
- `oasis_p2p_connections`
- `oasis_p2p_topics`
- `oasis_p2p_protocols`

See [metrics documentation] for descriptions of metrics.

[metrics documentation]: https://docs.oasis.io/core/oasis-node/metrics
5 changes: 5 additions & 0 deletions docs/oasis-node/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ oasis_node_net_receive_bytes_total | Gauge | Received data for each network devi
oasis_node_net_receive_packets_total | Gauge | Received data for each network device as reported by /proc/net/dev (packets). | device | [oasis-node/cmd/common/metrics](https://github.com/oasisprotocol/oasis-core/tree/master/go/oasis-node/cmd/common/metrics/net.go)
oasis_node_net_transmit_bytes_total | Gauge | Transmitted data for each network device as reported by /proc/net/dev (bytes). | device | [oasis-node/cmd/common/metrics](https://github.com/oasisprotocol/oasis-core/tree/master/go/oasis-node/cmd/common/metrics/net.go)
oasis_node_net_transmit_packets_total | Gauge | Transmitted data for each network device as reported by /proc/net/dev (packets). | device | [oasis-node/cmd/common/metrics](https://github.com/oasisprotocol/oasis-core/tree/master/go/oasis-node/cmd/common/metrics/net.go)
oasis_p2p_blocked_peers | Gauge | Number of blocked P2P peers. | | [p2p](https://github.com/oasisprotocol/oasis-core/tree/master/go/p2p/metrics.go)
oasis_p2p_connections | Gauge | Number of P2P connections. | | [p2p](https://github.com/oasisprotocol/oasis-core/tree/master/go/p2p/metrics.go)
oasis_p2p_peers | Gauge | Number of connected P2P peers. | | [p2p](https://github.com/oasisprotocol/oasis-core/tree/master/go/p2p/metrics.go)
oasis_p2p_protocols | Gauge | Number of supported P2P protocols. | | [p2p](https://github.com/oasisprotocol/oasis-core/tree/master/go/p2p/metrics.go)
oasis_p2p_topics | Gauge | Number of supported P2P topics. | | [p2p](https://github.com/oasisprotocol/oasis-core/tree/master/go/p2p/metrics.go)
oasis_registry_entities | Gauge | Number of registry entities. | | [registry](https://github.com/oasisprotocol/oasis-core/tree/master/go/registry/metrics.go)
oasis_registry_nodes | Gauge | Number of registry nodes. | | [registry](https://github.com/oasisprotocol/oasis-core/tree/master/go/registry/metrics.go)
oasis_registry_runtimes | Gauge | Number of registry runtimes. | | [registry](https://github.com/oasisprotocol/oasis-core/tree/master/go/registry/metrics.go)
Expand Down
78 changes: 78 additions & 0 deletions go/p2p/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package p2p

import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"

cmmetrics "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/metrics"
)

const metricsUpdateInterval = 60 * time.Second

var (
peersMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "oasis_p2p_peers",
Help: "Number of connected P2P peers.",
})
blockedPeersMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "oasis_p2p_blocked_peers",
Help: "Number of blocked P2P peers.",
})
connectionsMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "oasis_p2p_connections",
Help: "Number of P2P connections.",
})
topicsMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "oasis_p2p_topics",
Help: "Number of supported P2P topics.",
})
protocolsMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "oasis_p2p_protocols",
Help: "Number of supported P2P protocols.",
})

p2pCollectors = []prometheus.Collector{
peersMetric,
blockedPeersMetric,
connectionsMetric,
topicsMetric,
protocolsMetric,
}

metricsOnce sync.Once
)

func (p *p2p) metricsWorker() {
defer close(p.metricsClosedCh)
if !cmmetrics.Enabled() {
return
}

metricsOnce.Do(func() {
prometheus.MustRegister(p2pCollectors...)
})
// Update the metrics once on initialization.
p.updateMetrics()

t := time.NewTicker(metricsUpdateInterval)
defer t.Stop()

for {
select {
case <-p.ctx.Done():
return
case <-t.C:
p.updateMetrics()
}
}
}

func (p *p2p) updateMetrics() {
peersMetric.Set(float64(len(p.host.Network().Peers())))
blockedPeersMetric.Set(float64(len(p.gater.ListBlockedPeers())))
connectionsMetric.Set(float64(len(p.host.Network().Conns())))
topicsMetric.Set(float64(len(p.peerMgr.Topics())))
protocolsMetric.Set(float64(len(p.peerMgr.Protocols())))
}
17 changes: 13 additions & 4 deletions go/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ func DebugForceAllowUnroutableAddresses() {
type p2p struct {
sync.RWMutex

ctx context.Context
ctxCancel context.CancelFunc
quitCh chan struct{}
ctx context.Context
ctxCancel context.CancelFunc
quitCh chan struct{}
metricsClosedCh chan struct{}

chainContext string
signer signature.Signer
Expand Down Expand Up @@ -111,7 +112,7 @@ func (p *p2p) Stop() {

var wg sync.WaitGroup
defer wg.Wait()
wg.Add(2)
wg.Add(3)

go func() {
defer wg.Done()
Expand All @@ -122,6 +123,11 @@ func (p *p2p) Stop() {
defer wg.Done()
_ = p.host.Close() // This blocks until the host stops.
}()

go func() {
defer wg.Done()
<-p.metricsClosedCh
}()
}

// Implements api.Service.
Expand Down Expand Up @@ -414,6 +420,7 @@ func New(identity *identity.Identity, consensus consensus.Backend, store *persis
ctx: ctx,
ctxCancel: ctxCancel,
quitCh: make(chan struct{}),
metricsClosedCh: make(chan struct{}),
chainContext: chainContext,
signer: identity.P2PSigner,
host: host,
Expand All @@ -435,6 +442,8 @@ func New(identity *identity.Identity, consensus consensus.Backend, store *persis
)
}

go p.metricsWorker()

return p, nil
}

Expand Down

0 comments on commit d84698b

Please sign in to comment.