Open elnappo opened 6 years ago
This has been reported previously, django_prometheus also starts when running other management commands like migrate
. I don't have a good solution, except maybe using the URL router instead of serving on a distinct port, but that's an all or nothing solution.
Suggestions welcome.
Hi - is there a known workaround? I make heavy use of management commands, and sometimes >1 are running at a time. It seems I can't use django-prometheus then? :(
You can set an environment variable when you're in a Django worker process. For instance with Gunicorn you can use the post_fork
hook to, say, set IS_GUNICORN_WORKER
environment variable, then in your Django settings you can set PROMETHEUS_METRICS_EXPORT_PORT[_RANGE]
only if this variable is set.
As a workaround, you can do this in your base settings.py
if config("PROMETHEUS_ENABLE", cast=bool, default=False):
# there was a bug here. when we import libs and then assign env variables, the metrics didnt store
PROMETHEUS_MULTIPROC_DIR = config("PROMETHEUS_MULTIPROC_DIR")
if not os.environ.get("PROMETHEUS_MULTIPROC_DIR"):
os.environ["PROMETHEUS_MULTIPROC_DIR"] = config("PROMETHEUS_MULTIPROC_DIR")
if not os.path.exists(PROMETHEUS_MULTIPROC_DIR):
os.makedirs(PROMETHEUS_MULTIPROC_DIR)
from prometheus_client.multiprocess import MultiProcessCollector
from prometheus_client import CollectorRegistry, start_http_server
INSTALLED_APPS += ["django_prometheus"]
MIDDLEWARE.insert(0, "django_prometheus.middleware.PrometheusBeforeMiddleware")
MIDDLEWARE.append("django_prometheus.middleware.PrometheusAfterMiddleware")
registry = CollectorRegistry()
MultiProcessCollector(registry)
try:
start_http_server(8001, registry=registry)
except OSError:
"""
first process serves metrics on port 8001, other processes raise error: port already in use
one processes collect metrics from PROMETHEUS_MULTIPROC_DIR
"""
and don't use PROMETHEUS_METRICS_EXPORT_PORT[_RANGE]
and PROMETHEUS_METRICS_EXPORT_ADDRESS
When you serve django with gunicorn, it uses multi processes to serve your application, each process runs django_prometheus/exports.py
prometheus_client.start_http_server(port, addr=addr)
so, we can't use this approach
I can't start a Django shell on a host which is currently running my Django project because
python manage.py shell
tries to start the metrics endpoint on port 8001.Prometheus thread should not be started in a Django shell environment.