Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus Logger Syntax Fix #251

Merged
merged 1 commit into from May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Log/Prometheus.php
Expand Up @@ -87,7 +87,7 @@ public function onPhpbuEnd(Event\App\End $event)
$this->write('# HELP phpbu_backup_success Whether or not the backup succeeded' . PHP_EOL);
$this->write('# TYPE phpbu_backup_success gauge' . PHP_EOL);
foreach ($event->getResult()->getBackups() as $backupResult) {
$this->write('phpbu_backup_success{name="' . $backupResult->getName() . '} ' . (int)$backupResult->allOk() . PHP_EOL);
$this->write('phpbu_backup_success{name="' . $backupResult->getName() . '"} ' . (int)$backupResult->allOk() . PHP_EOL);
}

$this->write(PHP_EOL);
Expand All @@ -96,23 +96,23 @@ public function onPhpbuEnd(Event\App\End $event)
$this->write('# TYPE phpbu_backup_duration gauge' . PHP_EOL);
foreach ($this->backupStats as $backupName => $backupStats) {
$duration = $this->backupStats[$backupName]['timeEnd'] - $this->backupStats[$backupName]['timeStart'];
$this->write('phpbu_backup_duration{name="' . $backupName . '} ' . $duration . PHP_EOL);
$this->write('phpbu_backup_duration{name="' . $backupName . '"} ' . $duration . PHP_EOL);
}

$this->write(PHP_EOL);

$this->write('# HELP phpbu_backup_last_run The unix timestamp of the last run' . PHP_EOL);
$this->write('# TYPE phpbu_backup_last_run counter' . PHP_EOL);
foreach ($this->backupStats as $backupName => $backupStats) {
$this->write('phpbu_backup_last_run{name="' . $backupName . '} ' . (int)$this->backupStats[$backupName]['lastRun'] . PHP_EOL);
$this->write('phpbu_backup_last_run{name="' . $backupName . '"} ' . (int)$this->backupStats[$backupName]['lastRun'] . PHP_EOL);
}

$this->write(PHP_EOL);

$this->write('# HELP phpbu_backup_size The size of the last successful backup' . PHP_EOL);
$this->write('# TYPE phpbu_backup_size gauge' . PHP_EOL);
foreach ($this->backupStats as $backupName => $backupStats) {
$this->write('phpbu_backup_size{name="' . $backupName . '} ' . $this->backupStats[$backupName]['size'] . PHP_EOL);
$this->write('phpbu_backup_size{name="' . $backupName . '"} ' . $this->backupStats[$backupName]['size'] . PHP_EOL);
}

}
Expand Down
98 changes: 98 additions & 0 deletions tests/phpbu/Log/PrometheusTest.php
@@ -0,0 +1,98 @@
<?php
namespace phpbu\App\Log;

use phpbu\App\Configuration\Backup;

/**
* Json Test
*
* @package phpbu
* @subpackage tests
* @author MoeBrowne
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
* @since Class available since Release 6.0.0
*/
class PrometheusTest extends \PHPUnit\Framework\TestCase
{

/**
* Tests Prometheus::onBackupEnd
*/
public function testOutput()
{
// result mock
$result = $this->getResultMock();

// config mock
$backupConf = $this->createMock(Backup::class);
$backupConf->method('getName')->willReturn('backupName');

// backup start event mock
$backupStartEvent = $this->createMock(\phpbu\App\Event\Backup\Start::class);
$backupStartEvent->method('getConfiguration')->willReturn($backupConf);

// backup end event mock
$backupEndEvent = $this->createMock(\phpbu\App\Event\Backup\End::class);
$backupEndEvent->method('getConfiguration')->willReturn($backupConf);

// phpbu end event mock
$phpbuEndEvent = $this->createMock(\phpbu\App\Event\App\End::class);
$phpbuEndEvent->method('getResult')->willReturn($result);

$prometheus = new Prometheus();
$prometheus->setup(['target' => 'php://output']);

$prometheus->onBackupStart($backupStartEvent);
$prometheus->onBackupEnd($backupEndEvent);

ob_flush();
ob_start();
$prometheus->onPhpbuEnd($phpbuEndEvent);
$output = ob_get_clean();

$this->assertStringContainsString('phpbu_backup_success{name="foo"} 0', $output);
$this->assertStringContainsString('phpbu_backup_duration{name="backupName"} ', $output);
$this->assertStringContainsString('phpbu_backup_last_run{name="backupName"} ', $output);
$this->assertStringContainsString('phpbu_backup_size{name="backupName"} ', $output);
}

/**
* Create a app result mock
*
* @return \phpbu\App\Result
*/
protected function getResultMock()
{
$result = $this->createMock(\phpbu\App\Result::class);
$result->method('started')->willReturn(microtime(true));
$result->method('allOk')->willReturn(true);
$result->method('getErrors')->willReturn([new \Exception('foo bar')]);
$result->method('getBackups')->willReturn([$this->getBackupResultMock()]);
$result->method('backupsFailedCount')->willReturn(0);
$result->method('errorCount')->willReturn(1);

return $result;
}

/**
* Create a backup result mock
*
* @return \phpbu\App\Result\Backup
*/
protected function getBackupResultMock()
{
$backup = $this->createMock(\phpbu\App\Result\Backup::class);
$backup->method('getName')->willReturn('foo');
$backup->method('wasSuccessful')->willReturn(true);
$backup->method('checkCount')->willReturn(0);
$backup->method('checkCountFailed')->willReturn(0);
$backup->method('syncCount')->willReturn(0);
$backup->method('syncCountSkipped')->willReturn(0);
$backup->method('syncCountFailed')->willReturn(0);
$backup->method('cleanupCount')->willReturn(0);
$backup->method('cleanupCountSkipped')->willReturn(0);
$backup->method('cleanupCountFailed')->willReturn(0);

return $backup;
}
}