Skip to content

Commit

Permalink
Record container id and type in core job metrics
Browse files Browse the repository at this point in the history
Closes galaxyproject#16588.
Not sure if we want to do this versus a separate but always loaded
plugin vs a regular relationship in the database.

Seems unlikely that we'd do anything beyond reporting, so maybe this is
fine for now ?
  • Loading branch information
mvdbeek committed Jul 25, 2024
1 parent 864bec4 commit 9587f59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/galaxy/job_metrics/instrumenters/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""The module describes the ``core`` job metrics plugin."""

import json
import logging
import os
import time
from typing import (
Any,
Expand All @@ -23,10 +25,17 @@
START_EPOCH_KEY = "start_epoch"
END_EPOCH_KEY = "end_epoch"
RUNTIME_SECONDS_KEY = "runtime_seconds"
CONTAINER_FILE = "__container.json"
CONTAINER_ID = "container_id"
CONTAINER_TYPE = "container_type"


class CorePluginFormatter(JobMetricFormatter):
def format(self, key: str, value: Any) -> FormattedMetric:
if key == CONTAINER_ID:
return FormattedMetric("Container ID", value)
if key == CONTAINER_TYPE:
return FormattedMetric("Container Type", value)
value = int(value)
if key == GALAXY_SLOTS_KEY:
return FormattedMetric("Cores Allocated", "%d" % value)
Expand Down Expand Up @@ -73,12 +82,20 @@ def job_properties(self, job_id, job_directory: str) -> Dict[str, Any]:
properties[GALAXY_MEMORY_MB_KEY] = self.__read_integer(galaxy_memory_mb_file)
start = self.__read_seconds_since_epoch(job_directory, "start")
end = self.__read_seconds_since_epoch(job_directory, "end")
properties.update(self.__read_container_details(job_directory))
if start is not None and end is not None:
properties[START_EPOCH_KEY] = start
properties[END_EPOCH_KEY] = end
properties[RUNTIME_SECONDS_KEY] = end - start
return properties

def __read_container_details(self, job_directory) -> Dict[str, str]:
try:
with open(os.path.join(job_directory, CONTAINER_FILE)) as fh:
return json.load(fh)
except FileNotFoundError:
return {}

def __record_galaxy_slots_command(self, job_directory):
galaxy_slots_file = self.__galaxy_slots_file(job_directory)
return f"""echo "$GALAXY_SLOTS" > '{galaxy_slots_file}' """
Expand Down
4 changes: 4 additions & 0 deletions lib/galaxy/jobs/command_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def __externalize_commands(
source_command = ""
if container:
source_command = container.source_environment
with open(join(job_wrapper.working_directory, "__container.json"), "w") as container_file:
container_file.write(
json.dumps({"container_id": container.container_id, "container_type": container.container_type})
)
script_contents = f"#!{shell}\n{integrity_injection}{set_e}{source_command}{tool_commands}"
write_script(
local_container_script,
Expand Down

0 comments on commit 9587f59

Please sign in to comment.