Buckets:

download
raw
17.6 kB
"""
ACGS-2 Capacity Metrics Collector
Constitutional Hash: 608508a9bd224290
Main capacity metrics collector class for the Enhanced Agent Bus.
Tracks throughput, latency percentiles, queue depths, and resource utilization
with constitutional compliance monitoring for capacity planning decisions.
Performance Targets (Constitutional):
- P99 Latency: <5ms (achieved: 0.91ms)
- Throughput: >100 RPS (achieved: 6,471 RPS)
- Cache Hit Rate: >85% (achieved: 95%)
- Constitutional Compliance: 100%
Expert Reference: Michael Nygard (Release It!)
"""
from __future__ import annotations
import time
from collections import deque
from datetime import UTC, datetime, timedelta, timezone
try:
from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH
except ImportError:
CONSTITUTIONAL_HASH = "standalone"
try:
from enhanced_agent_bus._compat.types import JSONDict
except ImportError:
JSONDict = dict # type: ignore[misc,assignment]
from ..structured_logging import get_logger
from .models import (
CapacitySnapshot,
CapacityStatus,
LatencyPercentiles,
QueueMetrics,
ResourceUtilization,
ThroughputMetrics,
)
from .prometheus_compat import (
PROMETHEUS_AVAILABLE,
Gauge,
)
from .trackers import (
LatencyTracker,
SlidingWindowCounter,
)
logger = get_logger(__name__)
class EnhancedAgentBusCapacityMetrics:
"""
Capacity metrics collector for the Enhanced Agent Bus.
Tracks throughput, latency percentiles, queue depths, and resource utilization
with constitutional compliance monitoring for capacity planning decisions.
Constitutional Hash: 608508a9bd224290
"""
def __init__(
self,
service_name: str = "enhanced_agent_bus",
window_seconds: int = 60,
snapshot_interval_seconds: int = 10,
):
self.service_name = service_name
self.window_seconds = window_seconds
self.snapshot_interval = snapshot_interval_seconds
self.constitutional_hash = CONSTITUTIONAL_HASH
# Throughput tracking
self._request_counter = SlidingWindowCounter(window_seconds)
self._message_counter = SlidingWindowCounter(window_seconds)
self._validation_counter = SlidingWindowCounter(window_seconds)
# Latency tracking
self._request_latency = LatencyTracker(window_seconds=window_seconds)
self._validation_latency = LatencyTracker(window_seconds=window_seconds)
self._processing_latency = LatencyTracker(window_seconds=window_seconds)
# Queue metrics
self._queue_depth: int = 0
self._max_queue_depth: int = 0
self._queue_samples: deque[tuple[float, int]] = deque(maxlen=1000)
self._dlq_depth: int = 0
self._enqueue_counter = SlidingWindowCounter(window_seconds)
self._dequeue_counter = SlidingWindowCounter(window_seconds)
# Resource tracking (requires psutil)
self._psutil = None
try:
import psutil
self._psutil = psutil
except ImportError:
logger.warning(f"[{CONSTITUTIONAL_HASH}] psutil not available for resource metrics")
# Snapshot history
self._snapshots: deque[CapacitySnapshot] = deque(maxlen=100)
self._last_snapshot_time: float = 0
# Initialize Prometheus metrics if available
self._init_prometheus_metrics()
logger.info(f"[{CONSTITUTIONAL_HASH}] Capacity metrics initialized for {service_name}")
def _init_prometheus_metrics(self) -> None:
"""Initialize Prometheus metrics if available."""
if not PROMETHEUS_AVAILABLE:
self._prom_metrics = None
return
try:
self._prom_metrics = {
"request_rate": Gauge(
f"acgs2_{self.service_name}_request_rate",
"Current request rate (requests per second)",
["service"],
),
"message_rate": Gauge(
f"acgs2_{self.service_name}_message_rate",
"Current message throughput (messages per second)",
["service"],
),
"queue_depth": Gauge(
f"acgs2_{self.service_name}_queue_depth",
"Current message queue depth",
["service", "queue_type"],
),
"latency_p50": Gauge(
f"acgs2_{self.service_name}_latency_p50_ms",
"P50 request latency in milliseconds",
["service", "operation"],
),
"latency_p99": Gauge(
f"acgs2_{self.service_name}_latency_p99_ms",
"P99 request latency in milliseconds",
["service", "operation"],
),
"cpu_utilization": Gauge(
f"acgs2_{self.service_name}_cpu_utilization_percent",
"CPU utilization percentage",
["service"],
),
"memory_utilization": Gauge(
f"acgs2_{self.service_name}_memory_utilization_percent",
"Memory utilization percentage",
["service"],
),
"capacity_status": Gauge(
f"acgs2_{self.service_name}_capacity_status",
"Capacity status (0=healthy, 1=warning, 2=critical, 3=degraded)",
["service"],
),
}
logger.info(f"[{CONSTITUTIONAL_HASH}] Prometheus metrics initialized")
except ValueError as e:
# Metrics already exist
logger.debug(f"[{CONSTITUTIONAL_HASH}] Prometheus metrics already exist: {e}")
self._prom_metrics = None
# =========================================================================
# Recording Methods
# =========================================================================
def record_request(self, latency_ms: float, success: bool = True) -> None:
"""Record a request with latency."""
self._request_counter.increment()
self._request_latency.record(latency_ms)
def record_message(self, latency_ms: float | None = None) -> None:
"""Record a message processed."""
self._message_counter.increment()
if latency_ms is not None:
self._processing_latency.record(latency_ms)
def record_validation(self, latency_ms: float, compliant: bool = True) -> None:
"""Record a constitutional validation."""
self._validation_counter.increment()
self._validation_latency.record(latency_ms)
def record_enqueue(self, count: int = 1) -> None:
"""Record message enqueue event."""
self._enqueue_counter.increment(count)
self._queue_depth += count
if self._queue_depth > self._max_queue_depth:
self._max_queue_depth = self._queue_depth
self._queue_samples.append((time.time(), self._queue_depth))
def record_dequeue(self, count: int = 1) -> None:
"""Record message dequeue event."""
self._dequeue_counter.increment(count)
self._queue_depth = max(0, self._queue_depth - count)
self._queue_samples.append((time.time(), self._queue_depth))
def set_queue_depth(self, depth: int) -> None:
"""Set the current queue depth directly."""
self._queue_depth = depth
if depth > self._max_queue_depth:
self._max_queue_depth = depth
self._queue_samples.append((time.time(), depth))
def set_dlq_depth(self, depth: int) -> None:
"""Set the dead letter queue depth."""
self._dlq_depth = depth
# =========================================================================
# Retrieval Methods
# =========================================================================
def get_throughput_metrics(self) -> ThroughputMetrics:
"""Get current throughput metrics."""
current_rps = self._request_counter.get_rate()
peak_rps = self._request_counter.get_peak_rate()
total = self._request_counter.get_total()
return ThroughputMetrics(
current_rps=current_rps,
peak_rps=peak_rps,
avg_rps=(
total / max(1, time.time() - self._last_snapshot_time)
if self._last_snapshot_time
else current_rps
),
total_requests=total,
window_seconds=self.window_seconds,
)
def get_latency_percentiles(self, operation: str = "request") -> LatencyPercentiles:
"""Get latency percentiles for a specific operation."""
if operation == "request":
return self._request_latency.get_percentiles()
elif operation == "validation":
return self._validation_latency.get_percentiles()
elif operation == "processing":
return self._processing_latency.get_percentiles()
return LatencyPercentiles()
def get_queue_metrics(self) -> QueueMetrics:
"""Get current queue metrics."""
# Calculate average queue depth from samples
now = time.time()
cutoff = now - self.window_seconds
recent_samples = [(ts, depth) for ts, depth in self._queue_samples if ts >= cutoff]
avg_depth = sum(depth for _, depth in recent_samples) / max(1, len(recent_samples))
return QueueMetrics(
current_depth=self._queue_depth,
max_depth=self._max_queue_depth,
avg_depth=avg_depth,
enqueue_rate=self._enqueue_counter.get_rate(),
dequeue_rate=self._dequeue_counter.get_rate(),
pending_messages=self._queue_depth,
dlq_depth=self._dlq_depth,
)
def get_resource_utilization(self) -> ResourceUtilization:
"""Get current resource utilization metrics."""
if not self._psutil:
return ResourceUtilization()
try:
process = self._psutil.Process()
mem_info = process.memory_info()
cpu_percent = process.cpu_percent()
return ResourceUtilization(
cpu_percent=cpu_percent,
memory_percent=process.memory_percent(),
memory_bytes=mem_info.rss,
thread_count=process.num_threads(),
open_connections=(
len(process.net_connections())
if hasattr(process, "net_connections")
else len(process.connections())
if hasattr(process, "connections")
else 0
),
gc_collections=0, # Would need gc module
)
except (OSError, ValueError, AttributeError) as e:
logger.debug(
f"[{CONSTITUTIONAL_HASH}] Failed to get resource metrics ({type(e).__name__}): {e}"
)
return ResourceUtilization()
def get_capacity_snapshot(self) -> CapacitySnapshot:
"""Get a complete capacity snapshot."""
latency = self.get_latency_percentiles("request")
throughput = self.get_throughput_metrics()
queue = self.get_queue_metrics()
resources = self.get_resource_utilization()
# Determine status
status = CapacityStatus.HEALTHY
if latency.p99_ms > 5.0 or resources.cpu_percent > 85 or queue.current_depth > 500:
status = CapacityStatus.CRITICAL
elif latency.p99_ms > 3.0 or resources.cpu_percent > 70 or queue.current_depth > 100:
status = CapacityStatus.WARNING
elif throughput.current_rps < 10 and throughput.total_requests > 0:
status = CapacityStatus.DEGRADED
snapshot = CapacitySnapshot(
timestamp=datetime.now(UTC),
latency=latency,
throughput=throughput,
queue=queue,
resources=resources,
status=status,
)
# Update Prometheus metrics
self._update_prometheus_metrics(snapshot)
# Store snapshot
self._snapshots.append(snapshot)
self._last_snapshot_time = time.time()
return snapshot
def _update_prometheus_metrics(self, snapshot: CapacitySnapshot) -> None:
"""Update Prometheus metrics from snapshot."""
if not self._prom_metrics:
return
try:
self._prom_metrics["request_rate"].labels(service=self.service_name).set(
snapshot.throughput.current_rps
)
self._prom_metrics["message_rate"].labels(service=self.service_name).set(
self._message_counter.get_rate()
)
self._prom_metrics["queue_depth"].labels(
service=self.service_name, queue_type="main"
).set(snapshot.queue.current_depth)
self._prom_metrics["queue_depth"].labels(
service=self.service_name, queue_type="dlq"
).set(snapshot.queue.dlq_depth)
self._prom_metrics["latency_p50"].labels(
service=self.service_name, operation="request"
).set(snapshot.latency.p50_ms)
self._prom_metrics["latency_p99"].labels(
service=self.service_name, operation="request"
).set(snapshot.latency.p99_ms)
self._prom_metrics["cpu_utilization"].labels(service=self.service_name).set(
snapshot.resources.cpu_percent
)
self._prom_metrics["memory_utilization"].labels(service=self.service_name).set(
snapshot.resources.memory_percent
)
self._prom_metrics["capacity_status"].labels(service=self.service_name).set(
{"healthy": 0, "warning": 1, "critical": 2, "degraded": 3}[snapshot.status.value]
)
except (OSError, ValueError, KeyError) as e:
logger.debug(
f"[{CONSTITUTIONAL_HASH}] Failed to update Prometheus metrics ({type(e).__name__}): {e}"
)
def get_capacity_trend(self, duration_minutes: int = 10) -> JSONDict:
"""
Get capacity trend over a time period.
Args:
duration_minutes: Number of minutes to look back
Returns:
Dictionary with trend analysis
"""
cutoff = datetime.now(UTC) - timedelta(minutes=duration_minutes)
recent = [s for s in self._snapshots if s.timestamp >= cutoff]
if not recent:
return {
"available": False,
"message": "Not enough data for trend analysis",
"constitutional_hash": CONSTITUTIONAL_HASH,
}
# Calculate trends
latencies = [s.latency.p99_ms for s in recent]
throughputs = [s.throughput.current_rps for s in recent]
queue_depths = [s.queue.current_depth for s in recent]
latency_trend = "stable"
if len(latencies) >= 2:
change = latencies[-1] - latencies[0]
if change > 1.0:
latency_trend = "increasing"
elif change < -1.0:
latency_trend = "decreasing"
throughput_trend = "stable"
if len(throughputs) >= 2:
pct_change = (throughputs[-1] - throughputs[0]) / max(1, throughputs[0]) * 100
if pct_change > 20:
throughput_trend = "increasing"
elif pct_change < -20:
throughput_trend = "decreasing"
return {
"available": True,
"duration_minutes": duration_minutes,
"samples": len(recent),
"latency": {
"trend": latency_trend,
"start_p99_ms": latencies[0] if latencies else 0,
"end_p99_ms": latencies[-1] if latencies else 0,
"avg_p99_ms": sum(latencies) / len(latencies) if latencies else 0,
},
"throughput": {
"trend": throughput_trend,
"start_rps": throughputs[0] if throughputs else 0,
"end_rps": throughputs[-1] if throughputs else 0,
"avg_rps": sum(throughputs) / len(throughputs) if throughputs else 0,
},
"queue": {
"avg_depth": sum(queue_depths) / len(queue_depths) if queue_depths else 0,
"max_depth": max(queue_depths) if queue_depths else 0,
},
"constitutional_hash": CONSTITUTIONAL_HASH,
}
def get_scaling_recommendation(self) -> JSONDict:
"""Get current scaling recommendation based on capacity metrics."""
snapshot = self.get_capacity_snapshot()
return snapshot.get_scaling_recommendation()
# =============================================================================
# Singleton Instance
# =============================================================================
_capacity_metrics: EnhancedAgentBusCapacityMetrics | None = None
def get_capacity_metrics() -> EnhancedAgentBusCapacityMetrics:
"""Get or create the singleton capacity metrics instance."""
global _capacity_metrics
if _capacity_metrics is None:
_capacity_metrics = EnhancedAgentBusCapacityMetrics()
return _capacity_metrics
def reset_capacity_metrics() -> None:
"""Reset the capacity metrics singleton (for testing)."""
global _capacity_metrics
_capacity_metrics = None
__all__ = [
"EnhancedAgentBusCapacityMetrics",
"get_capacity_metrics",
"reset_capacity_metrics",
]

Xet Storage Details

Size:
17.6 kB
·
Xet hash:
758deaaf67e58920df3e8ae0e7fb71fca215a256b43024fd8162e5f1170056bb

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.