Skip to content

Commit 816ea61

Browse files
committed
Moving metric emition logic to a function
1 parent 3e08f5e commit 816ea61

File tree

1 file changed

+26
-72
lines changed

1 file changed

+26
-72
lines changed

src/clusterfuzz/_internal/build_management/build_manager.py

Lines changed: 26 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ def set_env_var(name, value):
299299
logs.error(f'Could not find app {app_name!r} in search directories.')
300300

301301

302+
def _emit_job_build_retrieval_metric(start_time, step, build_type):
303+
elapsed_minutes = (time.time() - start_time) / 60
304+
monitoring_metrics.JOB_BUILD_RETRIEVAL_TIME.add(
305+
elapsed_minutes, {
306+
'job': os.getenv('JOB_NAME'),
307+
'platform': environment.platform(),
308+
'step': step,
309+
'build_type': build_type,
310+
})
311+
312+
302313
class BaseBuild:
303314
"""Represents a build."""
304315

@@ -438,15 +449,7 @@ def _download_and_open_build_archive(self, base_build_dir: str,
438449
try:
439450
start_time = time.time()
440451
storage.copy_file_from(build_url, build_local_archive)
441-
# In minutes, as per metric definition.
442-
build_download_duration = (time.time() - start_time) / 60
443-
monitoring_metrics.JOB_BUILD_RETRIEVAL_TIME.add(
444-
build_download_duration, {
445-
'job': os.getenv('JOB_NAME'),
446-
'platform': environment.platform(),
447-
'step': 'download',
448-
'build_type': self._build_type,
449-
})
452+
_emit_job_build_retrieval_metric(start_time, 'download', self._build_type)
450453
except Exception as e:
451454
logs.error(f'Unable to download build from {build_url}: {e}')
452455
raise
@@ -532,14 +535,9 @@ def _unpack_build(self,
532535
list_fuzz_target_start_time = time.time()
533536
self.fuzz_targets = list(build.list_fuzz_targets())
534537
# In minutes, as per metric definition.
535-
elapsed_time = (time.time() - list_fuzz_target_start_time) / 60
536-
monitoring_metrics.JOB_BUILD_RETRIEVAL_TIME.add(
537-
elapsed_time, {
538-
'job': os.getenv('JOB_NAME'),
539-
'platform': environment.platform(),
540-
'step': 'list_fuzz_targets',
541-
'build_type': self._build_type,
542-
})
538+
_emit_job_build_retrieval_metric(list_fuzz_target_start_time,
539+
'list_fuzz_targets',
540+
self._build_type)
543541
# We only want to unpack a single fuzz target if unpack_everything is
544542
# False.
545543
fuzz_target_to_unpack = self.fuzz_target
@@ -564,16 +562,8 @@ def _unpack_build(self,
564562
fuzz_target=fuzz_target_to_unpack,
565563
trusted=trusted)
566564

567-
# In minutes, as per metric definition.
568-
unpack_elapsed_time = (time.time() - unpack_start_time) / 60
569-
570-
monitoring_metrics.JOB_BUILD_RETRIEVAL_TIME.add(
571-
unpack_elapsed_time, {
572-
'job': os.getenv('JOB_NAME'),
573-
'platform': environment.platform(),
574-
'step': 'unpack',
575-
'build_type': self._build_type,
576-
})
565+
_emit_job_build_retrieval_metric(unpack_start_time, 'unpack',
566+
self._build_type)
577567

578568
except Exception as e:
579569
logs.error(f'Unable to unpack build archive {build_url}: {e}')
@@ -582,31 +572,17 @@ def _unpack_build(self,
582572
if unpack_everything:
583573
list_fuzz_target_start_time = time.time()
584574
self.fuzz_targets = list(self._get_fuzz_targets_from_dir(build_dir))
585-
# In minutes, as per metric definition.
586-
elapsed_listing_time = (time.time() - list_fuzz_target_start_time) / 60
587-
monitoring_metrics.JOB_BUILD_RETRIEVAL_TIME.add(
588-
elapsed_listing_time, {
589-
'job': os.getenv('JOB_NAME'),
590-
'platform': environment.platform(),
591-
'step': 'list_fuzz_targets',
592-
'build_type': self._build_type,
593-
})
575+
_emit_job_build_retrieval_metric(list_fuzz_target_start_time,
576+
'list_fuzz_targets', self._build_type)
594577
else:
595578
# If this is partial build due to selected build files, then mark it as
596579
# such so that it is not re-used.
597580
partial_build_file_path = os.path.join(build_dir, PARTIAL_BUILD_FILE)
598581
utils.write_data_to_file('', partial_build_file_path)
599582

583+
_emit_job_build_retrieval_metric(start_time, 'total', self._build_type)
600584
elapsed_time = time.time() - start_time
601585

602-
monitoring_metrics.JOB_BUILD_RETRIEVAL_TIME.add(
603-
elapsed_time, {
604-
'job': os.getenv('JOB_NAME'),
605-
'platform': environment.platform(),
606-
'step': 'total',
607-
'build_type': self._build_type,
608-
})
609-
610586
elapsed_mins = elapsed_time / 60.
611587
log_func = logs.warning if elapsed_time > UNPACK_TIME_LIMIT else logs.info
612588
log_func(f'Build took {elapsed_mins:0.02f} minutes to unpack.')
@@ -935,16 +911,8 @@ def _unpack_custom_build(self):
935911
build_local_archive):
936912
return False
937913

938-
# In minutes, as per metric definition.
939-
build_download_time = (time.time() - download_start_time) / 60
940-
monitoring_metrics.JOB_BUILD_RETRIEVAL_TIME.add(
941-
build_download_time, {
942-
'job': os.getenv('JOB_NAME'),
943-
'platform': environment.platform(),
944-
'step': 'download',
945-
'build_type': self._build_type,
946-
})
947-
914+
_emit_job_build_retrieval_metric(download_start_time, 'download',
915+
self._build_type)
948916
# If custom binary is an archive, then unpack it.
949917
if archive.is_archive(self.custom_binary_filename):
950918
try:
@@ -964,15 +932,8 @@ def _unpack_custom_build(self):
964932
# Unpack belongs to the BuildArchive class
965933
unpack_start_time = time.time()
966934
build.unpack(self.build_dir, trusted=True)
967-
build_unpack_time = (time.time() - unpack_start_time) / 60
968-
# In minutes, as per metric definition.
969-
monitoring_metrics.JOB_BUILD_RETRIEVAL_TIME.add(
970-
build_unpack_time, {
971-
'job': os.getenv('JOB_NAME'),
972-
'platform': environment.platform(),
973-
'step': 'unpack',
974-
'build_type': self._build_type,
975-
})
935+
_emit_job_build_retrieval_metric(unpack_start_time, 'unpack',
936+
self._build_type)
976937
except:
977938
build.close()
978939
logs.error('Unable to unpack build archive %s.' % build_local_archive)
@@ -982,15 +943,8 @@ def _unpack_custom_build(self):
982943
# Remove the archive.
983944
shell.remove_file(build_local_archive)
984945

985-
total_retrieval_time = time.time() - download_start_time
986-
monitoring_metrics.JOB_BUILD_RETRIEVAL_TIME.add(
987-
total_retrieval_time, {
988-
'job': os.getenv('JOB_NAME'),
989-
'platform': environment.platform(),
990-
'step': 'total',
991-
'build_type': self._build_type,
992-
})
993-
946+
_emit_job_build_retrieval_metric(download_start_time, 'download',
947+
self._build_type)
994948
return True
995949

996950
def setup(self):

0 commit comments

Comments
 (0)