diff --git a/prometheus/prometheus.py b/prometheus/prometheus.py index bb712bb..df14142 100755 --- a/prometheus/prometheus.py +++ b/prometheus/prometheus.py @@ -32,7 +32,6 @@ class NodeCollector(BaseLnCollector): value=blockheight, ) - print(info) fees_msat = int(info.get( "fees_collected_msat", info.get("msatoshi_fees_collected", None) @@ -88,10 +87,19 @@ class PeerCollector(BaseLnCollector): labels=['id'], ) - for p in peers: - labels = [p['id']] - count.add_metric(labels, len(p['channels'])) - connected.add_metric(labels, int(p['connected'])) + channels = self.rpc.listpeerchannels()['channels'] + # Associate each channel with a peer + peers = {} + conn = {} + for c in channels: + peer_id = c['peer_id'] + peers[peer_id] = peers.get(peer_id, 0) + 1 + conn[peer_id] = conn.get(peer_id, 0) + c['peer_connected'] + + for p in peers.keys(): + labels = [p] + count.add_metric(labels, peers[p]) + connected.add_metric(labels, conn.get(p, 0)) return [count, connected] @@ -163,32 +171,31 @@ class ChannelsCollector(BaseLnCollector): labels=['id', 'scid', 'alias'], ) - peers = self.rpc.listpeers()['peers'] - for p in peers: - for c in p['channels']: - # append alias for human readable labels, if no label is found fill with shortid. - node = self.rpc.listnodes(p['id'])['nodes'] - if len(node) != 0 and 'alias' in node[0]: - alias = node[0]['alias'] - else: - alias = 'unknown' + channels = self.rpc.listpeerchannels()['channels'] + for c in channels: + # append alias for human readable labels, if no label is found fill with shortid. + node = self.rpc.listnodes(c['peer_id'])['nodes'] + if len(node) != 0 and 'alias' in node[0]: + alias = node[0]['alias'] + else: + alias = 'unknown' - labels = [p['id'], c.get('short_channel_id', c.get('channel_id')), alias] - balance_gauge.add_metric(labels, c['to_us_msat'].to_satoshi()) - spendable_gauge.add_metric(labels, - c['spendable_msat'].to_satoshi()) - total_gauge.add_metric(labels, c['total_msat'].to_satoshi()) - htlc_gauge.add_metric(labels, len(c['htlcs'])) + labels = [c['peer_id'], c.get('short_channel_id', c.get('channel_id')), alias] + balance_gauge.add_metric(labels, c['to_us_msat'].to_satoshi()) + spendable_gauge.add_metric(labels, + c['spendable_msat'].to_satoshi()) + total_gauge.add_metric(labels, c['total_msat'].to_satoshi()) + htlc_gauge.add_metric(labels, len(c['htlcs'])) - in_payments_offered_gauge.add_metric(labels, c['in_payments_offered']) - in_payments_fulfilled_gauge.add_metric(labels, c['in_payments_fulfilled']) - in_msatoshi_offered_gauge.add_metric(labels, c['in_msatoshi_offered']) - in_msatoshi_fulfilled_gauge.add_metric(labels, c['in_msatoshi_fulfilled']) + in_payments_offered_gauge.add_metric(labels, c['in_payments_offered']) + in_payments_fulfilled_gauge.add_metric(labels, c['in_payments_fulfilled']) + in_msatoshi_offered_gauge.add_metric(labels, int(c['in_offered_msat'])) + in_msatoshi_fulfilled_gauge.add_metric(labels, int(c['in_fulfilled_msat'])) - out_payments_offered_gauge.add_metric(labels, c['out_payments_offered']) - out_payments_fulfilled_gauge.add_metric(labels, c['out_payments_fulfilled']) - out_msatoshi_offered_gauge.add_metric(labels, c['out_msatoshi_offered']) - out_msatoshi_fulfilled_gauge.add_metric(labels, c['out_msatoshi_fulfilled']) + out_payments_offered_gauge.add_metric(labels, c['out_payments_offered']) + out_payments_fulfilled_gauge.add_metric(labels, c['out_payments_fulfilled']) + out_msatoshi_offered_gauge.add_metric(labels, int(c['out_offered_msat'])) + out_msatoshi_fulfilled_gauge.add_metric(labels, int(c['out_fulfilled_msat'])) return [ htlc_gauge, diff --git a/prometheus/test_prometheus.py b/prometheus/test_prometheus.py index 6a9f2ed..6394a84 100644 --- a/prometheus/test_prometheus.py +++ b/prometheus/test_prometheus.py @@ -1,6 +1,7 @@ import os from pyln.testing.fixtures import * # noqa: F401,F403 import urllib +from ephemeral_port_reserve import reserve plugin_path = os.path.join(os.path.dirname(__file__), "prometheus.py") @@ -25,3 +26,15 @@ def test_prometheus_scrape(node_factory): +def test_prometheus_channels(node_factory): + port = reserve() + l1, l2, l3 = node_factory.line_graph( + 3, + opts=[ + {}, + {'plugin': plugin_path, 'prometheus-listen': f'127.0.0.1:{port}'}, + {} + ] + ) + scrape = urllib.request.urlopen(f'http://localhost:{port}') + print(scrape)