add README for metrics (#1906)

This commit is contained in:
Jakub Sokołowski 2020-03-17 22:09:21 +01:00 committed by GitHub
parent 8a148f256a
commit 7785aecd65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 70 deletions

43
metrics/README.md Normal file
View File

@ -0,0 +1,43 @@
# Description
This package configures Prometheus metrics for the node.
# Technical Details
We use a trick to combine our metrics with Geth ones.
The `NewMetricsServer()` function in [`metrics.go`](./metrics.go) calls our own `Handler()` function which in turn calls two handlers:
* `promhttp.HandlerFor()` - Our own custom metrics from this package.
* `gethprom.Handler(reg)` - Geth metrics defined in [`metrics`](https://github.com/ethereum/go-ethereum/tree/master/metrics)
By calling both we can extend existing metrics.
# Metrics
We add a few extra metrics on top of the normal Geth ones in [`node/metrics.go`](./node/metrics.go):
* `p2p_peers_count` - Current numbers of peers split by name.
* `p2p_peers_absolute` - Absolute number of connected peers.
* `p2p_peers_max` - Maximum number of peers that can connect.
The `p2p_peers_count` metrics includes 3 labels:
* `type` - Set to `StatusIM` for mobile and `Statusd` for daemon.
* `version` - Version of `status-go`, always with the `v` prefix.
* `platform` - Host platform, like `android-arm64` or `darwin-arm64`
The way this data is acquired is using node names, which look like this:
```
StatusIM/vrelease-0.30.1-beta.2/android-arm/go1.11.5
Statusd/v0.34.0-beta.3/linux-amd64/go1.13.1
Geth/v1.9.9-stable-5aa131ca/linux-amd64/go1.13.3
```
This 4 segment format is standard for Ethereum as you can see on https://ethstats.net/.
We parse the names using `labelsFromNodeName()` from [`node/metrics.go`](./node/metrics.go).
# Links
* https://github.com/status-im/infra-misc/issues/26
* https://github.com/status-im/status-go/pull/1648

View File

@ -15,10 +15,6 @@ var (
Name: "p2p_peers_count",
Help: "Current numbers of peers split by name.",
}, []string{"type", "version", "platform"})
nodePeersChanges = prom.NewGauge(prom.GaugeOpts{
Name: "p2p_peers_changes",
Help: "Current changes to connected peers.",
})
nodePeersAbsolute = prom.NewGauge(prom.GaugeOpts{
Name: "p2p_peers_absolute",
Help: "Absolute number of connected peers.",
@ -31,7 +27,6 @@ var (
func init() {
prom.MustRegister(nodePeersGauge)
prom.MustRegister(nodePeersChanges)
prom.MustRegister(nodePeersAbsolute)
prom.MustRegister(nodeMaxPeersGauge)
}
@ -43,25 +38,12 @@ func updateNodeMetrics(node *node.Node, evType p2p.PeerEventType) error {
}
calculatePeerCounts(server)
change, err := computeMetrics(server, evType)
if err != nil {
return err
}
nodePeersChanges.Add(change.Counter)
nodePeersAbsolute.Set(change.Absolute)
nodeMaxPeersGauge.Set(change.Max)
nodePeersAbsolute.Set(float64(server.PeerCount()))
nodeMaxPeersGauge.Set(float64(server.MaxPeers))
return nil
}
type peersChange struct {
Counter float64
Absolute float64
Max float64
}
func labelsFromNodeName(name string) (prom.Labels, error) {
tokens := strings.Split(name, "/")
if len(tokens) == 4 {
@ -95,16 +77,3 @@ func calculatePeerCounts(server *p2p.Server) {
nodePeersGauge.With(labels).Inc()
}
}
func computeMetrics(server *p2p.Server, evType p2p.PeerEventType) (result peersChange, err error) {
switch evType {
case p2p.PeerEventTypeAdd:
result.Counter = 1
case p2p.PeerEventTypeDrop:
result.Counter = -1
}
result.Absolute = float64(server.PeerCount())
result.Max = float64(server.MaxPeers)
return
}

View File

@ -43,40 +43,3 @@ func TestParsingLabelsFromNodeName(t *testing.T) {
"version": "unknown",
})
}
func TestUpdateNodeMetricsPeersCounter(t *testing.T) {
var err error
n, err := node.New(&node.Config{
P2P: p2p.Config{
MaxPeers: 10,
},
NoUSB: true,
})
require.NoError(t, err)
require.NoError(t, n.Start())
defer func() { require.NoError(t, n.Stop()) }()
server := n.Server()
change, err := computeMetrics(server, p2p.PeerEventTypeAdd)
require.NoError(t, err)
require.Equal(t, float64(1), change.Counter)
require.Equal(t, float64(10), change.Max)
// skip other events
change, err = computeMetrics(server, p2p.PeerEventTypeMsgRecv)
require.NoError(t, err)
require.Equal(t, float64(0), change.Counter)
change, err = computeMetrics(server, p2p.PeerEventTypeMsgSend)
require.NoError(t, err)
require.Equal(t, float64(0), change.Counter)
change, err = computeMetrics(server, p2p.PeerEventTypeDrop)
require.NoError(t, err)
require.Equal(t, float64(-1), change.Counter)
server.MaxPeers = 20
change, err = computeMetrics(server, p2p.PeerEventTypeDrop)
require.NoError(t, err)
require.Equal(t, float64(20), change.Max)
}