-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,51 @@ | ||
from web3 import Web3 | ||
from cfg import network, flux_monitor_abi | ||
|
||
from metrics import metrics | ||
|
||
def analyse_contracts(cfg, contracts): | ||
|
||
def analyse_fluxmon_contracts(cfg, contracts): | ||
print(f"Analysing {len(contracts)} contracts...") | ||
for contract in contracts: | ||
rpc_for_contract = cfg[contract["network"]] | ||
w3 = Web3(Web3.HTTPProvider(rpc_for_contract)) | ||
print( | ||
f"Analysing contract {contract['code']} on {contract['network']}") | ||
chain = network(cfg['general']['networks'], contract['network']) | ||
if network is not None: | ||
w3 = Web3(Web3.HTTPProvider(chain["rpc"])) | ||
|
||
if w3.is_connected(): | ||
contract_instance = w3.eth.contract( | ||
contract["address"], abi=flux_monitor_abi) | ||
decimals = contract_instance.functions.decimals().call() | ||
allocated_funds = contract_instance.functions.allocatedFunds().call() / 10 ** 18 | ||
available_funds = contract_instance.functions.availableFunds().call() / 10 ** 18 | ||
latest_answer = contract_instance.functions.latestAnswer().call() / 10 ** 18 | ||
latest_round = contract_instance.functions.latestRound().call() | ||
withdrawable_payment = ( | ||
contract_instance.functions.withdrawablePayment( | ||
contract['oracle_address']).call() | ||
) / 10 ** 18 | ||
|
||
metrics["fluxmon_decimals"].labels( | ||
contract["project"], contract["code"] | ||
).set(decimals) | ||
metrics["fluxmon_allocated_funds"].labels( | ||
contract["project"], contract["code"] | ||
).set(allocated_funds) | ||
metrics["fluxmon_available_funds"].labels( | ||
contract["project"], contract["code"] | ||
).set(available_funds) | ||
metrics["fluxmon_latest_answer"].labels( | ||
contract["project"], contract["code"] | ||
).set(latest_answer) | ||
metrics["fluxmon_latest_round"].labels( | ||
contract["project"], contract["code"] | ||
).set(latest_round) | ||
metrics["fluxmon_withdrawable_payment"].labels( | ||
contract["project"], contract["code"] | ||
).set(withdrawable_payment) | ||
|
||
if w3.is_connected(): | ||
print(f"Connected to {contract['network']}") | ||
contract_instance = w3.eth.contract(contract["address"], abi=cfg["abi"]) | ||
allocated_funds = contract_instance.functions.allocatedFunds().call() | ||
available_funds = contract_instance.functions.availableFunds().call() | ||
decimals = contract_instance.functions.decimals().call() | ||
latest_answer = contract_instance.functions.latest_answer().call() | ||
latest_round = contract_instance.functions.latest_round().call() | ||
withdrawable_payment = ( | ||
contract_instance.functions.withdrawablePayment().call() | ||
) | ||
else: | ||
print(f"Failed to connect to {contract['network']}") | ||
else: | ||
print(f"Failed to connect to {contract['network']}") | ||
print(f"Network {contract['network']} not found in config") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from prometheus_client import Counter, Gauge, Info | ||
|
||
metrics = { | ||
"data_feed": Gauge("data_feed", "Numeric data feed", ["project", "code"]), | ||
"data_feed_response_time_ms": Gauge( | ||
"data_feed_response_time", | ||
"Response time of data feed in milliseconds", | ||
["project", "code"], | ||
), | ||
"data_feed_last_error": Info( | ||
"data_feed_last_error", "Most recent error detected", [ | ||
"project", "code"] | ||
), | ||
"data_feed_error_count": Counter( | ||
"data_feed_error_count", | ||
"Number of errors whilst retrieving data feed", | ||
["project", "code"], | ||
), | ||
"data_feed_update_count": Counter( | ||
"data_feed_update_count", | ||
"Number of times data feed retrieved", | ||
["project", "code"], | ||
), | ||
|
||
"fluxmon_decimals": Gauge("fluxmon_decimals", "Decimals of reported value", ["project", "code"]), | ||
"fluxmon_allocated_funds": Gauge( | ||
"fluxmon_allocated_funds", | ||
"Allocated funds", | ||
["project", "code"], | ||
), | ||
"fluxmon_available_funds": Gauge( | ||
"fluxmon_available_funds", | ||
"Available funds", | ||
["project", "code"], | ||
), | ||
"fluxmon_latest_answer": Gauge( | ||
"fluxmon_latest_answer", | ||
"Lastest answer", | ||
["project", "code"], | ||
), | ||
"fluxmon_latest_round": Gauge( | ||
"fluxmon_latest_round", | ||
"Lastest round", | ||
["project", "code"], | ||
), | ||
"fluxmon_withdrawable_payment": Gauge( | ||
"fluxmon_withdrawable_payment", | ||
"Withdrawable payment", | ||
["project", "code"], | ||
), | ||
|
||
} | ||
|
||
|
||
def init_metrics(cfg): | ||
for data_feed_source in cfg["data_source_metrics"]: | ||
metrics["data_feed"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).set(0) | ||
metrics["data_feed_response_time_ms"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).set(0) | ||
metrics["data_feed_last_error"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).info({"status": "", "timestamp": "", "error": ""}) | ||
for fluxmon_contract in cfg["fluxmon_contracts"]: | ||
for metric in ["fluxmon_decimals", "fluxmon_allocated_funds", "fluxmon_available_funds", "fluxmon_latest_answer", "fluxmon_latest_round", "fluxmon_withdrawable_payment"]: | ||
metrics[metric].labels( | ||
fluxmon_contract["project"], fluxmon_contract["code"] | ||
).set(0) | ||
print( | ||
f"Initialised metrics for {data_feed_source['project']}-{data_feed_source['code']}", | ||
) |