Skip to content

Commit

Permalink
Added cpu_load metric to db_replicator
Browse files Browse the repository at this point in the history
  • Loading branch information
bakwc committed Oct 27, 2024
1 parent dc0a24d commit 1b1627e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mysql_ch_replicator/db_replicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Statistics:
erase_events_count: int = 0
erase_records_count: int = 0
no_events_count: int = 0
cpu_load: float = 0.0


class DbReplicator:
Expand Down Expand Up @@ -119,6 +120,7 @@ def __init__(self, config: Settings, database: str, target_database: str = None,
self.last_save_state_time = 0
self.stats = Statistics()
self.last_dump_stats_time = 0
self.last_dump_stats_process_time = 0
self.records_to_insert = defaultdict(dict) # table_name => {record_id=>record, ...}
self.records_to_delete = defaultdict(set) # table_name => {record_id, ...}
self.last_records_upload_time = 0
Expand Down Expand Up @@ -420,7 +422,17 @@ def log_stats_if_required(self):
curr_time = time.time()
if curr_time - self.last_dump_stats_time < DbReplicator.STATS_DUMP_INTERVAL:
return

curr_process_time = time.process_time()

time_spent = curr_time - self.last_dump_stats_time
process_time_spent = curr_process_time - self.last_dump_stats_process_time

if time_spent > 0.0:
self.stats.cpu_load = process_time_spent / time_spent

self.last_dump_stats_time = curr_time
self.last_dump_stats_process_time = curr_process_time
logger.info(f'stats: {json.dumps(self.stats.__dict__)}')
self.stats = Statistics()

Expand Down

0 comments on commit 1b1627e

Please sign in to comment.