Skip to content

Commit

Permalink
Merge pull request #250 from nautobot/fix-#248
Browse files Browse the repository at this point in the history
fixes possible error state in nautobot_ssot_duration_seconds metric
  • Loading branch information
jdrew82 authored Oct 25, 2023
2 parents db0b376 + 770ca19 commit bc14722
Showing 1 changed file with 39 additions and 42 deletions.
81 changes: 39 additions & 42 deletions nautobot_ssot/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,45 @@ 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
if job.job_class is None or not issubclass(job.job_class, (DataSource, DataTarget)):
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 bc14722

Please sign in to comment.