|
| 1 | +"""Data collectors for admin dashboard.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import hashlib |
| 6 | +import time |
| 7 | +from typing import TYPE_CHECKING |
| 8 | + |
| 9 | +from .. import metrics |
| 10 | +from ..state.redis import is_using_redis |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from ..config import Settings |
| 14 | + from ..handlers import S3ProxyHandler |
| 15 | + |
| 16 | + |
| 17 | +def collect_key_status(settings: Settings) -> dict: |
| 18 | + """Collect encryption key status. Never exposes raw key material.""" |
| 19 | + return { |
| 20 | + "kek_fingerprint": hashlib.sha256(settings.kek).hexdigest()[:16], |
| 21 | + "algorithm": "AES-256-GCM + AES-KWP", |
| 22 | + "dek_tag_name": settings.dektag_name, |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | +async def collect_upload_status(handler: S3ProxyHandler) -> dict: |
| 27 | + """Collect active multipart upload status.""" |
| 28 | + uploads = await handler.multipart_manager.list_active_uploads() |
| 29 | + return { |
| 30 | + "active_count": len(uploads), |
| 31 | + "uploads": uploads, |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | +def _read_gauge(gauge) -> float: |
| 36 | + """Read current value from a Prometheus Gauge.""" |
| 37 | + return gauge._value.get() |
| 38 | + |
| 39 | + |
| 40 | +def _read_counter(counter) -> float: |
| 41 | + """Read current value from a Prometheus Counter.""" |
| 42 | + return counter._value.get() |
| 43 | + |
| 44 | + |
| 45 | +def _read_labeled_counter_sum(counter) -> float: |
| 46 | + """Sum all label combinations for a labeled counter.""" |
| 47 | + total = 0.0 |
| 48 | + for sample in counter.collect()[0].samples: |
| 49 | + if sample.name.endswith("_total"): |
| 50 | + total += sample.value |
| 51 | + return total |
| 52 | + |
| 53 | + |
| 54 | +def _read_labeled_gauge_sum(gauge) -> float: |
| 55 | + """Sum all label combinations for a labeled gauge.""" |
| 56 | + total = 0.0 |
| 57 | + for sample in gauge.collect()[0].samples: |
| 58 | + total += sample.value |
| 59 | + return total |
| 60 | + |
| 61 | + |
| 62 | +def collect_system_health(start_time: float) -> dict: |
| 63 | + """Collect system health metrics.""" |
| 64 | + memory_reserved = _read_gauge(metrics.MEMORY_RESERVED_BYTES) |
| 65 | + memory_limit = _read_gauge(metrics.MEMORY_LIMIT_BYTES) |
| 66 | + usage_pct = round(memory_reserved / memory_limit * 100, 1) if memory_limit > 0 else 0 |
| 67 | + |
| 68 | + return { |
| 69 | + "memory_reserved_bytes": int(memory_reserved), |
| 70 | + "memory_limit_bytes": int(memory_limit), |
| 71 | + "memory_usage_pct": usage_pct, |
| 72 | + "requests_in_flight": int(_read_labeled_gauge_sum(metrics.REQUESTS_IN_FLIGHT)), |
| 73 | + "memory_rejections": int(_read_counter(metrics.MEMORY_REJECTIONS)), |
| 74 | + "uptime_seconds": int(time.monotonic() - start_time), |
| 75 | + "storage_backend": ("Redis (HA)" if is_using_redis() else "In-memory"), |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +def collect_request_stats() -> dict: |
| 80 | + """Collect request statistics.""" |
| 81 | + encrypt_ops = 0.0 |
| 82 | + decrypt_ops = 0.0 |
| 83 | + for sample in metrics.ENCRYPTION_OPERATIONS.collect()[0].samples: |
| 84 | + if sample.name.endswith("_total"): |
| 85 | + if sample.labels.get("operation") == "encrypt": |
| 86 | + encrypt_ops = sample.value |
| 87 | + elif sample.labels.get("operation") == "decrypt": |
| 88 | + decrypt_ops = sample.value |
| 89 | + |
| 90 | + return { |
| 91 | + "total_requests": int(_read_labeled_counter_sum(metrics.REQUEST_COUNT)), |
| 92 | + "encrypt_ops": int(encrypt_ops), |
| 93 | + "decrypt_ops": int(decrypt_ops), |
| 94 | + "bytes_encrypted": int(_read_counter(metrics.BYTES_ENCRYPTED)), |
| 95 | + "bytes_decrypted": int(_read_counter(metrics.BYTES_DECRYPTED)), |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +def _format_bytes(n: int) -> str: |
| 100 | + """Format bytes to human-readable string.""" |
| 101 | + for unit in ("B", "KB", "MB", "GB", "TB"): |
| 102 | + if abs(n) < 1024: |
| 103 | + return f"{n:.1f} {unit}" if unit != "B" else f"{n} {unit}" |
| 104 | + n /= 1024 |
| 105 | + return f"{n:.1f} PB" |
| 106 | + |
| 107 | + |
| 108 | +def _format_uptime(seconds: int) -> str: |
| 109 | + """Format seconds to human-readable uptime string.""" |
| 110 | + days, remainder = divmod(seconds, 86400) |
| 111 | + hours, remainder = divmod(remainder, 3600) |
| 112 | + minutes, _ = divmod(remainder, 60) |
| 113 | + parts = [] |
| 114 | + if days: |
| 115 | + parts.append(f"{days}d") |
| 116 | + if hours: |
| 117 | + parts.append(f"{hours}h") |
| 118 | + parts.append(f"{minutes}m") |
| 119 | + return " ".join(parts) |
| 120 | + |
| 121 | + |
| 122 | +async def collect_all( |
| 123 | + settings: Settings, |
| 124 | + handler: S3ProxyHandler, |
| 125 | + start_time: float, |
| 126 | +) -> dict: |
| 127 | + """Collect all dashboard data.""" |
| 128 | + upload_status = await collect_upload_status(handler) |
| 129 | + health = collect_system_health(start_time) |
| 130 | + stats = collect_request_stats() |
| 131 | + return { |
| 132 | + "key_status": collect_key_status(settings), |
| 133 | + "upload_status": upload_status, |
| 134 | + "system_health": health, |
| 135 | + "request_stats": stats, |
| 136 | + "formatted": { |
| 137 | + "memory_reserved": _format_bytes(health["memory_reserved_bytes"]), |
| 138 | + "memory_limit": _format_bytes(health["memory_limit_bytes"]), |
| 139 | + "uptime": _format_uptime(health["uptime_seconds"]), |
| 140 | + "bytes_encrypted": _format_bytes(stats["bytes_encrypted"]), |
| 141 | + "bytes_decrypted": _format_bytes(stats["bytes_decrypted"]), |
| 142 | + }, |
| 143 | + } |
0 commit comments