From 40f11edaea75e76c64170bd6d93166e98ff3dc00 Mon Sep 17 00:00:00 2001 From: eonedar Date: Mon, 29 Sep 2025 12:04:39 +0100 Subject: [PATCH 1/8] Change Metrics Namespace when using renaming script consolidated APP_NAME replacement in python code to single place in config.py Removed unused total_requests counter Signed-off-by: darren.oneill@ericsson.com Signed-off-by: don2112e --- eric-oss-hello-world-python-app/config.py | 1 + eric-oss-hello-world-python-app/main.py | 7 ++++--- eric-oss-hello-world-python-app/mtls_logging.py | 2 +- eric-oss-hello-world-python-app/tests/test_main.py | 4 ++-- eric-oss-hello-world-python-app/tests/test_mtls_logging.py | 3 ++- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/eric-oss-hello-world-python-app/config.py b/eric-oss-hello-world-python-app/config.py index 603fa5e..2d9e459 100644 --- a/eric-oss-hello-world-python-app/config.py +++ b/eric-oss-hello-world-python-app/config.py @@ -31,6 +31,7 @@ def get_config(): "app_cert_file_path": app_cert_file_path, "client_creds_file_path": client_creds_file_path, "client_id_file_name": client_id_file_name, + "chosen_name": "eric-oss-hello-world-python-app", } return config diff --git a/eric-oss-hello-world-python-app/main.py b/eric-oss-hello-world-python-app/main.py index eda2a0d..8d3ab16 100755 --- a/eric-oss-hello-world-python-app/main.py +++ b/eric-oss-hello-world-python-app/main.py @@ -9,6 +9,7 @@ from flask import abort from flask import Flask from login import login +from config import get_config from mtls_logging import MtlsLogging, Severity from werkzeug.middleware.dispatcher import DispatcherMiddleware from prometheus_client import ( @@ -18,14 +19,14 @@ Counter, ) -SERVICE_PREFIX = "python_hello_world" +METRICS_NAMESPACE = get_config().get("chosen_name").replace("-", "_") class Application(Flask): """The Flask application itself. Subclassed for testing.""" def __init__(self): super().__init__(__name__) disable_created_metrics() - self.counters = {"total_requests": 0} + self.counters = {} self.session = {"token": None, "expiry_time": 0} self.create_metrics() self.wsgi_app = DispatcherMiddleware( @@ -78,7 +79,7 @@ def update_session(self): def create_metrics(self): self.registry = CollectorRegistry() self.requests_total = Counter( - namespace=SERVICE_PREFIX, + namespace=METRICS_NAMESPACE, name="requests_total", documentation="Total number of API requests", ) diff --git a/eric-oss-hello-world-python-app/mtls_logging.py b/eric-oss-hello-world-python-app/mtls_logging.py index 47fc012..ecdb9dc 100644 --- a/eric-oss-hello-world-python-app/mtls_logging.py +++ b/eric-oss-hello-world-python-app/mtls_logging.py @@ -74,7 +74,7 @@ def log(self, message, severity): "timestamp": time, "version": "0.0.1", "message": message, - "service_id": "rapp-eric-oss-hello-world-python-app", + "service_id": "rapp-" + self.config.get("chosen_name"), "severity": severity.name.lower(), } diff --git a/eric-oss-hello-world-python-app/tests/test_main.py b/eric-oss-hello-world-python-app/tests/test_main.py index 177b883..a4a16f6 100644 --- a/eric-oss-hello-world-python-app/tests/test_main.py +++ b/eric-oss-hello-world-python-app/tests/test_main.py @@ -28,7 +28,7 @@ def test_get_metrics_returns_metrics(client): """ response = client.get("/sample-app/python/metrics") assert response.status_code == 200 - assert "python_hello_world_requests_total 0.0" in response.text + assert "eric_oss_hello_world_python_app_requests_total 0.0" in response.text def test_metrics_does_not_expose_created(client): @@ -51,7 +51,7 @@ def test_metrics_successfully_increments(client): client.get("/sample-app/python/hello") response = client.get("/sample-app/python/metrics") assert response.status_code == 200 - assert "python_hello_world_requests_total 1.0" in response.text + assert "eric_oss_hello_world_python_app_requests_total 1.0" in response.text def test_get_health_returns_health_check(client): diff --git a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py index ec8db23..7055318 100644 --- a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py +++ b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py @@ -96,7 +96,8 @@ def test_init_sets_log_level_from_log_ctrl_file(): "app_cert": "appcert.pem", "app_key": "appkey.pem", "app_cert_file_path": "certs", - "log_endpoint": "log.endpoint" + "log_endpoint": "log.endpoint", + "chosen_name": "eric-oss-hello-world-python-app" } # Patch config and environment variable From b3a7b667131d5633024032ac1005f0790b89cd65 Mon Sep 17 00:00:00 2001 From: don2112e Date: Mon, 29 Sep 2025 14:37:34 +0100 Subject: [PATCH 2/8] fix for unit test following Github action --- eric-oss-hello-world-python-app/tests/test_main.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/eric-oss-hello-world-python-app/tests/test_main.py b/eric-oss-hello-world-python-app/tests/test_main.py index a4a16f6..1a045f8 100644 --- a/eric-oss-hello-world-python-app/tests/test_main.py +++ b/eric-oss-hello-world-python-app/tests/test_main.py @@ -1,4 +1,5 @@ """Tests which cover the routes of the application""" +from unittest import expectedFailure def test_get_root_returns_bad_response(client): @@ -20,7 +21,7 @@ def test_get_hello_returns_hello_world(client): assert [response.text, response.status_code] == ["Hello World!\n", 200] -def test_get_metrics_returns_metrics(client): +def test_get_metrics_returns_metrics(client, config): """ GET to "/metrics" 200 OK @@ -28,7 +29,8 @@ def test_get_metrics_returns_metrics(client): """ response = client.get("/sample-app/python/metrics") assert response.status_code == 200 - assert "eric_oss_hello_world_python_app_requests_total 0.0" in response.text + expected_response_text = config.get("chosen_name").replace("-", "_") + "_requests_total 0.0" + assert expected_response_text in response.text def test_metrics_does_not_expose_created(client): @@ -42,7 +44,7 @@ def test_metrics_does_not_expose_created(client): assert "_created" not in response.text -def test_metrics_successfully_increments(client): +def test_metrics_successfully_increments(client, config): """ GET to "/metrics" 200 OK @@ -51,7 +53,8 @@ def test_metrics_successfully_increments(client): client.get("/sample-app/python/hello") response = client.get("/sample-app/python/metrics") assert response.status_code == 200 - assert "eric_oss_hello_world_python_app_requests_total 1.0" in response.text + expected_response_text = config.get("chosen_name").replace("-", "_") + "_requests_total 1.0" + assert expected_response_text in response.text def test_get_health_returns_health_check(client): From 9d93722d3b80ba818d129ad987385e91d5927658 Mon Sep 17 00:00:00 2001 From: don2112e Date: Mon, 29 Sep 2025 15:15:11 +0100 Subject: [PATCH 3/8] introduced function for metrics_namespace and fixed tests --- eric-oss-hello-world-python-app/config.py | 2 ++ eric-oss-hello-world-python-app/main.py | 5 ++--- eric-oss-hello-world-python-app/tests/test_config.py | 7 +++++++ 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 eric-oss-hello-world-python-app/tests/test_config.py diff --git a/eric-oss-hello-world-python-app/config.py b/eric-oss-hello-world-python-app/config.py index 2d9e459..12fbfc0 100644 --- a/eric-oss-hello-world-python-app/config.py +++ b/eric-oss-hello-world-python-app/config.py @@ -35,6 +35,8 @@ def get_config(): } return config +def get_metrics_namespace(config): + return config.get("chosen_name").replace("-", "_") def get_os_env_string(env_name, default_value): """get env""" diff --git a/eric-oss-hello-world-python-app/main.py b/eric-oss-hello-world-python-app/main.py index 8d3ab16..faf69d9 100755 --- a/eric-oss-hello-world-python-app/main.py +++ b/eric-oss-hello-world-python-app/main.py @@ -9,7 +9,7 @@ from flask import abort from flask import Flask from login import login -from config import get_config +from config import get_config, get_metrics_namespace from mtls_logging import MtlsLogging, Severity from werkzeug.middleware.dispatcher import DispatcherMiddleware from prometheus_client import ( @@ -19,7 +19,6 @@ Counter, ) -METRICS_NAMESPACE = get_config().get("chosen_name").replace("-", "_") class Application(Flask): """The Flask application itself. Subclassed for testing.""" @@ -79,7 +78,7 @@ def update_session(self): def create_metrics(self): self.registry = CollectorRegistry() self.requests_total = Counter( - namespace=METRICS_NAMESPACE, + namespace=get_metrics_namespace(get_config()), name="requests_total", documentation="Total number of API requests", ) diff --git a/eric-oss-hello-world-python-app/tests/test_config.py b/eric-oss-hello-world-python-app/tests/test_config.py new file mode 100644 index 0000000..a421d30 --- /dev/null +++ b/eric-oss-hello-world-python-app/tests/test_config.py @@ -0,0 +1,7 @@ +"""Tests which cover the app's config""" + +from config import get_metrics_namespace + +def test_get_metrics_namespace(config): + assert get_metrics_namespace(config) == "eric_oss_hello_world_python_app" + From dc3951f81eb077e388ccdb7b45f9d6bba7f97250 Mon Sep 17 00:00:00 2001 From: don2112e Date: Mon, 29 Sep 2025 15:30:06 +0100 Subject: [PATCH 4/8] fixed metrics test --- eric-oss-hello-world-python-app/tests/test_config.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eric-oss-hello-world-python-app/tests/test_config.py b/eric-oss-hello-world-python-app/tests/test_config.py index a421d30..702fde4 100644 --- a/eric-oss-hello-world-python-app/tests/test_config.py +++ b/eric-oss-hello-world-python-app/tests/test_config.py @@ -2,6 +2,6 @@ from config import get_metrics_namespace -def test_get_metrics_namespace(config): - assert get_metrics_namespace(config) == "eric_oss_hello_world_python_app" - +def test_get_metrics_namespace(): + config = {"chosen_name": "chosen-name-for-test"} + assert get_metrics_namespace(config) == "chosen_name_for_test" From 92805a04e5321637de6499d6cc326a88cef0d5ee Mon Sep 17 00:00:00 2001 From: don2112e Date: Mon, 29 Sep 2025 16:01:38 +0100 Subject: [PATCH 5/8] moved config --- eric-oss-hello-world-python-app/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eric-oss-hello-world-python-app/main.py b/eric-oss-hello-world-python-app/main.py index faf69d9..2db5b0a 100755 --- a/eric-oss-hello-world-python-app/main.py +++ b/eric-oss-hello-world-python-app/main.py @@ -27,6 +27,7 @@ def __init__(self): disable_created_metrics() self.counters = {} self.session = {"token": None, "expiry_time": 0} + self.app_config = get_config() self.create_metrics() self.wsgi_app = DispatcherMiddleware( self.wsgi_app, @@ -78,7 +79,7 @@ def update_session(self): def create_metrics(self): self.registry = CollectorRegistry() self.requests_total = Counter( - namespace=get_metrics_namespace(get_config()), + namespace=get_metrics_namespace(self.app_config), name="requests_total", documentation="Total number of API requests", ) From 09c8296e30cef801667b357c92424b949b677ef1 Mon Sep 17 00:00:00 2001 From: don2112e Date: Mon, 29 Sep 2025 16:22:30 +0100 Subject: [PATCH 6/8] improved metrics tests --- eric-oss-hello-world-python-app/tests/test_main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eric-oss-hello-world-python-app/tests/test_main.py b/eric-oss-hello-world-python-app/tests/test_main.py index 1a045f8..ddb5cfd 100644 --- a/eric-oss-hello-world-python-app/tests/test_main.py +++ b/eric-oss-hello-world-python-app/tests/test_main.py @@ -1,5 +1,5 @@ """Tests which cover the routes of the application""" -from unittest import expectedFailure +from config import get_metrics_namespace def test_get_root_returns_bad_response(client): @@ -29,7 +29,7 @@ def test_get_metrics_returns_metrics(client, config): """ response = client.get("/sample-app/python/metrics") assert response.status_code == 200 - expected_response_text = config.get("chosen_name").replace("-", "_") + "_requests_total 0.0" + expected_response_text = get_metrics_namespace(config) + "_requests_total 0.0" assert expected_response_text in response.text @@ -53,7 +53,7 @@ def test_metrics_successfully_increments(client, config): client.get("/sample-app/python/hello") response = client.get("/sample-app/python/metrics") assert response.status_code == 200 - expected_response_text = config.get("chosen_name").replace("-", "_") + "_requests_total 1.0" + expected_response_text = get_metrics_namespace(config) + "_requests_total 1.0" assert expected_response_text in response.text From c003690239ceba43f6109ac9afb814bb7ea25858 Mon Sep 17 00:00:00 2001 From: don2112e Date: Tue, 30 Sep 2025 09:25:54 +0100 Subject: [PATCH 7/8] removed unused counters variable --- eric-oss-hello-world-python-app/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/eric-oss-hello-world-python-app/main.py b/eric-oss-hello-world-python-app/main.py index 2db5b0a..2ea2499 100755 --- a/eric-oss-hello-world-python-app/main.py +++ b/eric-oss-hello-world-python-app/main.py @@ -25,7 +25,6 @@ class Application(Flask): def __init__(self): super().__init__(__name__) disable_created_metrics() - self.counters = {} self.session = {"token": None, "expiry_time": 0} self.app_config = get_config() self.create_metrics() From 54c484dd8b6c3cbf2b87ba7f902cbbfed317f233 Mon Sep 17 00:00:00 2001 From: ekievin Date: Fri, 3 Oct 2025 16:43:12 +0100 Subject: [PATCH 8/8] Updated chosen_name to chosen_unique_name in config --- eric-oss-hello-world-python-app/config.py | 5 +++-- eric-oss-hello-world-python-app/mtls_logging.py | 2 +- eric-oss-hello-world-python-app/tests/test_config.py | 2 +- eric-oss-hello-world-python-app/tests/test_mtls_logging.py | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/eric-oss-hello-world-python-app/config.py b/eric-oss-hello-world-python-app/config.py index 12fbfc0..28266c4 100644 --- a/eric-oss-hello-world-python-app/config.py +++ b/eric-oss-hello-world-python-app/config.py @@ -31,12 +31,13 @@ def get_config(): "app_cert_file_path": app_cert_file_path, "client_creds_file_path": client_creds_file_path, "client_id_file_name": client_id_file_name, - "chosen_name": "eric-oss-hello-world-python-app", + "chosen_unique_name": "eric-oss-hello-world-python-app", } return config def get_metrics_namespace(config): - return config.get("chosen_name").replace("-", "_") + """Converts the chosen_unique_name to a valid Prometheus namespace""" + return config.get("chosen_unique_name").replace("-", "_") def get_os_env_string(env_name, default_value): """get env""" diff --git a/eric-oss-hello-world-python-app/mtls_logging.py b/eric-oss-hello-world-python-app/mtls_logging.py index ecdb9dc..36cc3a6 100644 --- a/eric-oss-hello-world-python-app/mtls_logging.py +++ b/eric-oss-hello-world-python-app/mtls_logging.py @@ -74,7 +74,7 @@ def log(self, message, severity): "timestamp": time, "version": "0.0.1", "message": message, - "service_id": "rapp-" + self.config.get("chosen_name"), + "service_id": "rapp-" + self.config.get("chosen_unique_name"), "severity": severity.name.lower(), } diff --git a/eric-oss-hello-world-python-app/tests/test_config.py b/eric-oss-hello-world-python-app/tests/test_config.py index 702fde4..0b95e52 100644 --- a/eric-oss-hello-world-python-app/tests/test_config.py +++ b/eric-oss-hello-world-python-app/tests/test_config.py @@ -3,5 +3,5 @@ from config import get_metrics_namespace def test_get_metrics_namespace(): - config = {"chosen_name": "chosen-name-for-test"} + config = {"chosen_unique_name": "chosen-name-for-test"} assert get_metrics_namespace(config) == "chosen_name_for_test" diff --git a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py index 7055318..4b9b623 100644 --- a/eric-oss-hello-world-python-app/tests/test_mtls_logging.py +++ b/eric-oss-hello-world-python-app/tests/test_mtls_logging.py @@ -97,7 +97,7 @@ def test_init_sets_log_level_from_log_ctrl_file(): "app_key": "appkey.pem", "app_cert_file_path": "certs", "log_endpoint": "log.endpoint", - "chosen_name": "eric-oss-hello-world-python-app" + "chosen_unique_name": "eric-oss-hello-world-python-app" } # Patch config and environment variable