Skip to content

Commit 8ca789e

Browse files
authored
Add Filecoin (#14)
1 parent b973276 commit 8ca789e

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/collectors/filecoin.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from helpers import strip_url
2+
from settings import cfg, logger
3+
from time import perf_counter
4+
import requests
5+
6+
7+
class filecoin_collector():
8+
9+
def __init__(self, https_url, provider):
10+
self.https_url = https_url
11+
self.labels = [
12+
'https_url', 'provider', 'blockchain', 'network_name',
13+
'network_type'
14+
]
15+
self.labels_values = [
16+
https_url, provider, cfg.blockchain, cfg.network_name,
17+
cfg.network_type
18+
]
19+
20+
def probe(self, metrics):
21+
try:
22+
payload = {
23+
'jsonrpc': '2.0',
24+
'method': "Filecoin.ChainHead",
25+
'id': 1
26+
}
27+
start = perf_counter()
28+
response = requests.post(self.https_url, json=payload).json()
29+
latency = (perf_counter() - start) * 1000
30+
31+
if response:
32+
metrics['ws_rpc_health'].add_metric(self.labels_values, True)
33+
metrics['ws_rpc_latency'].add_metric(self.labels_values,
34+
latency)
35+
metrics['ws_rpc_block_height'].add_metric(
36+
self.labels_values, response['result']['Height'])
37+
else:
38+
logger.error("Bad response from client {}: {}".format(
39+
strip_url(self.https_url), exc))
40+
metrics['ws_rpc_health'].add_metric(self.labels_values, False)
41+
except requests.RequestException as exc:
42+
logger.error("Health check failed for {}: {}".format(
43+
strip_url(self.https_url), exc))
44+
metrics['ws_rpc_health'].add_metric(self.labels_values, False)
45+
except Exception as e:
46+
logger.error("Health check failed for {}: {}".format(
47+
strip_url(self.https_url), e))
48+
metrics['ws_rpc_health'].add_metric(self.labels_values, False)

src/exporter.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from collectors.conflux import conflux_collector
1010
from collectors.bitcoin import bitcoin_collector
1111
from collectors.dogecoin import doge_collector
12+
from collectors.filecoin import filecoin_collector
1213
from settings import logger, cfg
1314

1415

@@ -30,6 +31,8 @@ def __init__(self):
3031
self._instantiate_bitcoin()
3132
if cfg.isDoge():
3233
self._instantiate_doge()
34+
if cfg.isFilecoin():
35+
self._instantiate_filecoin()
3336

3437
def _instantiate_evm(self):
3538
for item in cfg.endpoints:
@@ -82,6 +85,14 @@ def _instantiate_doge(self):
8285
doge_collector(item['https_url'], item['provider']))
8386
self.labels = self.collectors[0].labels
8487

88+
def _instantiate_filecoin(self):
89+
for item in cfg.endpoints:
90+
logger.info("Initializing filecoin node {}".format(
91+
strip_url(item['https_url'])))
92+
self.collectors.append(
93+
filecoin_collector(item['https_url'], item['provider']))
94+
self.labels = self.collectors[0].labels
95+
8596
def collect(self):
8697
metrics = {
8798
"ws_rpc_health":

src/settings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, config_file_path: str, validation_file_path: str):
1818
self.configuration = self._load_configuration_file(config_file_path)
1919
self.blockchain = self.configuration['blockchain']
2020
# Load chain_id only if evm compatible collector
21-
if self.configuration['collector'] not in ['cardano', 'solana', 'bitcoin', 'doge']:
21+
if self.configuration['collector'] not in ['cardano', 'solana', 'bitcoin', 'doge' , 'filecoin']:
2222
try:
2323
self.chain_id = self.configuration['chain_id']
2424
except KeyError:
@@ -82,6 +82,9 @@ def isBitcoin(self) -> bool:
8282
def isDoge(self) -> bool:
8383
return self.configuration['collector'] == "doge"
8484

85+
def isFilecoin(self) -> bool:
86+
return self.configuration['collector'] == "filecoin"
87+
8588
def _load_configuration_file(self, path):
8689
logger.info('Loading {}'.format(path))
8790
configuration_file_schema = Schema({
@@ -94,7 +97,7 @@ def _load_configuration_file(self, path):
9497
'network_type':
9598
And(str, lambda s: s in ('Testnet', 'Mainnet')),
9699
'collector':
97-
And(str, lambda s: s in ('evm', 'cardano', 'conflux', 'solana', 'bitcoin', 'doge')),
100+
And(str, lambda s: s in ('evm', 'cardano', 'conflux', 'solana', 'bitcoin', 'doge', 'filecoin')),
98101
Optional('connection_parameters'): {
99102
Optional('open_timeout'): And(int),
100103
Optional('close_timeout'): And(int),

0 commit comments

Comments
 (0)