|
| 1 | +<?php |
| 2 | +namespace phpbu\App\Log; |
| 3 | + |
| 4 | +use phpbu\App\Exception; |
| 5 | +use phpbu\App\Event; |
| 6 | +use phpbu\App\Listener; |
| 7 | + |
| 8 | +/** |
| 9 | + * Json Logger |
| 10 | + * |
| 11 | + * @package phpbu |
| 12 | + * @subpackage Log |
| 13 | + * @author MoeBrowne |
| 14 | + * @license https://opensource.org/licenses/MIT The MIT License (MIT) |
| 15 | + * @link http://phpbu.de/ |
| 16 | + * @since Class available since Release 6.0.0 |
| 17 | + */ |
| 18 | +class Prometheus extends File implements Listener, Logger |
| 19 | +{ |
| 20 | + protected $backupStats = []; |
| 21 | + |
| 22 | + /** |
| 23 | + * Returns an array of event names this subscriber wants to listen to. |
| 24 | + * |
| 25 | + * The array keys are event names and the value can be: |
| 26 | + * |
| 27 | + * - The method name to call (priority defaults to 0) |
| 28 | + * - An array composed of the method name to call and the priority |
| 29 | + * - An array of arrays composed of the method names to call and respective |
| 30 | + * priorities, or 0 if unset |
| 31 | + * |
| 32 | + * @return array The event names to listen to |
| 33 | + */ |
| 34 | + public static function getSubscribedEvents(): array |
| 35 | + { |
| 36 | + return [ |
| 37 | + 'phpbu.backup_start' => 'onBackupStart', |
| 38 | + 'phpbu.backup_end' => 'onBackupEnd', |
| 39 | + 'phpbu.app_end' => 'onPhpbuEnd', |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Setup the logger. |
| 45 | + * |
| 46 | + * @see \phpbu\App\Log\Logger::setup |
| 47 | + * @param array $options |
| 48 | + * @throws \phpbu\App\Exception |
| 49 | + */ |
| 50 | + public function setup(array $options) |
| 51 | + { |
| 52 | + if (empty($options['target'])) { |
| 53 | + throw new Exception('no target given'); |
| 54 | + } |
| 55 | + $this->setOut($options['target']); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Backup start event. |
| 60 | + * |
| 61 | + * @param \phpbu\App\Event\Backup\Start $event |
| 62 | + */ |
| 63 | + public function onBackupStart(Event\Backup\Start $event) |
| 64 | + { |
| 65 | + $this->backupStats[$event->getConfiguration()->getName()]['timeStart'] = microtime(true); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Backup end event. |
| 70 | + * |
| 71 | + * @param \phpbu\App\Event\Backup\End $event |
| 72 | + */ |
| 73 | + public function onBackupEnd(Event\Backup\End $event) |
| 74 | + { |
| 75 | + $this->backupStats[$event->getConfiguration()->getName()]['timeEnd'] = microtime(true); |
| 76 | + $this->backupStats[$event->getConfiguration()->getName()]['lastRun'] = microtime(true); |
| 77 | + $this->backupStats[$event->getConfiguration()->getName()]['size'] = $event->getTarget()->getSize(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * App end event. |
| 82 | + * |
| 83 | + * @param \phpbu\App\Event\App\End $event |
| 84 | + */ |
| 85 | + public function onPhpbuEnd(Event\App\End $event) |
| 86 | + { |
| 87 | + $this->write('# HELP phpbu_backup_success Whether or not the backup succeeded' . PHP_EOL); |
| 88 | + $this->write('# TYPE phpbu_backup_success gauge' . PHP_EOL); |
| 89 | + foreach ($event->getResult()->getBackups() as $backupResult) { |
| 90 | + $this->write('phpbu_backup_success{name="' . $backupResult->getName() . '} ' . (int)$backupResult->allOk() . PHP_EOL); |
| 91 | + } |
| 92 | + |
| 93 | + $this->write(PHP_EOL); |
| 94 | + |
| 95 | + $this->write('# HELP phpbu_backup_duration The total time the backup took to execute' . PHP_EOL); |
| 96 | + $this->write('# TYPE phpbu_backup_duration gauge' . PHP_EOL); |
| 97 | + foreach ($this->backupStats as $backupName => $backupStats) { |
| 98 | + $duration = $this->backupStats[$backupName]['timeEnd'] - $this->backupStats[$backupName]['timeStart']; |
| 99 | + $this->write('phpbu_backup_duration{name="' . $backupName . '} ' . $duration . PHP_EOL); |
| 100 | + } |
| 101 | + |
| 102 | + $this->write(PHP_EOL); |
| 103 | + |
| 104 | + $this->write('# HELP phpbu_backup_last_run The unix timestamp of the last run' . PHP_EOL); |
| 105 | + $this->write('# TYPE phpbu_backup_last_run counter' . PHP_EOL); |
| 106 | + foreach ($this->backupStats as $backupName => $backupStats) { |
| 107 | + $this->write('phpbu_backup_last_run{name="' . $backupName . '} ' . (int)$this->backupStats[$backupName]['lastRun'] . PHP_EOL); |
| 108 | + } |
| 109 | + |
| 110 | + $this->write(PHP_EOL); |
| 111 | + |
| 112 | + $this->write('# HELP phpbu_backup_size The size of the last successful backup' . PHP_EOL); |
| 113 | + $this->write('# TYPE phpbu_backup_size gauge' . PHP_EOL); |
| 114 | + foreach ($this->backupStats as $backupName => $backupStats) { |
| 115 | + $this->write('phpbu_backup_size{name="' . $backupName . '} ' . $this->backupStats[$backupName]['size'] . PHP_EOL); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | +} |
0 commit comments