diff --git a/eric-oss-hello-world-python-app/config.py b/eric-oss-hello-world-python-app/config.py index 603fa5e..28266c4 100644 --- a/eric-oss-hello-world-python-app/config.py +++ b/eric-oss-hello-world-python-app/config.py @@ -31,9 +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_unique_name": "eric-oss-hello-world-python-app", } return config +def get_metrics_namespace(config): + """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/main.py b/eric-oss-hello-world-python-app/main.py index eda2a0d..2ea2499 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, get_metrics_namespace from mtls_logging import MtlsLogging, Severity from werkzeug.middleware.dispatcher import DispatcherMiddleware from prometheus_client import ( @@ -18,15 +19,14 @@ Counter, ) -SERVICE_PREFIX = "python_hello_world" 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.session = {"token": None, "expiry_time": 0} + self.app_config = get_config() self.create_metrics() self.wsgi_app = DispatcherMiddleware( self.wsgi_app, @@ -78,7 +78,7 @@ def update_session(self): def create_metrics(self): self.registry = CollectorRegistry() self.requests_total = Counter( - namespace=SERVICE_PREFIX, + namespace=get_metrics_namespace(self.app_config), 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..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-eric-oss-hello-world-python-app", + "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 new file mode 100644 index 0000000..0b95e52 --- /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 = {"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_main.py b/eric-oss-hello-world-python-app/tests/test_main.py index 177b883..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,4 +1,5 @@ """Tests which cover the routes of the application""" +from config import get_metrics_namespace 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 "python_hello_world_requests_total 0.0" in response.text + expected_response_text = get_metrics_namespace(config) + "_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 "python_hello_world_requests_total 1.0" in response.text + expected_response_text = get_metrics_namespace(config) + "_requests_total 1.0" + assert expected_response_text 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..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 @@ -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_unique_name": "eric-oss-hello-world-python-app" } # Patch config and environment variable