jvstein / bitcoin-prometheus-exporter

Prometheus exporter for Bitcoin Core nodes
BSD 3-Clause "New" or "Revised" License
71 stars 37 forks source link

handle nil metrics more gracefully #2

Closed trinityXmontoya closed 4 years ago

trinityXmontoya commented 4 years ago

👋 as discussed here the bitcoin daemon will return

{
  "errors": [
    "Insufficient data or no feerate found"
  ],
  "blocks": 0
}

if you haven't yet downloaded enough transactions. because python throws a KeyError when accessing a key in a dict using square-bracket notation, pulling metrics will fail for lines 117-120, and stop execution (until you have seen enough transactions). If you switched to using the dict.get(key) notation instead it would at least return None as a default. I don't use Python often so i'm not sure if that affects something later on in your code but this is a major PITB to have to wait until the blockchain is seeded enough to run metrics, esp when running testnet or regtest. Based off this comment even if you started a node completely synced to the chain this may still not return values until you've been running the node a certain amount of time.

I understand you may not want to switch to using .get everywhere because it's basically a rescue nil & could obfuscate other issues but in the event of metrics (such as estimatesmartfee) where it's known the expected key won't exist, i'd consider different error handling.

jvstein commented 4 years ago

I've only run this against a stable, long-running node, so I haven't had a chance to see the errors you describe.

I'll put in some work to reproduce it this week. Thanks for the report!

trinityXmontoya commented 4 years ago

yo sweet work w this project tho, and np!

jvstein commented 4 years ago

I think this should be resolved on the master branch now. I pushed jvstein/bitcoin-prometheus-exporter:issue-2 to Docker Hub built off the latest commit. Can you give it a test?

jvstein commented 4 years ago

I'm pretty confident this is fixed. I've been testing with the docker-compose setup in 22feca181f33eb3fce5dd807065abea0ff91f126. Feel free to reopen if it's not fixed.