artifacts.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import gzip
  2. import json
  3. import os
  4. from ray_release.config import Test
  5. from ray_release.logger import logger
  6. from ray_release.reporter.reporter import Reporter
  7. from ray_release.result import Result
  8. # Write to this directory. run_release_tests.sh will copy the content
  9. # overt to DEFAULT_ARTIFACTS_DIR_HOST
  10. DEFAULT_ARTIFACTS_DIR = "/tmp/artifacts"
  11. ARTIFACT_TEST_CONFIG_FILE = "test_config.json"
  12. ARTIFACT_RESULT_FILE = "result.json"
  13. METRICS_RESULT_FILE = "metrics.json.gz"
  14. class ArtifactsReporter(Reporter):
  15. """This is called on on buildkite runners."""
  16. def __init__(self, artifacts_dir: str = DEFAULT_ARTIFACTS_DIR):
  17. self.artifacts_dir = artifacts_dir
  18. def report_result(self, test: Test, result: Result):
  19. if not os.path.exists(self.artifacts_dir):
  20. os.makedirs(self.artifacts_dir, 0o755)
  21. test_config_file = os.path.join(self.artifacts_dir, ARTIFACT_TEST_CONFIG_FILE)
  22. with open(test_config_file, "wt") as fp:
  23. json.dump(test, fp, sort_keys=True, indent=4)
  24. result_file = os.path.join(self.artifacts_dir, ARTIFACT_RESULT_FILE)
  25. result_dict = result.__dict__
  26. metrics_dict = result_dict.pop("prometheus_metrics")
  27. with open(result_file, "wt") as fp:
  28. json.dump(result_dict, fp, sort_keys=True, indent=4)
  29. logger.info(
  30. f"Wrote test config and result to artifacts directory: {self.artifacts_dir}"
  31. )
  32. if metrics_dict:
  33. metrics_file = os.path.join(self.artifacts_dir, METRICS_RESULT_FILE)
  34. with gzip.open(metrics_file, "wt", encoding="UTF-8") as fp:
  35. json.dump(metrics_dict, fp, sort_keys=True, indent=4)
  36. logger.info(
  37. f"Wrote prometheus metrics to artifacts directory: {self.artifacts_dir}"
  38. )