Commit 6efcbfdf authored by metacertain's avatar metacertain Committed by GitHub

Accounting metrics (#467)

* Initial total debit credit counters in metrics endpoint
* Add metrics.go for accounting
* Add counters for events, disconnects, blocks
* Count credit debit events
* Gramatic error fixed
* Fix update of debit metrics on disconnect
* Semantical rename of counters
* Fix name
* Initial total debit credit counters in metrics endpoint
* Add metrics.go for accounting
* Add counters for events, disconnects, blocks
* Count credit debit events
* Gramatic error fixed
* Fix update of debit metrics on disconnect
* Semantical rename of counters
* Fix name
* Grammatic fixes
Co-authored-by: default avatarPavle Batuta <pavle@batuta.xyz>
parent 732c9a8e
......@@ -54,6 +54,7 @@ type Accounting struct {
logger logging.Logger
store storage.StateStorer
disconnectThreshold uint64 // the debt threshold at which we will disconnect from a peer
metrics metrics
}
var (
......@@ -67,6 +68,7 @@ func NewAccounting(o Options) *Accounting {
disconnectThreshold: o.DisconnectThreshold,
logger: o.Logger,
store: o.Store,
metrics: newMetrics(),
}
}
......@@ -84,6 +86,7 @@ func (a *Accounting) Reserve(peer swarm.Address, price uint64) error {
// since we pay this we have to reduce this (positive quantity) from the balance
// the disconnectThreshold is stored as a positive value which is why it must be negated prior to comparison
if balance.freeBalance()-int64(price) < -int64(a.disconnectThreshold) {
a.metrics.AccountingBlocksCount.Inc()
return fmt.Errorf("%w with peer %v", ErrOverdraft, peer)
}
......@@ -133,6 +136,9 @@ func (a *Accounting) Credit(peer swarm.Address, price uint64) error {
balance.balance = nextBalance
a.metrics.TotalCreditedAmount.Add(float64(price))
a.metrics.CreditEventsCount.Inc()
// TODO: try to initiate payment if payment threshold is reached
// if balance.balance < -int64(a.paymentThreshold) { }
......@@ -160,8 +166,12 @@ func (a *Accounting) Debit(peer swarm.Address, price uint64) error {
balance.balance = nextBalance
a.metrics.TotalDebitedAmount.Add(float64(price))
a.metrics.DebitEventsCount.Inc()
if nextBalance >= int64(a.disconnectThreshold) {
// peer to much in debt
// peer too much in debt
a.metrics.AccountingDisconnectsCount.Inc()
return p2p.NewDisconnectError(fmt.Errorf("disconnect threshold exceeded for peer %s", peer.String()))
}
......
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package accounting
import (
m "github.com/ethersphere/bee/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
)
type metrics struct {
// all metrics fields must be exported
// to be able to return them by Metrics()
// using reflection
TotalDebitedAmount prometheus.Counter
TotalCreditedAmount prometheus.Counter
DebitEventsCount prometheus.Counter
CreditEventsCount prometheus.Counter
AccountingDisconnectsCount prometheus.Counter
AccountingBlocksCount prometheus.Counter
}
func newMetrics() metrics {
subsystem := "accounting"
return metrics{
TotalDebitedAmount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "total_debited_amount",
Help: "Amount of BZZ debited to peers (potential income of the node)",
}),
TotalCreditedAmount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "total_credited_amount",
Help: "Amount of BZZ credited to peers (potential cost of the node)",
}),
DebitEventsCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "debit_events_count",
Help: "Number of occurrences of BZZ debit events towards peers",
}),
CreditEventsCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "credit_events_count",
Help: "Number of occurrences of BZZ credit events towards peers",
}),
AccountingDisconnectsCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "accounting_disconnects_count",
Help: "Number of occurrences of peers disconnected based on payment thresholds",
}),
AccountingBlocksCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "accounting_blocks_count",
Help: "Number of occurrences of temporarily skipping a peer to avoid crossing their disconnect thresholds",
}),
}
}
func (a *Accounting) Metrics() []prometheus.Collector {
return m.PrometheusCollectorsFromFields(a.metrics)
}
......@@ -348,6 +348,8 @@ func NewBee(o Options) (*Bee, error) {
// register metrics from components
debugAPIService.MustRegisterMetrics(p2ps.Metrics()...)
debugAPIService.MustRegisterMetrics(pingPong.Metrics()...)
debugAPIService.MustRegisterMetrics(acc.Metrics()...)
if apiService != nil {
debugAPIService.MustRegisterMetrics(apiService.Metrics()...)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment