Add Prometheus metrics outside manage.py – docker related
My manage.py looks like this:
import os
import sys
from prometheus_client import start_http_server, Summary
import random
import time
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
s = Summary('request_latency_seconds', 'Description of summary')
s.observe(4.7) # Observe 4.7 (seconds in this case)
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
If I run my django app using docker, I am able to see added Summary metrics. However, if I try to run Django management command, created metric object will be not added to Prometheus.
I am basically trying to add many metrics the same way I did it with Summary, but I dont want my exposed port to be stopped (if I run loop inside manage.py, it will stop)… ideally, if I would be able to run management command (I will exec to running container, and run command like "python manage.py prometheus"), where I will be able to create objects such as here in manage.py, and they will be present in Prometheus. Maybe I will have to attach somehow to exposed port, because outside manage.py it does not work.
I am using this api: https://github.com/prometheus/client_python#summary
But I don’t find
start_http_server(8000)
very useful, as docker is already creating its own server.
Thank you, and sorry for confusion
Source: Docker Questions