forked from LePetitBloc/bitcoind-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetricsHandler.js
112 lines (99 loc) · 3.42 KB
/
metricsHandler.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
const { createCall } = require('bitcoind-client');
const { register } = require('prom-client');
require('isomorphic-fetch');
const {
bestBlockIndexMetric,
bestBlockTimeMetric,
difficultyMetric,
walletVersionMetric,
walletBalanceMetric,
walletTransationsMetric,
keyPoolOldestMetric,
keyPoolSizeMetric,
unlockedUntilMetric,
transactionFeeMetric,
addressBalanceMetric
} = require('./metrics');
const {
ticker = 'BTC',
rpcuser = 'rpcuser',
rpcpassword = 'rpcpassword',
rpchost = '127.0.0.1',
rpcport = '8332',
rpcscheme = 'http',
walletMonitor = 'false'
} = process.env;
const call = createCall({
rpcuser,
rpcpassword,
rpchost,
rpcport,
rpcscheme,
});
const parseBoolean = (value) => {
if (typeof value === 'string') {
value = value.trim().toLowerCase();
return value === 'true' || value === '1';
}
return !!value;
};
const metricsHandler = (req, res) => {
res.set('Content-Type', register.contentType);
const promises = []
if (parseBoolean(walletMonitor)) {
const listUnspentPromise = call('listunspent')
.then(transactions => transactions.reduce((balances, transaction) => {
balances[transaction.address] = (balances[transaction.address] || 0) + transaction.amount;
return balances;
}, {}))
.then(balances => Object
.keys(balances)
.forEach((address) => addressBalanceMetric.set({ address }, balances[address]))
);
const walletInfoPromise = call('getwalletinfo')
.then(
({
unconfirmed_balance,
immature_balance,
balance,
walletversion,
txcount,
keypoolsize,
unlocked_until,
paytxfee,
}) => {
walletVersionMetric.set({ ticker }, walletversion);
walletBalanceMetric.set({ status: 'unconfirmed' }, unconfirmed_balance);
walletBalanceMetric.set({ status: 'immature' }, immature_balance);
walletBalanceMetric.set({ status: 'confirmed' }, balance);
walletTransationsMetric.set(txcount);
unlockedUntilMetric.set(unlocked_until || 0);
keyPoolSizeMetric.set(keypoolsize);
transactionFeeMetric.set(paytxfee);
}
);
promises.push(listUnspentPromise, walletInfoPromise);
}
const bestBlockPromise = call('getbestblockhash')
.then(hash => call('getblock', hash))
.then(bestBlockInfo => {
bestBlockIndexMetric.set(bestBlockInfo.height);
bestBlockTimeMetric.set(bestBlockInfo.time);
});
const difficultyPromise = call('getdifficulty')
.then(difficulty => difficultyMetric.set(difficulty));
promises.push(bestBlockPromise, difficultyPromise);
Promise.all(promises)
.then(() => res.end(register.metrics()))
.catch((error) => {
console.error(error);
let code = 500;
if (error.code === -28) {
code = 503;
} else if (error.code === 403) {
code = 403;
}
return res.status(code).send(`# ${error.message}\n`)
});
};
module.exports = metricsHandler;