Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions eric-oss-hello-world-python-app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
6 changes: 3 additions & 3 deletions eric-oss-hello-world-python-app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down Expand Up @@ -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",
)
Expand Down
2 changes: 1 addition & 1 deletion eric-oss-hello-world-python-app/mtls_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}

Expand Down
7 changes: 7 additions & 0 deletions eric-oss-hello-world-python-app/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 7 additions & 4 deletions eric-oss-hello-world-python-app/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -20,15 +21,16 @@ 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
Body containing Prometheus-compatible metrics
"""
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):
Expand All @@ -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
Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion eric-oss-hello-world-python-app/tests/test_mtls_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down