This repository has been archived by the owner on Jul 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·244 lines (219 loc) · 7.26 KB
/
index.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env node
const fetch = require('node-fetch')
const polka = require('polka')
const yargs = require('yargs')
const logger = require('log2json2stdout')
const { Registry, Gauge } = require('prom-client')
const { hashObject } = require('prom-client/lib/util')
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
function getArgs () {
return yargs
.usage('Usage: $0 [options]')
.env('BITCOIND_EXPORTER')
.option('interval', {
default: 100,
describe: 'Metrics fetch interval',
type: 'number'
})
.option('delay', {
default: 0,
describe: 'Start delay',
type: 'number'
})
.option('listen', {
coerce (arg) {
const [hostname, port] = arg.split(':')
return { hostname, port }
},
default: 'localhost:8000',
describe: 'Provide metrics on host:port/metrics',
type: 'string'
})
.option('node', {
default: 'http://bitcoinrpc:password@localhost:8332/',
describe: 'Fetch info from this node'
})
.option('type', {
choices: [
'bitcoin',
'bitcoincash',
'bitcoingold',
'bitcoinsv',
'dash',
'decred',
'digibyte',
'dogecoin',
'litecoin',
'qtum',
'vertcoin',
'zcash'
],
default: 'bitcoin',
describe: 'Type of bitcoin-like coin'
})
.version()
.help('help').alias('help', 'h')
.argv
}
async function makeRequest (url, method, ...params) {
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify({
jsonrpc: '2.0',
method,
params,
id: 42
}),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
const json = await res.json()
if (json.error) throw new Error(`RPC error for ${url} (code: ${json.error.code}): ${json.error.message}`)
return json.result
}
async function getEstimateFee (type, url) {
// ok: bitcoin, dash, litecoin, vertcoin
// not ok:
if (['dogecoin', 'zcash', 'bitcoincash', 'bitcoinsv'].includes(type)) return []
async function process (target, mode) {
let obj = {}
if (['dash'].includes(type)) { // no mode
obj = await makeRequest(url, 'estimatesmartfee', target)
} else {
obj = await makeRequest(url, 'estimatesmartfee', target, mode)
}
return { target, mode, value: obj.feerate }
}
const promises = []
for (let i = 1; i <= 3; i += 1) {
promises.push(process(i, 'CONSERVATIVE'))
promises.push(process(i, 'ECONOMICAL'))
}
const items = await Promise.all(promises)
return items.filter((item) => typeof item.value === 'number')
}
function initParityMetrics (registry, nodeType, nodeURL) {
const createGauge = (name, help, labelNames) => new Gauge({ name, help, labelNames, registers: [registry] })
const gauges = {
version: createGauge('bitcoind_version', 'Client version', ['value']),
latest: {
hash: createGauge('bitcoind_blockchain_latest', 'Latest block information', ['hash']),
sync: createGauge('bitcoind_blockchain_sync', 'Blockchain sync info', ['type']),
size: createGauge('bitcoind_blockchain_size_bytes', 'Blockchain size on disk', [])
},
mempool: createGauge('bitcoind_mempool_size', 'Mempool information', ['type']),
fee: createGauge('bitcoind_fee', 'Approximate fee per kilobyte by estimatesmartfee method', ['target', 'mode']),
peers: createGauge('bitcoind_peers', 'Connected peers', ['version'])
}
const data = {
version: '',
latest: '',
peers: new Map([['all', 0]])
}
const update = async () => {
const [
blockchainInfo,
mempoolInfo,
networkInfo,
peerInfo,
feeItems
] = await Promise.all([
makeRequest(nodeURL, 'getblockchaininfo'),
makeRequest(nodeURL, 'getmempoolinfo'),
makeRequest(nodeURL, 'getnetworkinfo'),
makeRequest(nodeURL, 'getpeerinfo'),
getEstimateFee(nodeType, nodeURL)
])
// version
if (networkInfo.subversion !== data.version) {
gauges.version.set({ value: networkInfo.subversion }, 1)
data.version = networkInfo.subversion
logger.info(`update version to ${networkInfo.subversion}`)
}
// latest
if (data.latest !== blockchainInfo.bestblockhash) {
if (data.latest) delete gauges.latest.hash.hashMap[hashObject({ hash: data.latest })]
gauges.latest.hash.set({ hash: blockchainInfo.bestblockhash }, blockchainInfo.blocks)
data.latest = blockchainInfo.bestblockhash
logger.info(`update latest to ${blockchainInfo.blocks}:${blockchainInfo.bestblockhash}`)
gauges.latest.sync.set({ type: 'blocks' }, blockchainInfo.blocks)
gauges.latest.sync.set({ type: 'headers' }, blockchainInfo.headers)
gauges.latest.sync.set({ type: 'progress' }, parseFloat((blockchainInfo.blocks / blockchainInfo.headers).toFixed(5)) || 0)
}
gauges.latest.size.set(blockchainInfo.size_on_disk || 0)
// mempool
gauges.mempool.set({ type: 'size' }, mempoolInfo.size)
gauges.mempool.set({ type: 'bytes' }, mempoolInfo.bytes)
// fee
for (const item of feeItems) {
gauges.fee.set({ target: item.target, mode: item.mode }, item.value)
}
// peers
for (const key of data.peers.keys()) data.peers.set(key, 0)
data.peers.set('all', peerInfo.length)
for (const peer of peerInfo) data.peers.set(peer.subver, (data.peers.get(peer.subver) || 0) + 1)
for (const [version, value] of data.peers.entries()) {
if (value === 0 && version !== 'all') delete gauges.peers.hashMap[hashObject({ version })]
else gauges.peers.set({ version }, value)
}
}
return async () => {
try {
await update()
} catch (err) {
const skip = [
'Loading block index',
'Rewinding blocks',
'Verifying blocks',
'Loading P2P addresses',
// dash specific
'Loading masternode cache',
'Loading masternode payment cache',
'Loading governance cache',
'Masternode cache is empty, skipping payments and governance cache',
'Loading fulfilled requests cache',
'Loading addresses', // also zcash
// dogecoin specific
'Activating best chain'
]
for (const item of skip) {
if (err.message.match(item)) {
logger.info(`waiting node because: ${item.toLowerCase()}`)
return
}
}
throw err
}
}
}
function createPrometheusClient (args) {
const register = new Registry()
return {
update: initParityMetrics(register, args.type, args.node),
onRequest (req, res) {
res.setHeader('Content-Type', register.contentType)
res.end(register.metrics())
}
}
}
async function main () {
const args = getArgs()
const promClient = createPrometheusClient(args)
await polka().get('/metrics', promClient.onRequest).listen(args.listen)
logger.info(`listen at ${args.listen.hostname}:${args.listen.port}`)
process.on('SIGINT', () => process.exit(0))
process.on('SIGTERM', () => process.exit(0))
while (true) {
const ts = Date.now()
await delay(args.delay) // start delay
await promClient.update()
const interval = Math.max(10, args.interval - (Date.now() - ts))
await delay(interval)
}
}
main().catch((err) => {
logger.error({ message: err.message, stack: err.stack })
process.exit(1)
})