birb_latency_reporter/reporter.py
2021-11-17 19:31:43 +01:00

58 lines
1.5 KiB
Python

import os
import time
import requests
from urllib.parse import urljoin
from apscheduler.schedulers.blocking import BlockingScheduler
def run_report_task(hostname: str, test_urls: list, report_urls:list):
for test_url in test_urls:
start_time = time.time()
try:
r = requests.get(test_url, timeout=10)
except requests.exceptions.Timeout:
print(test_url, "Timed out")
continue
else:
latency_s = time.time() - start_time
if r.status_code != 204:
print(test_url, "Wrong response")
continue
report = {
"client": hostname,
"cloud": test_url,
"measurements": {
"latency": latency_s * 1000
}
}
for report_url in report_urls:
r = requests.post(report_url, json=report)
if r.status_code != 201:
print(report_url, "Wrong response")
continue
def main():
test_urls = os.environ.get("TEST_URLS", "").split(';')
report_urls = os.environ.get("REPORT_URLS", "").split(';')
frequency = float(os.environ.get("FREQUENCY", 15))
if (not test_urls) or (not report_urls):
print("No urls defined")
return
hostname = os.environ['HOSTNAME']
scheduler = BlockingScheduler()
scheduler.add_job(lambda: run_report_task(hostname, test_urls, report_urls), trigger='interval', seconds=frequency)
scheduler.start()
if __name__ == '__main__':
main()