From e3105bb2b66ce0930c433e79cfc408c77f9ffd53 Mon Sep 17 00:00:00 2001 From: Francia Csaba Date: Thu, 29 Feb 2024 12:56:49 +0100 Subject: [PATCH] [#1] Count errors in log file --- amarillo/plugins/metrics/metrics.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/amarillo/plugins/metrics/metrics.py b/amarillo/plugins/metrics/metrics.py index 2f83211..8b9d5d8 100644 --- a/amarillo/plugins/metrics/metrics.py +++ b/amarillo/plugins/metrics/metrics.py @@ -62,6 +62,23 @@ def amarillo_grfs_file_size() -> Callable[[Info], None]: return instrumentation +def amarillo_errors() -> Callable[[Info], None]: + METRIC = Gauge("amarillo_errors", "Number of errors in the error.log file") + + def instrumentation(info: Info) -> None: + file_path = "error.log" + search_string = " - ERROR - " + + error_count = 0 + try: + with open(file_path, 'r') as file: + error_count = sum(1 for line in file if search_string in line) + # logger.info(f"Number of lines containing '{search_string}': {error_count}") + except FileNotFoundError: + logger.warning(f"File '{file_path}' not found.") + METRIC.set(error_count) + + return instrumentation router = APIRouter( prefix="/metrics", @@ -93,6 +110,7 @@ def setup(app: FastAPI): instrumentator.add(amarillo_trips_number_total()) instrumentator.add(amarillo_gtfs_file_size()) instrumentator.add(amarillo_grfs_file_size()) + instrumentator.add(amarillo_errors()) instrumentator.instrument(app) \ No newline at end of file