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

go/p2p: collect Prometheus metrics #5327

Merged
merged 1 commit into from
Jul 20, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
74 changes: 74 additions & 0 deletions go/p2p/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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...)
})

for {
p.updateMetrics()

select {
case <-p.ctx.Done():
return
case <-time.After(metricsUpdateInterval):
}
}
}

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 @@ -100,6 +101,8 @@ func (p *p2p) Start() error {
// Unfortunately, we cannot start the host as libp2p starts everything on construction.
// However, we can start everything else.
p.peerMgr.Start()
go p.metricsWorker()

return nil
}

Expand All @@ -111,7 +114,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 +125,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 +422,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 Down