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 4, 2024
1 parent 3f5cd2f commit 842b7df
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 86 deletions.
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
8 changes: 0 additions & 8 deletions acceptance_tests/tests/docker-compose.yaml
Original file line number Diff line number Diff line change
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))
132 changes: 66 additions & 66 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ prometheus-client = { version = "0.21.0", optional = true}
pyramid_mako = { version = "1.1.0", optional = true}
psutil = { version = "6.1.0", optional = true}
Paste = { version = "3.10.1", optional = true}
coverage = { version = "7.6.4", optional = true}

[tool.poetry.extras]
standard = [
Expand Down Expand Up @@ -123,7 +124,7 @@ standard = [
"Paste",
]
alembic = ["alembic"]
debug = ["objgraph", "psutil"]
debug = ["objgraph", "psutil", "coverage"]
oauth2 = ["pyjwt", "requests-oauthlib"]
sentry = ["sentry-sdk"]
dev = ["waitress"]
Expand Down Expand Up @@ -160,6 +161,7 @@ all = [
# debug
"objgraph",
"psutil",
"coverage",
# oauth2
"pyjwt",
"requests-oauthlib",
Expand Down Expand Up @@ -193,7 +195,6 @@ test_images = ["scikit-image"]
prospector = { version = "1.13.3", extras = ["with_bandit", "with_mypy", "with_pyroma", "with_ruff"] }
prospector-profile-duplicated = "1.9.0"
prospector-profile-utils = "1.14.0"
coverage = "7.6.8"
junit2html = "31.0.2"
pytest = "8.3.4"
pytest-cov = "6.0.0"
Expand Down

0 comments on commit 842b7df

Please sign in to comment.