Skip to content

Commit 482fb15

Browse files
author
Richard Patel
committed
add publisher balance metric
1 parent 8c7e85d commit 482fb15

File tree

3 files changed

+80
-24
lines changed

3 files changed

+80
-24
lines changed

balance.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/gagliardetto/solana-go"
8+
"github.com/gagliardetto/solana-go/rpc"
9+
"github.com/prometheus/client_golang/prometheus"
10+
"go.blockdaemon.com/pyth_exporter/metrics"
11+
"go.uber.org/zap"
12+
)
13+
14+
type balanceScraper struct {
15+
*prometheus.GaugeVec
16+
17+
log *zap.Logger
18+
rpc *rpc.Client
19+
publishers []solana.PublicKey
20+
}
21+
22+
func newBalanceScraper(publishers []solana.PublicKey, rpcURL string, log *zap.Logger) *balanceScraper {
23+
return &balanceScraper{
24+
GaugeVec: metrics.PublisherBalances,
25+
rpc: rpc.New(rpcURL),
26+
log: log,
27+
publishers: publishers,
28+
}
29+
}
30+
31+
func (b *balanceScraper) Collect(metrics chan<- prometheus.Metric) {
32+
const collectTimeout = 5 * time.Second
33+
ctx, cancel := context.WithTimeout(context.Background(), collectTimeout)
34+
defer cancel()
35+
b.update(ctx)
36+
b.GaugeVec.Collect(metrics)
37+
}
38+
39+
func (b *balanceScraper) update(ctx context.Context) {
40+
res, err := b.rpc.GetMultipleAccounts(ctx, b.publishers...)
41+
if err != nil {
42+
b.log.Warn("Failed to check publisher SOL balances", zap.Error(err))
43+
return
44+
}
45+
for i, acc := range res.Value {
46+
b.GaugeVec.
47+
WithLabelValues(b.publishers[i].String()).
48+
Set(float64(acc.Lamports))
49+
}
50+
}

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ func main() {
130130
return nil
131131
})
132132

133+
// Scrape publisher balances.
134+
balances := newBalanceScraper(publishKeys.pubkeys, *rpcURL, log.Named("balances"))
135+
metrics.Registry.MustRegister(balances)
136+
133137
if err := group.Wait(); err != nil {
134138
log.Fatal("App crashed", zap.Error(err))
135139
}

metrics/metrics.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
)
77

88
const (
9-
promNamespace = "pyth"
9+
Namespace = "pyth"
1010

11-
promSubsystemExporter = "exporter"
12-
promSubsystemOracle = "oracle"
11+
SubsystemExporter = "exporter"
12+
SubsystemSolana = "solana"
13+
SubsystemOracle = "oracle"
1314
)
1415

1516
// Prometheus metric labels.
@@ -31,69 +32,70 @@ const (
3132
var (
3233
// RPC request stats
3334
rpcRequestsTotal = factory.NewCounterVec(prometheus.CounterOpts{
34-
Namespace: promNamespace,
35-
Subsystem: promSubsystemExporter,
35+
Namespace: Namespace,
36+
Subsystem: SubsystemExporter,
3637
Name: "rpc_requests_total",
3738
Help: "Number of outgoing RPC requests from pyth_exporter to RPC nodes",
3839
}, []string{})
3940
WsActiveConns = factory.NewGauge(prometheus.GaugeOpts{
40-
Namespace: promNamespace,
41-
Subsystem: promSubsystemExporter,
41+
Namespace: Namespace,
42+
Subsystem: SubsystemExporter,
4243
Name: "ws_active_conns",
4344
Help: "Number of active WebSockets between pyth_exporter and RPC nodes",
4445
})
4546
WsEventsTotal = factory.NewCounter(prometheus.CounterOpts{
46-
Namespace: promNamespace,
47-
Subsystem: promSubsystemExporter,
47+
Namespace: Namespace,
48+
Subsystem: SubsystemExporter,
4849
Name: "ws_events_total",
4950
Help: "Number of WebSocket events delivered from RPC nodes to pyth_exporter",
5051
})
5152

52-
// Publisher Observables
53-
publisherBalances = prometheus.NewGaugeVec(prometheus.GaugeOpts{
54-
Namespace: promNamespace,
53+
PublisherBalances = prometheus.NewGaugeVec(prometheus.GaugeOpts{
54+
Namespace: Namespace,
55+
Subsystem: SubsystemSolana,
5556
Name: "publish_account_balance",
5657
Help: "SOL balance of Pyth publish account in lamports",
5758
}, []string{labelPublisher})
59+
5860
AggPrice = factory.NewGaugeVec(prometheus.GaugeOpts{
59-
Namespace: promNamespace,
60-
Subsystem: promSubsystemOracle,
61+
Namespace: Namespace,
62+
Subsystem: SubsystemOracle,
6163
Name: "aggregated_price",
6264
Help: "Last aggregated price of Pyth product",
6365
}, []string{labelProduct})
6466
AggConf = factory.NewGaugeVec(prometheus.GaugeOpts{
65-
Namespace: promNamespace,
66-
Subsystem: promSubsystemOracle,
67+
Namespace: Namespace,
68+
Subsystem: SubsystemOracle,
6769
Name: "aggregated_conf_amount",
6870
Help: "Last aggregated conf of Pyth product",
6971
}, []string{labelProduct})
7072
PublisherPrice = factory.NewGaugeVec(prometheus.GaugeOpts{
71-
Namespace: promNamespace,
72-
Subsystem: promSubsystemOracle,
73+
Namespace: Namespace,
74+
Subsystem: SubsystemOracle,
7375
Name: "publisher_price",
7476
Help: "Last published product price by Pyth publisher",
7577
}, []string{labelProduct, labelPublisher})
7678
PublisherConf = factory.NewGaugeVec(prometheus.GaugeOpts{
77-
Namespace: promNamespace,
78-
Subsystem: promSubsystemOracle,
79+
Namespace: Namespace,
80+
Subsystem: SubsystemOracle,
7981
Name: "publisher_conf_amount",
8082
Help: "Last published product confidence by Pyth publisher",
8183
}, []string{labelProduct, labelPublisher})
8284
PublisherSlot = factory.NewGaugeVec(prometheus.GaugeOpts{
83-
Namespace: promNamespace,
84-
Subsystem: promSubsystemOracle,
85+
Namespace: Namespace,
86+
Subsystem: SubsystemOracle,
8587
Name: "publisher_slot",
8688
Help: "Last observed slot for Pyth publisher",
8789
}, []string{labelProduct, labelPublisher})
8890

8991
// Publisher Observers
9092
metricTxFeesTotal = factory.NewCounterVec(prometheus.CounterOpts{
91-
Namespace: promNamespace,
93+
Namespace: Namespace,
9294
Name: "tx_fees_total",
9395
Help: "Approximate amount of SOL in lamports spent on Pyth publishing",
9496
}, []string{labelPublisher})
9597
metricTxCount = factory.NewCounterVec(prometheus.CounterOpts{
96-
Namespace: promNamespace,
98+
Namespace: Namespace,
9799
Name: "txs_total",
98100
Help: "Approximate number of Pyth transactions sent",
99101
}, []string{labelPublisher, labelTxStatus})

0 commit comments

Comments
 (0)