Skip to content

Commit b64db24

Browse files
PLEX-1843: add node balances to beholder
1 parent 767d7c6 commit b64db24

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

metrics/balance.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package metrics
22

33
import (
4+
"context"
5+
"fmt"
6+
47
"github.com/prometheus/client_golang/prometheus"
58
"github.com/prometheus/client_golang/prometheus/promauto"
9+
"go.opentelemetry.io/otel/attribute"
10+
"go.opentelemetry.io/otel/metric"
11+
12+
"github.com/smartcontractkit/chainlink-common/pkg/beholder"
613
)
714

815
var (
@@ -11,3 +18,36 @@ var (
1118
[]string{"account", "chainID", "chainFamily"},
1219
)
1320
)
21+
22+
type GenericBalanceMetrics interface {
23+
RecordNodeBalance(ctx context.Context, account string, balance float64)
24+
}
25+
26+
var _ GenericBalanceMetrics = &balanceMetrics{}
27+
28+
type balanceMetrics struct {
29+
network string
30+
chainID string
31+
nodeBalance metric.Float64Gauge
32+
}
33+
34+
func NewGenericBalanceMetrics(network string, chainID string) (GenericBalanceMetrics, error) {
35+
nodeBalance, err := beholder.GetMeter().Float64Gauge("node_balance")
36+
if err != nil {
37+
return nil, fmt.Errorf("failed to register node balance metric: %w", err)
38+
}
39+
40+
return &balanceMetrics{
41+
network: network,
42+
chainID: chainID,
43+
nodeBalance: nodeBalance,
44+
}, nil
45+
}
46+
47+
func (m *balanceMetrics) RecordNodeBalance(ctx context.Context, account string, balance float64) {
48+
NodeBalance.WithLabelValues(account, m.chainID, m.network).Set(balance)
49+
m.nodeBalance.Record(ctx, balance, metric.WithAttributes(
50+
attribute.String("network", m.network),
51+
attribute.String("chainID", m.chainID),
52+
attribute.String("account", account)))
53+
}

metrics/balance_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package metrics
2+
3+
import (
4+
"testing"
5+
6+
"github.com/prometheus/client_golang/prometheus/testutil"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func setupTestBalanceMetrics(t *testing.T) GenericBalanceMetrics {
11+
m, err := NewGenericBalanceMetrics("test-network", "1")
12+
require.NoError(t, err)
13+
return m
14+
}
15+
16+
func TestBalanceMetrics_RecordNodeBalance(t *testing.T) {
17+
m := setupTestBalanceMetrics(t)
18+
m.RecordNodeBalance(t.Context(), "account", 5.5)
19+
require.InEpsilon(t,
20+
5.5,
21+
testutil.ToFloat64(NodeBalance.WithLabelValues("account", "1", "test-network")),
22+
0.001,
23+
)
24+
}

0 commit comments

Comments
 (0)