Refactor upload_stats_to_grist function in grpc-balancer.py to consolidate server statistics into a single dictionary, improving clarity and maintainability. Update error logging to include exception details.
This commit is contained in:
parent
8425114abf
commit
77a5f1a071
@ -268,25 +268,26 @@ def select_servers():
|
|||||||
def upload_stats_to_grist(update_row):
|
def upload_stats_to_grist(update_row):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
stats = {}
|
total_stats = {
|
||||||
stats['failures'] = 0
|
'failures': 0,
|
||||||
stats['successes'] = 0
|
'successes': 0,
|
||||||
stats['rps'] = 0
|
'rps': 0
|
||||||
|
}
|
||||||
|
|
||||||
with STATS_LOCK:
|
with STATS_LOCK:
|
||||||
for server in BACKEND_SERVERS:
|
for server in BACKEND_SERVERS:
|
||||||
server_id = server['id']
|
server_id = server['id']
|
||||||
stats = SERVER_STATS[server_id]
|
server_stats = SERVER_STATS[server_id]
|
||||||
failures = sum(1 for t, success in stats if not success)
|
failures = sum(1 for t, success in server_stats if not success)
|
||||||
successes = len(stats) - failures
|
successes = len(server_stats) - failures
|
||||||
stats['failures'] += failures
|
total_stats['failures'] += failures
|
||||||
stats['successes'] += successes
|
total_stats['successes'] += successes
|
||||||
stats['rps'] += len(stats)/STATISTICS_WINDOW.total_seconds()
|
total_stats['rps'] += len(server_stats)/STATISTICS_WINDOW.total_seconds()
|
||||||
#logging.info(f"Server {server_id}: {successes} successes, {failures} failures, Weight: {weight:.2f}")
|
|
||||||
|
|
||||||
health = f"{stats['successes']}/{stats['failures']}/{stats['rps']}"
|
health = f"{total_stats['successes']}/{total_stats['failures']}/{total_stats['rps']:.2f}"
|
||||||
update_row({"Health": health})
|
update_row({"Health": health})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Failed to upload stats to Grist: {e}")
|
logging.error(f"Failed to upload stats to Grist: {str(e)}")
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user