-
Notifications
You must be signed in to change notification settings - Fork 2
fix: prometheus multproc dir permissions error #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a45278b
cc03e9a
2fbb7e4
74ca0e3
f3cadf4
709b05e
a2ef2a5
4a34e08
c5e9834
1402215
a127cc2
a64b30b
786e490
5b88f81
4bead79
0653e2c
85233a1
1aae629
6e741bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| DEFAULT_PROMETHEUS_MULTIPROC_DIR = "/tmp/flagsmith-prometheus" | ||
| DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME = "flagsmith-prometheus" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| from common.prometheus.utils import Histogram | ||
| from typing import Any | ||
|
|
||
| __all__ = ("Histogram",) | ||
| _utils = ("Histogram",) | ||
|
|
||
|
|
||
| def __getattr__(name: str) -> Any: | ||
| """ | ||
| Since utils imports django.conf.settings, we lazy load any objects that | ||
| we want to import to prevent django.core.exceptions.ImproperlyConfigured | ||
| due to settings not being configured. | ||
| """ | ||
| if name in _utils: | ||
| from common.prometheus import utils | ||
|
|
||
| return getattr(utils, name) | ||
| raise AttributeError(name) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't love that this code can't live in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps the more logical answer here would be to move |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import os | ||
| import pathlib | ||
| import shutil | ||
| from tempfile import gettempdir | ||
|
|
||
| from common.core.constants import DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME | ||
|
|
||
|
|
||
| def prepare_prom_multiproc_dir() -> None: | ||
| prom_dir = pathlib.Path( | ||
| os.environ.setdefault( | ||
| "PROMETHEUS_MULTIPROC_DIR", | ||
| os.path.join(gettempdir(), DEFAULT_PROMETHEUS_MULTIPROC_DIR_NAME), | ||
| ) | ||
| ) | ||
|
|
||
| if prom_dir.exists(): | ||
| for p in prom_dir.rglob("*"): | ||
| try: | ||
| # Ensure that the cleanup doesn't silently fail on | ||
| # files and subdirs created by other users. | ||
| p.chmod(0o777) | ||
| except Exception: # pragma: no cover | ||
| pass | ||
|
|
||
| shutil.rmtree(prom_dir, ignore_errors=True) | ||
|
|
||
| prom_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # While `mkdir` sets mode=0o777 by default, this can be affected by umask resulting in | ||
| # lesser permissions for other users. This step ensures the directory is writable for | ||
| # all users. | ||
| try: | ||
| prom_dir.chmod(0o777) | ||
| except PermissionError: | ||
| pass |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,7 +194,7 @@ def _run_task( | |
| "result": result.lower(), | ||
| } | ||
|
|
||
| timer.labels(**labels) # type: ignore[no-untyped-call] | ||
| timer.labels(**labels) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really know why mypy pulled me up on this in this PR given that I've not touched this file, or any related code... |
||
| ctx.close() | ||
|
|
||
| metrics.flagsmith_task_processor_finished_tasks_total.labels(**labels).inc() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty ugly, but might actually save us pain in the future, as well as fixing the pain I experienced. There are a couple of alternatives:
utils.py, use a factory instead of subclassingHistogram, and perform a local import ofdjango.conf.settingsprepare_prom_multiproc_dirinto generic functions where necessary likeclear_sub_dirs(dir: str), and just call it step by step inensure_cli_envThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've implemented option 2 in #120