Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ijonas committed Jul 15, 2023
1 parent f99d1a6 commit 4dbdbd9
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 83 deletions.
21 changes: 21 additions & 0 deletions contract_metrics.py
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']}")
82 changes: 79 additions & 3 deletions data_feed_metrics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import datetime

import pyjq
import requests
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_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_last_error", "Most recent error detected", ["project", "code"]
),
"data_feed_error_count": Counter(
"data_feed_error_count",
Expand All @@ -18,3 +25,72 @@
["project", "code"],
),
}


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 analyse_data_feeds(data_feed_sources):
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()
83 changes: 4 additions & 79 deletions main.py
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)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ prometheus-client
requests
pyjq
inflection
pyyaml
pyyaml
web3

0 comments on commit 4dbdbd9

Please sign in to comment.