Skip to content

Commit

Permalink
Only use multiprocess mode is env var is set.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed Jan 31, 2023
1 parent 02a42fd commit 1ac564a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
19 changes: 1 addition & 18 deletions tiled/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,7 @@ def construct_build_app_kwargs(
"response_bytesize_limit"
)
server_settings["database"] = config.get("database", {})
metrics = config.get("metrics", {})
if metrics.get("prometheus", False):
prometheus_multiproc_dir = os.getenv("PROMETHEUS_MULTIPROC_DIR", None)
if not prometheus_multiproc_dir:
raise ValueError(
"prometheus enabled but PROMETHEUS_MULTIPROC_DIR env variable not set"
)
elif not Path(prometheus_multiproc_dir).is_dir():
raise ValueError(
"prometheus enabled but PROMETHEUS_MULTIPROC_DIR "
f"({prometheus_multiproc_dir}) is not a directory"
)
elif not os.access(prometheus_multiproc_dir, os.W_OK):
raise ValueError(
"prometheus enabled but PROMETHEUS_MULTIPROC_DIR "
f"({prometheus_multiproc_dir}) is not writable"
)
server_settings["metrics"] = metrics
server_settings["metrics"] = config.get("metrics", {})
for structure_family, values in config.get("media_types", {}).items():
for media_type, import_path in values.items():
serializer = import_object(import_path, accept_live_object=True)
Expand Down
18 changes: 18 additions & 0 deletions tiled/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,24 @@ def override_get_validation_registry():

metrics_config = server_settings.get("metrics", {})
if metrics_config.get("prometheus", True):

# PROMETHEUS_MULTIRPOC_DIR puts prometheus_client in multiprocess mode
# (for e.g. gunicorn) which uses a directory of memory-mapped files.
# If that environment variable is set, check that the directory exists
# and is writable.
prometheus_multiproc_dir = os.getenv("PROMETHEUS_MULTIPROC_DIR", None)
if prometheus_multiproc_dir:
if not Path(prometheus_multiproc_dir).is_dir():
raise ValueError(
"prometheus enabled and PROMETHEUS_MULTIPROC_DIR is set but "
f"({prometheus_multiproc_dir}) is not a directory"
)
if not os.access(prometheus_multiproc_dir, os.W_OK):
raise ValueError(
"prometheus enabled and PROMETHEUS_MULTIPROC_DIR is set but "
f"({prometheus_multiproc_dir}) is not writable"
)

from . import metrics

app.include_router(metrics.router, prefix="/api/v1")
Expand Down
17 changes: 11 additions & 6 deletions tiled/server/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
`tiled_<noun>_<verb>_<type_suffix>`.
"""

import os
from functools import lru_cache

from fastapi import APIRouter, Request, Response, Security
Expand Down Expand Up @@ -140,13 +141,17 @@ def prometheus_registry():
This is run the first time the /metrics endpoint is used.
"""
# The multiprocess configuration makes it compatible with gunicorn.
# https://github.com/prometheus/client_python/#multiprocess-mode-eg-gunicorn
from prometheus_client import CollectorRegistry
from prometheus_client.multiprocess import MultiProcessCollector
if os.environ.get("PROMETHEUS_MULTIPROC_DIR"):
# The multiprocess configuration makes it compatible with gunicorn.
# https://github.com/prometheus/client_python/#multiprocess-mode-eg-gunicorn
from prometheus_client.multiprocess import MultiProcessCollector
from prometheus_client import CollectorRegistry

registry = CollectorRegistry()
MultiProcessCollector(registry) # This has a side effect.
else:
from prometheus_client import REGISTRY as registry

registry = CollectorRegistry()
MultiProcessCollector(registry) # This has a side effect, apparently.
return registry


Expand Down

0 comments on commit 1ac564a

Please sign in to comment.