[#1] Count errors in log file
All checks were successful
Amarillo/amarillo-gitea/amarillo-metrics/pipeline/head This commit looks good

This commit is contained in:
Csaba 2024-02-29 12:56:49 +01:00
parent e0f9abf4ca
commit e3105bb2b6

View file

@ -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)