A Prometheus exporter for Bitcoin Core metrics.
- Python 3.8+
- Bitcoin Core running with cookie authentication enabled
- The
bitcoinuser must have access to the Bitcoin Core cookie file - Root/sudo access for systemd service setup
- Bitcoin Core indexes enabled:
txindex=1: Required for transaction lookupscoinstatsindex=1: Required for UTXO metrics collection
cd /opt/bitcoin/metrics-collector
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtCreate a .env file with the following configuration:
cat > .env << EOL
# Bitcoin RPC Configuration
BITCOIN_RPC_HOST=127.0.0.1
BITCOIN_RPC_PORT=8332
# Cookie Authentication
BITCOIN_COOKIE_PATH=/mnt/bitcoin-node/.cookie
# Metrics Configuration
METRICS_PORT=9332
METRICS_HOST=0.0.0.0
EOLCreate the service file:
echo '[Unit]
Description=Bitcoin Metrics Collector
After=bitcoind.service
Requires=bitcoind.service
[Service]
Type=simple
User=bitcoin
Group=bitcoin
WorkingDirectory=/opt/bitcoin/metrics-collector
Environment=PATH=/opt/bitcoin/metrics-collector/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ExecStart=/opt/bitcoin/metrics-collector/venv/bin/python src/collector.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target' | sudo tee /etc/systemd/system/bitcoin-metrics.servicesudo systemctl daemon-reload
sudo systemctl enable bitcoin-metrics.service
sudo systemctl start bitcoin-metrics.serviceThe metrics collector runs as a systemd service under the bitcoin user. To manage the service:
# Check service status
sudo systemctl status bitcoin-metrics.service
# Stop the service
sudo systemctl stop bitcoin-metrics.service
# Start the service
sudo systemctl start bitcoin-metrics.service
# Restart the service
sudo systemctl restart bitcoin-metrics.service
# View logs
sudo journalctl -u bitcoin-metrics.serviceThe collector exposes the following metrics on port 9332:
bitcoin_block_height: Current block heightbitcoin_verification_progress: Blockchain verification progressbitcoin_difficulty: Current mining difficulty
bitcoin_version_info: Bitcoin Core version information (with version as a label)bitcoin_version_major: Major version number (e.g., 28 for v28.1.0)bitcoin_version_minor: Minor version number (e.g., 1 for v28.1.0)bitcoin_version_patch: Patch version number (e.g., 0 for v28.1.0)bitcoin_version_text: Full version text including build details (with text as a label)bitcoin_version_number: Version as a decimal number (e.g., 28.1 for v28.1.0)bitcoin_full_version_string: Metric with description containing the full version string
bitcoin_mempool_size: Number of transactions in mempoolbitcoin_mempool_bytes: Size of mempool in bytes
bitcoin_peer_count: Number of connected peersbitcoin_network_bytes_sent_total: Total bytes sentbitcoin_network_bytes_received_total: Total bytes receivedbitcoin_connections_inbound: Number of inbound connectionsbitcoin_connections_outbound: Number of outbound connections
bitcoin_utxo_count: Total number of unspent transaction outputsbitcoin_utxo_size_bytes: Total size of UTXO set in bytes
Note: UTXO metrics are collected every 5 minutes to minimize system impact. The initial collection starts 15 seconds after service startup, and each collection typically takes about 1 minute to complete. Regular metrics continue to update every 15 seconds independently of UTXO collection.
bitcoin_memory_usage_bytes: Memory usage in bytesbitcoin_size_on_disk_bytes: Total blockchain size on disk in bytes
bitcoin_block_size_bytes_mean: Average block size in bytesbitcoin_block_transactions_mean: Average transactions per blockbitcoin_block_interval_seconds: Time between last two blocks
bitcoin_fee_high: Estimated fee rate for high priority - next block (sat/vB)bitcoin_fee_medium: Estimated fee rate for medium priority - 3 blocks (sat/vB)bitcoin_fee_low: Estimated fee rate for low priority - 6 blocks (sat/vB)
bitcoin_price_usd: Current Bitcoin price in USD
- The collector runs under the bitcoin user without sudo privileges
- Cookie authentication is used by default for enhanced security
- RPC connection is cached to minimize authentication overhead
- The service is configured to restart automatically on failure
- The service runs as a system service with proper user/group permissions
The collector operates with different intervals for different metric types:
- Regular metrics (blockchain, mempool, network, etc.): Every 15 seconds
- UTXO metrics: Every 5 minutes
- Bitcoin price: Every 15 seconds with regular metrics
The collection processes run in parallel, ensuring that long-running UTXO collection does not block other metrics updates.
- If the service fails to start, check the logs:
# View all logs
sudo journalctl -u bitcoin-metrics.service
# Follow logs in real-time
sudo journalctl -u bitcoin-metrics.service -f- Understanding the log prefixes:
[RPC]: RPC connection related messages[Metrics]: Regular metrics collection[UTXO]: UTXO statistics collection[Startup]: Service startup messages
- Verify the cookie file permissions:
ls -l /mnt/bitcoin-node/.cookie- Ensure Bitcoin Core is running:
sudo systemctl status bitcoind- Check if the metrics endpoint is accessible:
curl http://localhost:9332/metrics- Verify Python environment:
cd /opt/bitcoin/metrics-collector
source venv/bin/activate
python3 -c "import bitcoinrpc, prometheus_client, dotenv, aiohttp"- Test the collector directly:
cd /opt/bitcoin/metrics-collector
source venv/bin/activate
python3 src/collector.pyCurrently, the collector uses these Bitcoin Core RPC endpoints:
getblockchaininfo: General blockchain state- Used for: block height, verification progress, difficulty, disk size
getbestblockhash: Latest block hashgetblock: Block details- Used for: block time calculations
getmempoolinfo: Mempool statistics- Used for: transaction count, mempool size in bytes
estimatesmartfee: Fee estimates- Used for: high (1 block), medium (3 blocks), low (6 blocks) priority fees
getnetworkinfo: Network state- Used for: connection count, version information
getnettotals: Network traffic- Used for: bytes sent/received
getpeerinfo: Peer details- Used for: inbound/outbound connection counts
getblockstats: Detailed block metrics- Used for: average block size, transactions per block
getmemoryinfo: Memory usage statisticsgettxoutsetinfo: UTXO set information- Used with "muhash" mode for faster UTXO stats
getnetworkinfo: Bitcoin Core version details- Used for: version number, user agent string
These RPC commands could be useful for additional metrics:
getmininginfo: Mining statisticsgetnetworkhashps: Network hash rategetblocktemplate: Current block template
getrawmempool: Detailed mempool transactionsgetmempoolancestors: Transaction dependenciesgetmempooldescendants: Child transactionsgetmempoolentry: Single transaction details
getnodeaddresses: Known network nodesgetnetworkinfo: More network detailsgetpeerinfo: Additional peer metrics
getchaintips: Chain reorganization infogetchaintxstats: Chain transaction statisticsgetblockstats: Additional block metrics- total_weight
- total_size
- subsidy
- totalfee
getwalletinfo: Wallet statisticslistunspent: Detailed UTXO information
The collector exposes metrics that can be visualized in Grafana. A sample dashboard is provided in ../home-server/services/grafana/bitcoin-node-dashboard.json.
To display Bitcoin Core version in Grafana:
-
Use
bitcoin_version_numberfor a simple numeric version display (e.g., 28.1) -
To display as "v28.1", use value mapping or transformations:
Method 1: Value Mapping
- Add a new panel using the
bitcoin_version_numbermetric - Add value mapping with regex
/.*/and display textv${__value}
Method 2: Transformation
- Add a transformation to add a field using binary operation
- Select "Concat" operation with parameter "v"
- Add a new panel using the
-
For full version information, use
bitcoin_version_textorbitcoin_version_infowith label support enabled
To add new metrics from these endpoints:
- Define a new Prometheus metric in the collector
- Add the RPC call in the
collect_regular_metrics()function - Update tests and documentation