-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from codeperfectplus/features/prometheus_support
Features/prometheus support
- Loading branch information
Showing
69 changed files
with
2,675 additions
and
862 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
from src.config import app | ||
from src import routes | ||
from src.background_task import start_website_monitoring, monitor_settings | ||
import os | ||
from src.background_task import start_background_tasks | ||
|
||
# Start the background tasks | ||
start_background_tasks() | ||
|
||
# background thread to monitor system settings changes | ||
print("FLASK_ENV: ", os.getenv('FLASK_ENV')) | ||
if os.getenv('FLASK_ENV') == 'production': | ||
monitor_settings() # Starts monitoring for system logging changes | ||
start_website_monitoring() # Starts pinging active websites | ||
|
||
if __name__ == "__main__": | ||
app.run(host="0.0.0.0", port=5000, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
import os | ||
from src.background_task.monitor_website import start_website_monitoring | ||
from src.background_task.log_system_info import monitor_settings | ||
from src.background_task.external_monitoring import fetch_file_metrics_task | ||
from src.logger import logger | ||
|
||
__all__ = ["start_website_monitoring", "monitor_settings"] | ||
|
||
|
||
def start_background_tasks(): | ||
""" | ||
Starts the background tasks for the application. | ||
""" | ||
if os.getenv('FLASK_ENV') == 'production': | ||
logger.info("Starting background tasks for production environment.") | ||
start_website_monitoring() | ||
fetch_file_metrics_task() | ||
monitor_settings() | ||
else: | ||
logger.info("Background tasks are not started in development environment.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from flask import Flask, Response | ||
from prometheus_client import Gauge, generate_latest | ||
import os | ||
import time | ||
from src.models import ExternalMonitornig | ||
from src.config import app | ||
import threading | ||
from src.logger import logger | ||
|
||
# Define a Gauge metric with a label 'key' to store multiple values from the file | ||
file_metric = Gauge('external_metrics', 'Value read from file for each key', ['key']) | ||
|
||
def read_file_and_update_metric(file_path: str) -> None: | ||
"""Reads a file and updates metrics based on its content.""" | ||
if os.path.exists(file_path): | ||
with open(file_path, 'r') as file: | ||
for line in file: | ||
try: | ||
key, value = line.strip().split(':') | ||
file_metric.labels(key.strip()).set(float(value.strip())) | ||
except ValueError as ve: | ||
logger.error(f"Value error processing line '{line}': {ve}") | ||
except Exception as e: | ||
logger.error(f"Error processing line '{line}': {e}") | ||
else: | ||
logger.warning(f"File {file_path} does not exist") | ||
|
||
def fetch_file_metrics(sleep_duration: int = 5) -> None: | ||
"""Background task to read file paths from the database and update metrics.""" | ||
while True: | ||
with app.app_context(): | ||
file_paths = ExternalMonitornig.query.all() | ||
current_keys = {sample.labels['key'] for sample in file_metric.collect()[0].samples} | ||
new_keys = set() | ||
|
||
for file_path in file_paths: | ||
logger.info(f"Reading file: {file_path.file_path}") | ||
read_file_and_update_metric(file_path.file_path) | ||
new_keys.update({key.strip() for line in open(file_path.file_path) for key in line.split(':')[0].strip()}) | ||
|
||
# Remove metrics for keys that are no longer in the database | ||
for key in current_keys: | ||
if key not in new_keys: | ||
file_metric.remove(key) | ||
|
||
time.sleep(sleep_duration) | ||
|
||
def fetch_file_metrics_task() -> None: | ||
"""Starts the background task in a separate thread.""" | ||
thread = threading.Thread(target=fetch_file_metrics) | ||
thread.daemon = True | ||
thread.start() |
Oops, something went wrong.