forked from onemorebsmith/kaspa-stratum-bridge
-
Notifications
You must be signed in to change notification settings - Fork 11
/
prom.go
198 lines (162 loc) · 5.84 KB
/
prom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package karlsenstratum
import (
"fmt"
"net/http"
"sync"
"github.com/karlsen-network/karlsen-stratum-bridge/v2/src/gostratum"
"github.com/karlsen-network/karlsend/v2/app/appmessage"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
var workerLabels = []string{
"worker", "miner", "wallet", "ip",
}
var shareCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "kls_valid_share_counter",
Help: "Number of shares found by worker over time",
}, workerLabels)
var shareDiffCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "kls_valid_share_diff_counter",
Help: "Total difficulty of shares found by worker over time",
}, workerLabels)
var invalidCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "kls_invalid_share_counter",
Help: "Number of stale shares found by worker over time",
}, append(workerLabels, "type"))
var blockCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "kls_blocks_mined",
Help: "Number of blocks mined over time",
}, workerLabels)
var blockGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "kls_mined_blocks_gauge",
Help: "Gauge containing 1 unique instance per block mined",
}, append(workerLabels, "nonce", "bluescore", "hash"))
var disconnectCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "kls_worker_disconnect_counter",
Help: "Number of disconnects by worker",
}, workerLabels)
var jobCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "kls_worker_job_counter",
Help: "Number of jobs sent to the miner by worker over time",
}, workerLabels)
var balanceGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "kls_balance_by_wallet_gauge",
Help: "Gauge representing the wallet balance for connected workers",
}, []string{"wallet"})
var errorByWallet = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "kls_worker_errors",
Help: "Gauge representing errors by worker",
}, []string{"wallet", "error"})
var estimatedNetworkHashrate = promauto.NewGauge(prometheus.GaugeOpts{
Name: "kls_estimated_network_hashrate_gauge",
Help: "Gauge representing the estimated network hashrate",
})
var networkDifficulty = promauto.NewGauge(prometheus.GaugeOpts{
Name: "kls_network_difficulty_gauge",
Help: "Gauge representing the network difficulty",
})
var networkBlockCount = promauto.NewGauge(prometheus.GaugeOpts{
Name: "kls_network_block_count",
Help: "Gauge representing the network block count",
})
func commonLabels(worker *gostratum.StratumContext) prometheus.Labels {
return prometheus.Labels{
"worker": worker.WorkerName,
"miner": worker.RemoteApp,
"wallet": worker.WalletAddr,
"ip": worker.RemoteAddr,
}
}
func RecordShareFound(worker *gostratum.StratumContext, shareDiff float64) {
shareCounter.With(commonLabels(worker)).Inc()
shareDiffCounter.With(commonLabels(worker)).Add(shareDiff)
}
func RecordStaleShare(worker *gostratum.StratumContext) {
labels := commonLabels(worker)
labels["type"] = "stale"
invalidCounter.With(labels).Inc()
}
func RecordDupeShare(worker *gostratum.StratumContext) {
labels := commonLabels(worker)
labels["type"] = "duplicate"
invalidCounter.With(labels).Inc()
}
func RecordInvalidShare(worker *gostratum.StratumContext) {
labels := commonLabels(worker)
labels["type"] = "invalid"
invalidCounter.With(labels).Inc()
}
func RecordWeakShare(worker *gostratum.StratumContext) {
labels := commonLabels(worker)
labels["type"] = "weak"
invalidCounter.With(labels).Inc()
}
func RecordBlockFound(worker *gostratum.StratumContext, nonce, bluescore uint64, hash string) {
blockCounter.With(commonLabels(worker)).Inc()
labels := commonLabels(worker)
labels["nonce"] = fmt.Sprintf("%d", nonce)
labels["bluescore"] = fmt.Sprintf("%d", bluescore)
labels["hash"] = hash
blockGauge.With(labels).Set(1)
}
func RecordDisconnect(worker *gostratum.StratumContext) {
disconnectCounter.With(commonLabels(worker)).Inc()
}
func RecordNewJob(worker *gostratum.StratumContext) {
jobCounter.With(commonLabels(worker)).Inc()
}
func RecordNetworkStats(hashrate uint64, blockCount uint64, difficulty float64) {
estimatedNetworkHashrate.Set(float64(hashrate))
networkDifficulty.Set(difficulty)
networkBlockCount.Set(float64(blockCount))
}
func RecordWorkerError(address string, shortError ErrorShortCodeT) {
errorByWallet.With(prometheus.Labels{
"wallet": address,
"error": string(shortError),
}).Inc()
}
func InitInvalidCounter(worker *gostratum.StratumContext, errorType string) {
labels := commonLabels(worker)
labels["type"] = errorType
invalidCounter.With(labels).Add(0)
}
func InitWorkerCounters(worker *gostratum.StratumContext) {
labels := commonLabels(worker)
shareCounter.With(labels).Add(0)
shareDiffCounter.With(labels).Add(0)
errTypes := []string{"stale", "duplicate", "invalid", "weak"}
for _, e := range errTypes {
InitInvalidCounter(worker, e)
}
blockCounter.With(labels).Add(0)
disconnectCounter.With(labels).Add(0)
jobCounter.With(labels).Add(0)
}
func RecordBalances(response *appmessage.GetBalancesByAddressesResponseMessage) {
unique := map[string]struct{}{}
for _, v := range response.Entries {
// only set once per run
if _, exists := unique[v.Address]; !exists {
balanceGauge.With(prometheus.Labels{
"wallet": v.Address,
}).Set(float64(v.Balance) / 100000000)
unique[v.Address] = struct{}{}
}
}
}
var promInit sync.Once
func StartPromServer(log *zap.SugaredLogger, port string) {
go func() { // prom http handler, separate from the main router
promInit.Do(func() {
logger := log.With(zap.String("server", "prometheus"))
http.Handle("/metrics", promhttp.Handler())
logger.Info("hosting prom stats on ", port, "/metrics")
if err := http.ListenAndServe(port, nil); err != nil {
logger.Error("error serving prom metrics", zap.Error(err))
}
})
}()
}