132 lines
4.2 KiB
Python
132 lines
4.2 KiB
Python
"""
|
|
Metrics collector for tracking AI processor performance and statistics.
|
|
Uses Prometheus for metrics collection and logging for Grafana integration.
|
|
"""
|
|
|
|
import logging
|
|
import time
|
|
from datetime import datetime
|
|
from typing import Dict, Any
|
|
|
|
from prometheus_client import start_http_server, Counter, Histogram, Gauge
|
|
|
|
# Initialize Prometheus metrics
|
|
try:
|
|
# Start Prometheus metrics server on port 8002 for AI processor
|
|
start_http_server(8002)
|
|
print("Prometheus metrics server started on port 8002")
|
|
except Exception as e:
|
|
print(f"Failed to start Prometheus server: {e}")
|
|
|
|
# AI Processor Metrics
|
|
articles_processed_total = Counter(
|
|
'ai_processor_articles_processed_total',
|
|
'Total number of articles processed by AI processor'
|
|
)
|
|
|
|
articles_failed_total = Counter(
|
|
'ai_processor_articles_failed_total',
|
|
'Total number of articles failed to process by AI processor'
|
|
)
|
|
|
|
facts_extracted_total = Counter(
|
|
'ai_processor_facts_extracted_total',
|
|
'Total number of facts extracted by AI processor'
|
|
)
|
|
|
|
processing_time_seconds = Histogram(
|
|
'ai_processor_processing_time_seconds',
|
|
'Time spent processing articles in AI processor'
|
|
)
|
|
|
|
cache_hits_total = Counter(
|
|
'ai_processor_cache_hits_total',
|
|
'Total number of cache hits in AI processor'
|
|
)
|
|
|
|
cache_misses_total = Counter(
|
|
'ai_processor_cache_misses_total',
|
|
'Total number of cache misses in AI processor'
|
|
)
|
|
|
|
# Current processing status
|
|
current_processing_status = Gauge(
|
|
'ai_processor_current_status',
|
|
'Current processing status of AI processor (0=inactive, 1=active)'
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class MetricsCollector:
|
|
"""Collects and reports metrics for the AI processor."""
|
|
|
|
def __init__(self):
|
|
self.start_time = None
|
|
self.active = False
|
|
|
|
def start_processing(self):
|
|
"""Mark processing as started."""
|
|
self.start_time = time.time()
|
|
self.active = True
|
|
current_processing_status.set(1)
|
|
logger.info("AI processor started processing")
|
|
|
|
def stop_processing(self):
|
|
"""Mark processing as stopped."""
|
|
self.active = False
|
|
current_processing_status.set(0)
|
|
if self.start_time:
|
|
total_time = time.time() - self.start_time
|
|
logger.info(f"AI processor stopped after {total_time:.2f} seconds")
|
|
|
|
def increment_articles_processed(self, count: int = 1):
|
|
"""Increment articles processed counter."""
|
|
articles_processed_total.inc(count)
|
|
logger.info(f"Articles processed: {count}")
|
|
|
|
def increment_articles_failed(self, count: int = 1):
|
|
"""Increment articles failed counter."""
|
|
articles_failed_total.inc(count)
|
|
logger.error(f"Articles failed: {count}")
|
|
|
|
def increment_facts_extracted(self, count: int = 1):
|
|
"""Increment facts extracted counter."""
|
|
facts_extracted_total.inc(count)
|
|
logger.info(f"Facts extracted: {count}")
|
|
|
|
def record_processing_time(self, duration: float):
|
|
"""Record processing time."""
|
|
processing_time_seconds.observe(duration)
|
|
logger.info(f"Processing time: {duration:.2f} seconds")
|
|
|
|
def increment_cache_hit(self):
|
|
"""Increment cache hit counter."""
|
|
cache_hits_total.inc()
|
|
logger.debug("Cache hit")
|
|
|
|
def increment_cache_miss(self):
|
|
"""Increment cache miss counter."""
|
|
cache_misses_total.inc()
|
|
logger.debug("Cache miss")
|
|
|
|
def log_status(self, message: str, level: str = "info"):
|
|
"""Log status message with appropriate level."""
|
|
log_method = getattr(logger, level)
|
|
log_method(message)
|
|
|
|
def get_metrics_summary(self) -> Dict[str, Any]:
|
|
"""Get current metrics summary."""
|
|
return {
|
|
"timestamp": datetime.now().isoformat(),
|
|
"active": self.active,
|
|
"start_time": self.start_time,
|
|
"articles_processed": articles_processed_total._value.get(),
|
|
"articles_failed": articles_failed_total._value.get(),
|
|
"facts_extracted": facts_extracted_total._value.get(),
|
|
"cache_hits": cache_hits_total._value.get(),
|
|
"cache_misses": cache_misses_total._value.get()
|
|
}
|
|
|
|
# Global metrics collector instance
|
|
metrics_collector = MetricsCollector()
|