Skip to content

Commit

Permalink
fixes possible error state in nautobot_ssot_duration_seconds metric
Browse files Browse the repository at this point in the history
  • Loading branch information
Kircheneer committed Oct 18, 2023
1 parent d427dcd commit d1a7bf3
Showing 1 changed file with 42 additions and 42 deletions.
84 changes: 42 additions & 42 deletions nautobot_ssot/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,48 @@ 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

Expand Down

0 comments on commit d1a7bf3

Please sign in to comment.