File tree Expand file tree Collapse file tree
src/github_runner_manager/metrics Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1313from github_runner_manager .errors import IssueMetricEventError
1414from github_runner_manager .manager .vm_manager import CodeInformation
1515
16- METRICS_LOG_PATH = Path (os .getenv ("METRICS_LOG_PATH" , "/var/log/github-runner-metrics.log" ))
17-
16+ _DEFAULT_METRICS_LOG_PATH = "/var/log/github-runner-metrics.log"
1817
1918logger = logging .getLogger (__name__ )
2019
@@ -156,8 +155,14 @@ def issue_event(event: Event) -> None:
156155 Raises:
157156 IssueMetricEventError: If the event cannot be logged.
158157 """
158+ metrics_log_path = _get_metrics_log_path ()
159159 try :
160- with METRICS_LOG_PATH .open (mode = "a" , encoding = "utf-8" ) as metrics_file :
160+ with metrics_log_path .open (mode = "a" , encoding = "utf-8" ) as metrics_file :
161161 metrics_file .write (f"{ event .json (exclude_none = True )} \n " )
162162 except OSError as exc :
163- raise IssueMetricEventError (f"Cannot write to { METRICS_LOG_PATH } " ) from exc
163+ raise IssueMetricEventError (f"Cannot write to { metrics_log_path } " ) from exc
164+
165+
166+ def _get_metrics_log_path () -> Path :
167+ """Get the metrics log path, reading the env var at call time rather than import time."""
168+ return Path (os .getenv ("METRICS_LOG_PATH" , _DEFAULT_METRICS_LOG_PATH ))
Original file line number Diff line number Diff line change 1313@pytest .fixture (autouse = True , name = "patch_metrics_path" )
1414def patch_metrics_path_fixture (monkeypatch : pytest .MonkeyPatch , tmp_path : Path ):
1515 """Patch the hardcoded metrics log path."""
16- monkeypatch .setattr (
17- "github_runner_manager.metrics.events.METRICS_LOG_PATH" , Path (tmp_path / "metrics.log" )
18- )
16+ monkeypatch .setenv ("METRICS_LOG_PATH" , str (tmp_path / "metrics.log" ))
1917
2018
2119def test_issue_events_logs_events (tmp_path : Path ):
@@ -28,7 +26,7 @@ def test_issue_events_logs_events(tmp_path: Path):
2826
2927 events .issue_event (event )
3028
31- assert json .loads (events .METRICS_LOG_PATH .read_text ()) == {
29+ assert json .loads (events ._get_metrics_log_path () .read_text ()) == {
3230 "event" : "runner_installed" ,
3331 "timestamp" : 123 ,
3432 "flavor" : "small" ,
@@ -55,7 +53,7 @@ def test_issue_events_exclude_none_values(tmp_path: Path):
5553
5654 events .issue_event (event )
5755
58- assert json .loads (events .METRICS_LOG_PATH .read_text ()) == {
56+ assert json .loads (events ._get_metrics_log_path () .read_text ()) == {
5957 "event" : "runner_stop" ,
6058 "timestamp" : 123 ,
6159 "flavor" : "small" ,
You can’t perform that action at this time.
0 commit comments