From e08f40fa437cbc3a5212fc500b508041d1a38358 Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Wed, 24 Jul 2024 11:00:38 +1000 Subject: [PATCH] Update location to jenkins logs so it's together with the others (#159) * Update location to jenkins logs so it's together with the others Other logs such as tasks, slaves/agents, health-checker, and sse-events. * add debug log for loki * logging fixes --------- Co-authored-by: Yanks Yoon <37652070+yanksyoon@users.noreply.github.com> Co-authored-by: arturo-seijas <102022572+arturo-seijas@users.noreply.github.com> Co-authored-by: Yanks Yoon --- docs/reference/integrations.md | 2 +- src-docs/jenkins.py.md | 25 ++++++++++++++++++++++--- src/jenkins.py | 13 +++++++++++-- src/pebble.py | 1 + templates/logging.properties | 2 +- tests/integration/test_cos.py | 6 +++++- tests/unit/test_charm.py | 2 +- tests/unit/test_jenkins.py | 12 ++++++------ tests/unit/test_pebble.py | 2 +- 9 files changed, 49 insertions(+), 16 deletions(-) diff --git a/docs/reference/integrations.md b/docs/reference/integrations.md index 156b2e93..552724c0 100644 --- a/docs/reference/integrations.md +++ b/docs/reference/integrations.md @@ -32,7 +32,7 @@ _Interface_: loki_push_api _Supported charms_: [loki-k8s](https://charmhub.io/loki-k8s) Logging relation provides a way to scrape logs produced from the Jenkins server charm. The Jenkins -server logs are stored at `/var/lib/jenkins/jenkins.log`. These logs are the same logs as the logs +server logs are stored at `/var/lib/jenkins/logs/jenkins.log`. These logs are the same logs as the logs emitted to the standard output. A promtail worker is spawned and will periodically push logs to Loki. diff --git a/src-docs/jenkins.py.md b/src-docs/jenkins.py.md index 403ff2ae..fb02817e 100644 --- a/src-docs/jenkins.py.md +++ b/src-docs/jenkins.py.md @@ -84,7 +84,7 @@ Return whether the Jenkins home directory is mounted and owned by jenkins. --- - + ## function `install_default_config` @@ -103,7 +103,7 @@ Install default jenkins-config.xml. --- - + ## function `install_auth_proxy_config` @@ -122,7 +122,26 @@ Install jenkins-config.xml for auth_proxy. --- - + + +## function `install_logging_config` + +```python +install_logging_config(container: Container) → None +``` + +Install logging config. + + + +**Args:** + + - `container`: The Jenkins workload container. + + +--- + + ## function `get_agent_name` diff --git a/src/jenkins.py b/src/jenkins.py index cb42c216..e949eeff 100644 --- a/src/jenkins.py +++ b/src/jenkins.py @@ -50,7 +50,7 @@ # The Jenkins logging configuration path LOGGING_CONFIG_PATH = JENKINS_HOME_PATH / "logging.properties" # The Jenkins logging path as defined in templates/logging.properties file -LOGGING_PATH = JENKINS_HOME_PATH / "jenkins.log" +LOGGING_PATH = JENKINS_HOME_PATH / "logs/jenkins.log" # The plugins that are required for Jenkins to work REQUIRED_PLUGINS = [ "instance-identity", # required to connect agent nodes to server @@ -740,7 +740,6 @@ def _install_configs(container: ops.Container, jenkins_config_file: str) -> None jenkins_config_file: the path to the Jenkins configuration file to install. """ _install_config(container, jenkins_config_file, CONFIG_FILE_PATH) - _install_config(container, JENKINS_LOGGING_CONFIG, LOGGING_CONFIG_PATH) def install_default_config(container: ops.Container) -> None: @@ -761,6 +760,16 @@ def install_auth_proxy_config(container: ops.Container) -> None: _install_config(container, AUTH_PROXY_JENKINS_CONFIG, CONFIG_FILE_PATH) +def install_logging_config(container: ops.Container) -> None: + """Install logging config. + + Args: + container: The Jenkins workload container. + """ + container.make_dir(LOGGING_PATH.parent, make_parents=True, user=USER, group=GROUP) + _install_config(container, JENKINS_LOGGING_CONFIG, LOGGING_CONFIG_PATH) + + def _get_groovy_proxy_args(proxy_config: state.ProxyConfig) -> typing.Iterable[str]: """Get proxy arguments for proxy configuration Groovy script. diff --git a/src/pebble.py b/src/pebble.py index 4e2d7eb9..fca5ca25 100644 --- a/src/pebble.py +++ b/src/pebble.py @@ -34,6 +34,7 @@ def replan_jenkins( Raises: JenkinsBootstrapError: if an error occurs while bootstrapping Jenkins. """ + jenkins.install_logging_config(container=container) container.add_layer("jenkins", _get_pebble_layer(jenkins_instance), combine=True) container.replan() try: diff --git a/templates/logging.properties b/templates/logging.properties index a20d3880..ad242040 100644 --- a/templates/logging.properties +++ b/templates/logging.properties @@ -32,7 +32,7 @@ handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler ############################################################ # default file output is in user's home directory. -java.util.logging.FileHandler.pattern = /var/lib/jenkins/jenkins.log +java.util.logging.FileHandler.pattern = /var/lib/jenkins/logs/jenkins.log java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.count = 1 java.util.logging.FileHandler.append=true diff --git a/tests/integration/test_cos.py b/tests/integration/test_cos.py index 463a52fc..c3ca966e 100644 --- a/tests/integration/test_cos.py +++ b/tests/integration/test_cos.py @@ -4,6 +4,7 @@ """Integration tests for jenkins-k8s-operator with COS.""" import functools +import logging import typing import pytest @@ -17,6 +18,8 @@ from .helpers import wait_for from .types_ import UnitWebClient +logger = logging.getLogger(__name__) + @pytest.mark.abort_on_fail async def test_prometheus_integration( @@ -56,6 +59,7 @@ def log_files_exist( """ series = requests.get(f"http://{unit_address}:3100/loki/api/v1/series", timeout=10).json() log_files = set(series_data["filename"] for series_data in series["data"]) + logger.info("Loki log files: %s", log_files) if not all(filename in log_files for filename in filenames): return False log_query = requests.get( @@ -88,7 +92,7 @@ async def test_loki_integration( log_files_exist, unit.address, application.name, - ("/var/lib/jenkins/jenkins.log",), + ("/var/lib/jenkins/logs/jenkins.log",), ), timeout=10 * 60, ) diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index e97e1a59..6b3812b3 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -96,7 +96,7 @@ def test_storage_not_ready(harness: Harness, event_handler: str): def test__on_jenkins_pebble_ready_get_version_error( harness_container: HarnessWithContainer, - mocked_get_request: typing.Callable[[str, int, typing.Any, typing.Any], requests.Response], + mocked_get_request: typing.Callable[..., requests.Response], monkeypatch: pytest.MonkeyPatch, ): """ diff --git a/tests/unit/test_jenkins.py b/tests/unit/test_jenkins.py index a53eafb9..357f9adc 100644 --- a/tests/unit/test_jenkins.py +++ b/tests/unit/test_jenkins.py @@ -49,7 +49,7 @@ def test__is_ready_connection_exception( ) def test__is_ready( monkeypatch: pytest.MonkeyPatch, - mocked_get_request: typing.Callable[[str, int, typing.Any, typing.Any], requests.Response], + mocked_get_request: typing.Callable[..., requests.Response], status_code: int, expected_ready: bool, mock_env: jenkins.Environment, @@ -66,7 +66,7 @@ def test__is_ready( def test_wait_ready_timeout( monkeypatch: pytest.MonkeyPatch, - mocked_get_request: typing.Callable[[str, int, typing.Any, typing.Any], requests.Response], + mocked_get_request: typing.Callable[..., requests.Response], mock_env: jenkins.Environment, ): """ @@ -113,7 +113,7 @@ def __init__(self, *_args, **_kwargs) -> None: def test_wait_ready( monkeypatch: pytest.MonkeyPatch, - mocked_get_request: typing.Callable[[str, int, typing.Any, typing.Any], requests.Response], + mocked_get_request: typing.Callable[..., requests.Response], mock_env: jenkins.Environment, ): """ @@ -356,7 +356,7 @@ def test_version_error( def test_version( monkeypatch: pytest.MonkeyPatch, - mocked_get_request: typing.Callable[[str, int, typing.Any, typing.Any], requests.Response], + mocked_get_request: typing.Callable[..., requests.Response], jenkins_version: str, mock_env: jenkins.Environment, ): @@ -372,7 +372,7 @@ def test_version( def test__unlock_wizard( harness_container: HarnessWithContainer, - mocked_get_request: typing.Callable[[str, int, typing.Any, typing.Any], requests.Response], + mocked_get_request: typing.Callable[..., requests.Response], monkeypatch: pytest.MonkeyPatch, jenkins_version: str, mock_env: jenkins.Environment, @@ -397,7 +397,7 @@ def test__unlock_wizard( def test__unlock_wizard_raises_exception( - mocked_get_request: typing.Callable[[str, int, typing.Any, typing.Any], requests.Response], + mocked_get_request: typing.Callable[..., requests.Response], monkeypatch: pytest.MonkeyPatch, mock_env: jenkins.Environment, ): diff --git a/tests/unit/test_pebble.py b/tests/unit/test_pebble.py index 3d010c20..013bbb07 100644 --- a/tests/unit/test_pebble.py +++ b/tests/unit/test_pebble.py @@ -19,7 +19,7 @@ def test_replan_jenkins_pebble_error( harness_container: HarnessWithContainer, - mocked_get_request: typing.Callable[[str, int, typing.Any, typing.Any], requests.Response], + mocked_get_request: typing.Callable[..., requests.Response], monkeypatch: pytest.MonkeyPatch, ): """