Skip to content

Commit 0653e2c

Browse files
committed
Fix issue with settings being accessed at import time
1 parent 4bead79 commit 0653e2c

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

src/common/core/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010

1111
from common.core.cli import healthcheck
12-
from common.prometheus.utils import prepare_prom_multiproc_dir
12+
from common.prometheus.multiprocessing import prepare_prom_multiproc_dir
1313

1414
logger = logging.getLogger(__name__)
1515

src/common/prometheus/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
from common.prometheus.utils import Histogram
1+
from typing import Any
22

3-
__all__ = ("Histogram",)
3+
_utils = ("Histogram",)
4+
5+
6+
def __getattr__(name: str) -> Any:
7+
"""
8+
Since utils imports settings, we lazy load any objects that we want to import to
9+
prevent Django's settings-at-import-time trap
10+
"""
11+
if name in _utils:
12+
from common.prometheus import utils
13+
14+
return getattr(utils, name)
15+
raise AttributeError(name)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
import pathlib
3+
import shutil
4+
from tempfile import gettempdir
5+
6+
from common.core.constants import DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME
7+
8+
9+
def prepare_prom_multiproc_dir() -> None:
10+
prom_dir = pathlib.Path(
11+
os.environ.setdefault(
12+
"PROMETHEUS_MULTIPROC_DIR",
13+
os.path.join(gettempdir(), DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME),
14+
)
15+
)
16+
17+
if prom_dir.exists():
18+
for p in prom_dir.rglob("*"):
19+
try:
20+
# Ensure that the cleanup doesn't silently fail on
21+
# files and subdirs created by other users.
22+
p.chmod(0o777)
23+
except Exception: # pragma: no cover
24+
pass
25+
26+
shutil.rmtree(prom_dir, ignore_errors=True)
27+
28+
prom_dir.mkdir(parents=True, exist_ok=True)
29+
30+
# While `mkdir` sets mode=0o777 by default, this can be affected by umask resulting in
31+
# lesser permissions for other users. This step ensures the directory is writable for
32+
# all users.
33+
prom_dir.chmod(0o777)

src/common/prometheus/utils.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import importlib
2-
import os
3-
import pathlib
4-
import shutil
5-
from tempfile import gettempdir
62

73
import prometheus_client
84
from django.conf import settings
95
from prometheus_client.metrics import MetricWrapperBase
106
from prometheus_client.multiprocess import MultiProcessCollector
117

12-
from common.core.constants import DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME
13-
148

159
class Histogram(prometheus_client.Histogram):
1610
DEFAULT_BUCKETS = settings.PROMETHEUS_HISTOGRAM_BUCKETS
@@ -42,30 +36,3 @@ def reload_metrics(*metric_module_names: str) -> None:
4236
registry.unregister(module_attr)
4337

4438
importlib.reload(metrics_module)
45-
46-
47-
def prepare_prom_multiproc_dir() -> None:
48-
prom_dir = pathlib.Path(
49-
os.environ.setdefault(
50-
"PROMETHEUS_MULTIPROC_DIR",
51-
os.path.join(gettempdir(), DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME),
52-
)
53-
)
54-
55-
if prom_dir.exists():
56-
for p in prom_dir.rglob("*"):
57-
try:
58-
# Ensure that the cleanup doesn't silently fail on
59-
# files and subdirs created by other users.
60-
p.chmod(0o777)
61-
except Exception: # pragma: no cover
62-
pass
63-
64-
shutil.rmtree(prom_dir, ignore_errors=True)
65-
66-
prom_dir.mkdir(parents=True, exist_ok=True)
67-
68-
# While `mkdir` sets mode=0o777 by default, this can be affected by umask resulting in
69-
# lesser permissions for other users. This step ensures the directory is writable for
70-
# all users.
71-
prom_dir.chmod(0o777)

0 commit comments

Comments
 (0)