From de51d28a0a55332876c9d2d8af21a427fe6cfe0c Mon Sep 17 00:00:00 2001 From: Leo Kirchner Date: Wed, 18 Oct 2023 10:29:44 +0200 Subject: [PATCH] fixes possible error state in nautobot_ssot_duration_seconds metric --- nautobot_ssot/metrics.py | 88 +++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/nautobot_ssot/metrics.py b/nautobot_ssot/metrics.py index d97aa51c9..11c00ebba 100644 --- a/nautobot_ssot/metrics.py +++ b/nautobot_ssot/metrics.py @@ -24,48 +24,52 @@ def metric_ssot_jobs(): ) for job in Job.objects.all(): - if issubclass(job.job_class, (DataSource, DataTarget)): - last_job_sync = Sync.objects.filter(job_result__job_model_id=job.id).last() - if last_job_sync: - if last_job_sync.source_load_time: - ssot_job_durations.add_metric( - labels=["source_load_time", ".".join(job.natural_key())], - value=( - (last_job_sync.source_load_time.seconds * 100000) - + last_job_sync.source_load_time.microseconds - ) - / 1000, - ) - - if last_job_sync.target_load_time: - ssot_job_durations.add_metric( - labels=["target_load_time", ".".join(job.natural_key())], - value=( - (last_job_sync.target_load_time.seconds * 1000000) - + last_job_sync.target_load_time.microseconds - ) - / 1000, - ) - - if last_job_sync.diff_time: - ssot_job_durations.add_metric( - labels=["diff_time", ".".join(job.natural_key())], - value=((last_job_sync.diff_time.seconds * 1000000) + last_job_sync.diff_time.microseconds) - / 1000, - ) - - if last_job_sync.sync_time: - ssot_job_durations.add_metric( - labels=["sync_time", ".".join(job.natural_key())], - value=((last_job_sync.sync_time.seconds * 1000000) + last_job_sync.sync_time.microseconds) - / 1000, - ) - - if last_job_sync.duration: - ssot_job_durations.add_metric( - labels=["sync_duration", ".".join(job.natural_key())], - value=((last_job_sync.duration.seconds * 1000000) + last_job_sync.duration.microseconds) / 1000, - ) + # Skip any jobs that aren't SSoT jobs + try: + if not issubclass(job.job_class, (DataSource, DataTarget)): + continue + # If job.job_class is None, there will be a TypeError + except TypeError: + continue + last_job_sync = Sync.objects.filter(job_result__job_model_id=job.id).last() + if not last_job_sync: + continue + + if last_job_sync.source_load_time: + ssot_job_durations.add_metric( + labels=["source_load_time", ".".join(job.natural_key())], + value=( + (last_job_sync.source_load_time.seconds * 100000) + last_job_sync.source_load_time.microseconds + ) + / 1000, + ) + + if last_job_sync.target_load_time: + ssot_job_durations.add_metric( + labels=["target_load_time", ".".join(job.natural_key())], + value=( + (last_job_sync.target_load_time.seconds * 1000000) + last_job_sync.target_load_time.microseconds + ) + / 1000, + ) + + if last_job_sync.diff_time: + ssot_job_durations.add_metric( + labels=["diff_time", ".".join(job.natural_key())], + value=((last_job_sync.diff_time.seconds * 1000000) + last_job_sync.diff_time.microseconds) / 1000, + ) + + if last_job_sync.sync_time: + ssot_job_durations.add_metric( + labels=["sync_time", ".".join(job.natural_key())], + value=((last_job_sync.sync_time.seconds * 1000000) + last_job_sync.sync_time.microseconds) / 1000, + ) + + if last_job_sync.duration: + ssot_job_durations.add_metric( + labels=["sync_duration", ".".join(job.natural_key())], + value=((last_job_sync.duration.seconds * 1000000) + last_job_sync.duration.microseconds) / 1000, + ) yield ssot_job_durations