| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | use Daphnie\Collector; |
| 4: | use Module\Skeleton\Contracts\Tasking; |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | class Telemetry_Module extends Module_Skeleton implements Tasking |
| 19: | { |
| 20: | |
| 21: | private $collector; |
| 22: | |
| 23: | protected $exportedFunctions = [ |
| 24: | '*' => PRIVILEGE_ADMIN, |
| 25: | 'metrics' => PRIVILEGE_SITE | PRIVILEGE_ADMIN, |
| 26: | 'enabled' => PRIVILEGE_SITE | PRIVILEGE_ADMIN, |
| 27: | 'get' => PRIVILEGE_ADMIN | PRIVILEGE_SITE, |
| 28: | 'range' => PRIVILEGE_SITE | PRIVILEGE_ADMIN, |
| 29: | 'histogram' => PRIVILEGE_SITE | PRIVILEGE_ADMIN, |
| 30: | 'has' => PRIVILEGE_SITE | PRIVILEGE_ADMIN, |
| 31: | 'interval' => PRIVILEGE_SITE | PRIVILEGE_ADMIN |
| 32: | ]; |
| 33: | |
| 34: | public function __construct() |
| 35: | { |
| 36: | parent::__construct(); |
| 37: | if (!TELEMETRY_ENABLED) { |
| 38: | $this->exportedFunctions = [ |
| 39: | 'enabled' => PRIVILEGE_SITE | PRIVILEGE_ADMIN, |
| 40: | '*' => PRIVILEGE_NONE |
| 41: | ]; |
| 42: | } |
| 43: | } |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | public function enabled(): bool |
| 51: | { |
| 52: | if ($this->permission_level & PRIVILEGE_ADMIN) { |
| 53: | return (bool)TELEMETRY_ENABLED; |
| 54: | } |
| 55: | |
| 56: | return TELEMETRY_ENABLED && (bool)$this->getServiceValue('metrics', 'enabled'); |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | public function get($metric, int $site_id = null) |
| 69: | { |
| 70: | if ($this->permission_level & (PRIVILEGE_USER | PRIVILEGE_SITE)) { |
| 71: | if ($site_id && $site_id !== $this->site_id) { |
| 72: | error('Cannot specify site ID'); |
| 73: | return null; |
| 74: | } |
| 75: | $site_id = $this->site_id; |
| 76: | } |
| 77: | |
| 78: | return $this->getCollector()->get($metric, $site_id); |
| 79: | } |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | public function has(string $metric): bool |
| 88: | { |
| 89: | return null !== \Daphnie\MetricBroker::resolve($metric); |
| 90: | } |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | public function range($metric, int $begin, ?int $end = null, int $site_id = null, $summable = true) |
| 103: | { |
| 104: | if ($this->permission_level & (PRIVILEGE_USER | PRIVILEGE_SITE)) { |
| 105: | if ($site_id && $site_id !== $this->site_id) { |
| 106: | error('Cannot specify site ID'); |
| 107: | |
| 108: | return null; |
| 109: | } |
| 110: | $site_id = $this->site_id; |
| 111: | } |
| 112: | |
| 113: | return $this->getCollector()->range($metric, $begin, $end, $site_id, $summable); |
| 114: | } |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | public function interval($metric, int $begin, ?int $end = null, $site_id = null, int $size = 86400) |
| 127: | { |
| 128: | if ($this->permission_level & (PRIVILEGE_USER | PRIVILEGE_SITE)) { |
| 129: | if ($site_id && $site_id !== $this->site_id) { |
| 130: | error('Cannot specify site ID'); |
| 131: | |
| 132: | return null; |
| 133: | } |
| 134: | $site_id = $this->site_id; |
| 135: | } |
| 136: | |
| 137: | if ($size < 1 || $size > 2e31) { |
| 138: | return error("Invalid grouping size"); |
| 139: | } |
| 140: | |
| 141: | if ($size < CRON_RESOLUTION) { |
| 142: | warn("Interval smaller than [cron] => resolution time %d seconds", CRON_RESOLUTION); |
| 143: | } |
| 144: | |
| 145: | return $this->getCollector()->interval($metric, $begin, $end, $site_id, $size); |
| 146: | |
| 147: | } |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | public function histogram($metric, int $begin, ?int $end = null, int $site_id = null, int $buckets = 5, int $min = 0, int $max = 1024) |
| 162: | { |
| 163: | if ($this->permission_level & (PRIVILEGE_USER | PRIVILEGE_SITE)) { |
| 164: | if ($site_id && $site_id !== $this->site_id) { |
| 165: | error('Cannot specify site ID'); |
| 166: | |
| 167: | return null; |
| 168: | } |
| 169: | $site_id = $this->site_id; |
| 170: | } |
| 171: | |
| 172: | if ($buckets > 50 || $buckets < 2) { |
| 173: | return error("Buckets must be within [2,50]"); |
| 174: | } |
| 175: | |
| 176: | if ($min !== null && $max !== null && $min > $max) { |
| 177: | error("Minimum value %(min)d may not exceed %(max)d", [ |
| 178: | 'min' => $min, |
| 179: | 'max' => $max |
| 180: | ]); |
| 181: | |
| 182: | return null; |
| 183: | } |
| 184: | |
| 185: | return $this->getCollector()->histogram($metric, $begin, $end, $site_id, $buckets, $min, $max); |
| 186: | |
| 187: | } |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | private function getCollector(): Collector |
| 195: | { |
| 196: | if (!isset($this->collector)) { |
| 197: | $this->collector = new Collector(\PostgreSQL::pdo()); |
| 198: | } |
| 199: | |
| 200: | return $this->collector; |
| 201: | } |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | public function drop_metric(string $metric, bool $rekey = false): bool |
| 211: | { |
| 212: | if (null === ($id = $this->getCollector()->metricAsId($metric))) { |
| 213: | return false; |
| 214: | } |
| 215: | |
| 216: | $db = \PostgreSQL::pdo(); |
| 217: | |
| 218: | $table = $rekey ? 'metric_attributes' : 'metrics'; |
| 219: | $chunker = new \Daphnie\Chunker($db); |
| 220: | $chunker->decompressRange(null); |
| 221: | $stmt = $db->prepare("DELETE FROM $table WHERE attr_id = :attr_id"); |
| 222: | $ret = $stmt->execute([':attr_id' => $id]); |
| 223: | $chunker->release(); |
| 224: | return $ret ?: error('Failed to drop metric %(metric)s: %(err)s', |
| 225: | ['metric' => $metric, 'err' => array_get($stmt->errorInfo(), 2, '')] |
| 226: | ); |
| 227: | } |
| 228: | |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | public function chunks(): array |
| 235: | { |
| 236: | return (new \Daphnie\Chunker(\PostgreSQL::pdo()))->getChunkStats(); |
| 237: | } |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | public function metrics(bool $extended = false): array |
| 245: | { |
| 246: | $metrics = $this->getCollector()->all(); |
| 247: | return $extended ? $metrics : array_keys($metrics); |
| 248: | } |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | public function db_compression_usage(): array { |
| 256: | $pg = PostgreSQL::pdo(); |
| 257: | $query = (new \Daphnie\Connector($pg))->vendor()->getCompressionStats(); |
| 258: | $res = $pg->query($query); |
| 259: | if (!$res) { |
| 260: | return []; |
| 261: | } |
| 262: | |
| 263: | $rec = array_get($res->fetchAll(\PDO::FETCH_ASSOC), 0, []); |
| 264: | |
| 265: | foreach ($rec as $k => $v) { |
| 266: | if (substr($k, -6) === '_bytes') { |
| 267: | $rec[$k] = \Formatter::changeBytes($v); |
| 268: | } |
| 269: | } |
| 270: | |
| 271: | return (array)$rec; |
| 272: | } |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | public function db_usage(): array |
| 280: | { |
| 281: | $pg = PostgreSQL::pdo(); |
| 282: | $query = (new \Daphnie\Connector($pg))->vendor()->databaseUsage(); |
| 283: | $res = $pg->query($query); |
| 284: | if (!$res) { |
| 285: | return []; |
| 286: | } |
| 287: | |
| 288: | $rec = array_get($res->fetchAll(\PDO::FETCH_ASSOC), 0); |
| 289: | |
| 290: | foreach ($rec as $k => $v) { |
| 291: | if (substr($k, -6) === '_bytes') { |
| 292: | $rec[$k] = \Formatter::changeBytes($v); |
| 293: | } |
| 294: | } |
| 295: | |
| 296: | return (array)$rec; |
| 297: | } |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | |
| 304: | |
| 305: | |
| 306: | public function decompress_all(): bool |
| 307: | { |
| 308: | $pg = PostgreSQL::pdo(); |
| 309: | $chunker = new \Daphnie\Chunker($pg); |
| 310: | |
| 311: | return $chunker->decompressAll(); |
| 312: | } |
| 313: | |
| 314: | |
| 315: | |
| 316: | |
| 317: | |
| 318: | |
| 319: | public function reinitialize_compression(): bool |
| 320: | { |
| 321: | $pg = PostgreSQL::pdo(); |
| 322: | $chunker = new \Daphnie\Chunker($pg); |
| 323: | foreach ($chunker->getJobs() as $job) { |
| 324: | if (!$chunker->resumeJob($job['job_id'])) { |
| 325: | return false; |
| 326: | } |
| 327: | } |
| 328: | |
| 329: | return true; |
| 330: | } |
| 331: | |
| 332: | public function collect(): void |
| 333: | { |
| 334: | $collector = $this->getCollector(); |
| 335: | foreach ($collector->getAnonymousCollections() as $collection) { |
| 336: | $collection->log($collector); |
| 337: | } |
| 338: | |
| 339: | } |
| 340: | |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | public function version(): ?string |
| 347: | { |
| 348: | if (!TELEMETRY_ENABLED) { |
| 349: | return null; |
| 350: | } |
| 351: | |
| 352: | return (new \Daphnie\Connector(\PostgreSQL::pdo()))->getVersion(); |
| 353: | } |
| 354: | |
| 355: | public function _cron(Cronus $cron) |
| 356: | { |
| 357: | $this->collect(); |
| 358: | |
| 359: | |
| 360: | |
| 361: | $cache = \Cache_Global::spawn(); |
| 362: | $cache->get(CONFIGURATION_KEY); |
| 363: | \Lararia\JobDaemon::snapshot(); |
| 364: | } |
| 365: | } |