|
1 | 1 | package metrics |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
4 | 7 | "github.com/prometheus/client_golang/prometheus" |
5 | 8 | "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" |
6 | 13 | ) |
7 | 14 |
|
8 | 15 | var ( |
|
11 | 18 | []string{"account", "chainID", "chainFamily"}, |
12 | 19 | ) |
13 | 20 | ) |
| 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 | +} |
0 commit comments