-
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
4 changed files
with
106 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from web3 import Web3 | ||
|
||
|
||
def analyse_contracts(cfg, contracts): | ||
for contract in contracts: | ||
rpc_for_contract = cfg[contract["network"]] | ||
w3 = Web3(Web3.HTTPProvider(rpc_for_contract)) | ||
|
||
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']}") |
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,89 +1,14 @@ | ||
import datetime | ||
import time | ||
from prometheus_client import start_http_server | ||
|
||
import pyjq | ||
import requests | ||
from prometheus_client import start_http_server | ||
|
||
from cfg import load_config | ||
from data_feed_metrics import metrics | ||
|
||
|
||
def init_metrics(data_feed_sources): | ||
for data_feed_source in data_feed_sources['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": ""}) | ||
print( | ||
f"Initialised metrics for {data_feed_source['project']}-{data_feed_source['code']}", | ||
) | ||
|
||
|
||
def process_request(data_feed_sources, t): | ||
for data_feed_source in data_feed_sources['data_source_metrics']: | ||
try: | ||
request_datetime = datetime.datetime.now() | ||
response = requests.get( | ||
data_feed_source["url"], headers=data_feed_source["headers"] | ||
) | ||
current_datetime = datetime.datetime.now() | ||
if response.status_code != 200: | ||
metrics["data_feed_last_error"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).info( | ||
{ | ||
"status": response.status_code, | ||
"timestamp": current_datetime.strftime("%Y-%m-%d %H:%M:%S"), | ||
"error": response.text, | ||
} | ||
) | ||
metrics["data_feed_error_count"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).inc() | ||
else: | ||
data = response.json() | ||
duration = (current_datetime - | ||
request_datetime).total_seconds() * 1000 | ||
metrics["data_feed_response_time_ms"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).set(duration) | ||
price_str = pyjq.first(data_feed_source["jq"], data) | ||
price = float(price_str) / 10 ** data_feed_source["decimals"] | ||
metrics["data_feed"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).set(price) | ||
metrics["data_feed_update_count"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).inc() | ||
print(f"{data_feed_source['code']} price: {price}") | ||
|
||
except Exception as e: | ||
current_datetime = datetime.datetime.now() | ||
metrics["data_feed_last_error"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).info( | ||
{ | ||
"status": "0", | ||
"timestamp": current_datetime.strftime("%Y-%m-%d %H:%M:%S"), | ||
"error": f"{e.args[0]}", | ||
} | ||
) | ||
metrics["data_feed_error_count"].labels( | ||
data_feed_source["project"], data_feed_source["code"] | ||
).inc() | ||
|
||
time.sleep(t) | ||
|
||
from data_feed_metrics import analyse_data_feeds, init_metrics | ||
|
||
if __name__ == "__main__": | ||
start_http_server(8000) | ||
data_feed_sources = load_config() | ||
init_metrics(data_feed_sources) | ||
while True: | ||
process_request(data_feed_sources, 60) | ||
analyse_data_feeds(data_feed_sources) | ||
time.sleep(60) |
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ prometheus-client | |
requests | ||
pyjq | ||
inflection | ||
pyyaml | ||
pyyaml | ||
web3 |