Skip to content

Commit

Permalink
Merge pull request #98 from qbiqing/master
Browse files Browse the repository at this point in the history
Update env var to `PROMETHEUS_MULTIPROC_DIR`
  • Loading branch information
rycus86 committed Apr 14, 2021
2 parents 800fa6f + 3c2050a commit 62e8364
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ This should return `True` on one process only, and the underlying
[Prometheus client library](https://github.com/prometheus/client_python)
will collect the metrics for all the forked children or siblings.

__Note:__ this needs the `prometheus_multiproc_dir` environment variable
__Note:__ this needs the `PROMETHEUS_MULTIPROC_DIR` environment variable
to point to a valid, writable directory.

You'll also have to call the `metrics.start_http_server()` function
Expand Down
1 change: 1 addition & 0 deletions examples/gunicorn-app-factory/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ADD examples/gunicorn-app-factory/server.py \
/var/flask/
WORKDIR /var/flask

ENV PROMETHEUS_MULTIPROC_DIR /tmp
ENV prometheus_multiproc_dir /tmp

CMD gunicorn -c config.py -w 4 -b 0.0.0.0:4000 server:app
1 change: 1 addition & 0 deletions examples/gunicorn-internal/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN pip install -e /tmp/latest --upgrade
ADD examples/gunicorn-internal/server.py examples/gunicorn-internal/config.py /var/flask/
WORKDIR /var/flask

ENV PROMETHEUS_MULTIPROC_DIR /tmp
ENV prometheus_multiproc_dir /tmp

CMD gunicorn -c config.py -w 4 -b 0.0.0.0:4000 server:app
1 change: 1 addition & 0 deletions examples/gunicorn/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN pip install -e /tmp/latest --upgrade
ADD examples/gunicorn/server.py examples/gunicorn/config.py /var/flask/
WORKDIR /var/flask

ENV PROMETHEUS_MULTIPROC_DIR /tmp
ENV prometheus_multiproc_dir /tmp
ENV METRICS_PORT 9200

Expand Down
3 changes: 2 additions & 1 deletion examples/pytest-app-factory/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ RUN pip install flask pytest
ADD . /tmp/latest
RUN pip install -e /tmp/latest --upgrade

ENV prometheus_multiproc_dir=/tmp
ENV PROMETHEUS_MULTIPROC_DIR /tmp
ENV prometheus_multiproc_dir /tmp
ENV PYTHONPATH=/data

ADD ./examples/pytest-app-factory /data
Expand Down
3 changes: 2 additions & 1 deletion examples/uwsgi-connexion/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ RUN pip install -e /tmp/latest --upgrade
ADD examples/uwsgi-connexion /var/flask
WORKDIR /var/flask

ENV PROMETHEUS_MULTIPROC_DIR /tmp
ENV prometheus_multiproc_dir /tmp
ENV METRICS_PORT 9200

CMD uwsgi --http 0.0.0.0:4000 --module main:app --master --processes 4 --threads 2
CMD uwsgi --http 0.0.0.0:4000 --module main:app --master --processes 4 --threads 2
1 change: 1 addition & 0 deletions examples/uwsgi-lazy-apps/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN pip install -e /tmp/latest --upgrade
ADD examples/uwsgi-lazy-apps/server.py /var/flask/
WORKDIR /var/flask

ENV PROMETHEUS_MULTIPROC_DIR /tmp
ENV prometheus_multiproc_dir /tmp

CMD uwsgi --http 0.0.0.0:4000 --module server:app --master --processes 4 --threads 2 --lazy-apps
1 change: 1 addition & 0 deletions examples/uwsgi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN pip install -e /tmp/latest --upgrade
ADD examples/uwsgi/server.py /var/flask/
WORKDIR /var/flask

ENV PROMETHEUS_MULTIPROC_DIR /tmp
ENV prometheus_multiproc_dir /tmp
ENV METRICS_PORT 9200

Expand Down
3 changes: 2 additions & 1 deletion examples/wsgi/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

sys.path.insert(0, os.path.dirname(__file__))

os.environ["PROMETHEUS_MULTIPROC_DIR"] = "/tmp"
os.environ["prometheus_multiproc_dir"] = "/tmp"

from app import app as application
from app import app as application
4 changes: 2 additions & 2 deletions prometheus_flask_exporter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ def prometheus_metrics():
# import these here so they don't clash with our own multiprocess module
from prometheus_client import multiprocess, CollectorRegistry

if 'prometheus_multiproc_dir' in os.environ:
if 'PROMETHEUS_MULTIPROC_DIR' in os.environ or 'prometheus_multiproc_dir' in os.environ:
registry = CollectorRegistry()
else:
registry = self.registry

if 'name[]' in request.args:
registry = registry.restricted_registry(request.args.getlist('name[]'))

if 'prometheus_multiproc_dir' in os.environ:
if 'PROMETHEUS_MULTIPROC_DIR' in os.environ or 'prometheus_multiproc_dir' in os.environ:
multiprocess.MultiProcessCollector(registry)

generate_latest, content_type = choose_encoder(request.headers.get("Accept"))
Expand Down
10 changes: 7 additions & 3 deletions prometheus_flask_exporter/multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@

def _check_multiproc_env_var():
"""
Checks that the `prometheus_multiproc_dir` environment variable is set,
Checks that the `PROMETHEUS_MULTIPROC_DIR` environment variable is set,
which is required for the multiprocess collector to work properly.
:raises ValueError: if the environment variable is not set
or if it does not point to a directory
"""

if 'prometheus_multiproc_dir' in os.environ:
if 'PROMETHEUS_MULTIPROC_DIR' in os.environ:
if os.path.isdir(os.environ['PROMETHEUS_MULTIPROC_DIR']):
return
elif 'prometheus_multiproc_dir' in os.environ:
if os.path.isdir(os.environ['prometheus_multiproc_dir']):
return

raise ValueError('env prometheus_multiproc_dir is not set or not a directory')
raise ValueError('one of env PROMETHEUS_MULTIPROC_DIR or env prometheus_multiproc_dir' +
'must be set and be a directory')


class MultiprocessPrometheusMetrics(PrometheusMetrics):
Expand Down
48 changes: 46 additions & 2 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class ExtensionsTest(BaseTestCase):
def setUp(self):
super(ExtensionsTest, self).setUp()

if 'prometheus_multiproc_dir' not in os.environ:
if 'PROMETHEUS_MULTIPROC_DIR' not in os.environ:
os.environ['PROMETHEUS_MULTIPROC_DIR'] = '/tmp'
self._multiproc_dir_added = True
elif 'prometheus_multiproc_dir' not in os.environ:
os.environ['prometheus_multiproc_dir'] = '/tmp'
self._multiproc_dir_added = True
else:
Expand All @@ -41,7 +44,10 @@ def setUp(self):

def tearDown(self):
if self._multiproc_dir_added:
del os.environ['prometheus_multiproc_dir']
if os.environ.get('PROMETHEUS_MULTIPROC_DIR'):
del os.environ['PROMETHEUS_MULTIPROC_DIR']
if os.environ.get('prometheus_multiproc_dir'):
del os.environ['prometheus_multiproc_dir']

def test_with_defaults(self):
for extension_type in self._all_extensions:
Expand Down Expand Up @@ -114,3 +120,41 @@ def test_with_other_parameters(self):
else:
self.assertIs(getattr(obj, arg), value,
'Unexpected %s object in %s' % (arg, extension_type.__name__))

def test_prometheus_multiproc_env_var_change(self):
for extension_type in self._all_extensions:
if extension_type in [
MultiprocessPrometheusMetrics, UWsgiPrometheusMetrics,
GunicornPrometheusMetrics, GunicornInternalPrometheusMetrics
]:
# Check only lower case env var works
if os.environ.get('PROMETHEUS_MULTIPROC_DIR'):
del os.environ['PROMETHEUS_MULTIPROC_DIR']
os.environ['prometheus_multiproc_dir'] = '/tmp'

try:
app = Flask(__name__)
app.testing = True
flask_app = app

obj = extension_type(app=app)
except Exception as ex:
self.fail('Failed to instantiate %s: %s' % (extension_type.__name__, ex))

self.assertIs(obj.app, flask_app, 'Unexpected app object in %s' % extension_type.__name__)

# Check only upper case env var works
if os.environ.get('prometheus_multiproc_dir'):
del os.environ['prometheus_multiproc_dir']
os.environ['PROMETHEUS_MULTIPROC_DIR'] = '/tmp'

try:
app2 = Flask(__name__)
app2.testing = True
flask_app2 = app2

obj2 = extension_type(app=app2)
except Exception as ex:
self.fail('Failed to instantiate %s: %s' % (extension_type.__name__, ex))

self.assertIs(obj2.app, flask_app2, 'Unexpected app object in %s' % extension_type.__name__)

0 comments on commit 62e8364

Please sign in to comment.