Skip to content

Commit

Permalink
Fix logs in process commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Dec 11, 2024
1 parent 3f5cd2f commit ae5c437
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def hello_get(request):

In production mode we usually use Gunicorn but we can also use Waitress.

The advantage to use Waitress it that he creates only one process, that makes it easier to manage especially on Kubernetes:
The advantage to use Waitress is that it creates only one process, that makes it easier to manage especially on Kubernetes:

- The memory is more stable.
- The OOM killer will restart the container.
Expand Down
1 change: 0 additions & 1 deletion acceptance_tests/gunicorn_app/app_alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def run_migrations_online():
context.run_migrations()


c2cwsgiutils.setup_process.bootstrap_application()
if context.is_offline_mode():
run_migrations_offline()
else:
Expand Down
1 change: 1 addition & 0 deletions acceptance_tests/gunicorn_app/c2cwsgiutils_app/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import prometheus_client
import requests
import sqlalchemy.sql.expression
from pyramid.httpexceptions import (
HTTPBadRequest,
HTTPForbidden,
Expand Down
10 changes: 1 addition & 9 deletions acceptance_tests/tests/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ services:
app2:
<<: *app
image: camptocamp/c2cwsgiutils_test_app_waitress
# Same as app but with 2 workers (and different Redis DB 2, broadcast_prefix, ports, and JSON log format)
# Same as app but with Waitress (and different Redis DB 2, broadcast_prefix, ports, and JSON log format)
environment:
- SQLALCHEMY_URL
- SQLALCHEMY_SLAVE_URL
Expand Down Expand Up @@ -88,10 +88,6 @@ services:
- C2CWSGIUTILS_LOG_LEVEL=DEBUG
- SQL_LOG_LEVEL=DEBUG
- OTHER_LOG_LEVEL=INFO
- SENTRY_URL=https://[email protected]/5
- SENTRY_CLIENT_ENVIRONMENT=local
- SENTRY_CLIENT_RELEASE=latest
- SENTRY_TAG_SERVICE=alembic_master
links:
- db
command:
Expand All @@ -105,10 +101,6 @@ services:
- C2CWSGIUTILS_LOG_LEVEL=DEBUG
- SQL_LOG_LEVEL=DEBUG
- OTHER_LOG_LEVEL=INFO
- SENTRY_URL=https://[email protected]/5
- SENTRY_CLIENT_ENVIRONMENT=local
- SENTRY_CLIENT_RELEASE=latest
- SENTRY_TAG_SERVICE=alembic_slave
links:
- db_slave:db
command:
Expand Down
12 changes: 6 additions & 6 deletions acceptance_tests/tests/tests/test_prometheus_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@


def test_prometheus_1(prometheus_1_connection):
# One for the root process, one for each workers
# One for the root process, one for each workers (1)
assert (
len(set(_PID_RE.findall(prometheus_1_connection.get("metrics", cache_expected=False, cors=False))))
== 3
== 2
)


def test_prometheus_2(prometheus_2_connection):
# One for the root process, one for each workers
assert (
len(set(_PID_RE.findall(prometheus_2_connection.get("metrics", cache_expected=False, cors=False))))
== 3
)
metrics = prometheus_2_connection.get("metrics", cache_expected=False, cors=False)
assert len(set(_PID_RE.findall(metrics))) == 0
assert re.search(r"^c2cwsgiutils_python_resource\{.*", metrics, re.MULTILINE) is not None
assert re.search(r"^c2cwsgiutils_python_memory_info\{.*", metrics, re.MULTILINE) is not None
1 change: 0 additions & 1 deletion acceptance_tests/waitress_app/app_alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def run_migrations_online():
context.run_migrations()


c2cwsgiutils.setup_process.bootstrap_application()
if context.is_offline_mode():
run_migrations_offline()
else:
Expand Down
1 change: 1 addition & 0 deletions acceptance_tests/waitress_app/c2cwsgiutils_app/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import prometheus_client
import requests
import sqlalchemy.sql.expression
from pyramid.httpexceptions import (
HTTPBadRequest,
HTTPForbidden,
Expand Down
22 changes: 20 additions & 2 deletions c2cwsgiutils/loader.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
import logging.config
from typing import Optional, cast

from plaster_pastedeploy import Loader as BaseLoader

from c2cwsgiutils import get_config_defaults
from c2cwsgiutils import get_config_defaults, get_logconfig_dict

_LOG = logging.getLogger(__name__)

Expand All @@ -19,3 +19,21 @@ def _get_defaults(self, defaults: Optional[dict[str, str]] = None) -> dict[str,
def __repr__(self) -> str:
"""Get the object representation."""
return f'c2cwsgiutils.loader.Loader(uri="{self.uri}")'

def setup_logging(self, defaults: Optional[dict[str, str]] = None) -> None:
"""
Set up logging via :func:`logging.config.fileConfig`.
Defaults are specified for the special ``__file__`` and ``here``
variables, similar to PasteDeploy config loading. Extra defaults can
optionally be specified as a dict in ``defaults``.
Arguments:
defaults: The defaults that will be used when passed to
:func:`logging.config.fileConfig`.
"""
if "loggers" in self.get_sections():
logging.config.dictConfig(get_logconfig_dict(self.uri.path))
else:
logging.basicConfig()
2 changes: 2 additions & 0 deletions c2cwsgiutils/setup_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import argparse
import logging
import warnings
from typing import Any, Callable, Optional, TypedDict, cast

Expand Down Expand Up @@ -97,4 +98,5 @@ def bootstrap_application(
"""
loader = get_config_loader(config_uri)
loader.setup_logging(options)
logging.getLogger(__name__).info("Loading the application from %s", config_uri)
return cast(PyramidEnv, bootstrap(config_uri, options=options))
Loading

0 comments on commit ae5c437

Please sign in to comment.