Skip to content

Commit

Permalink
prom: More msat-purge...
Browse files Browse the repository at this point in the history
  • Loading branch information
cdecker committed Jul 20, 2023
1 parent a3d3388 commit ce078bb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
69 changes: 38 additions & 31 deletions prometheus/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def collect(self):
value=blockheight,
)

print(info)
fees_msat = int(info.get(
"fees_collected_msat",
info.get("msatoshi_fees_collected", None)
Expand Down Expand Up @@ -88,10 +87,19 @@ def collect(self):
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]

Expand Down Expand Up @@ -163,32 +171,31 @@ def collect(self):
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'

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']))

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'])

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'])
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 = [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, 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, int(c['out_offered_msat']))
out_msatoshi_fulfilled_gauge.add_metric(labels, int(c['out_fulfilled_msat']))

return [
htlc_gauge,
Expand Down
13 changes: 13 additions & 0 deletions prometheus/test_prometheus.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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)

0 comments on commit ce078bb

Please sign in to comment.