-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters