Skip to content

Commit 638bb9e

Browse files
committed
fix: use lazy loading for DEFAULT_METRICS_LOG_PATH
1 parent 5f44166 commit 638bb9e

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

github-runner-manager/src/github_runner_manager/metrics/events.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
from github_runner_manager.errors import IssueMetricEventError
1414
from 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

1918
logger = 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))

github-runner-manager/tests/unit/metrics/test_events.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
@pytest.fixture(autouse=True, name="patch_metrics_path")
1414
def 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

2119
def 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",

0 commit comments

Comments
 (0)