Skip to content

Commit 204ab86

Browse files
Merge pull request #251 from moebrowne
Prometheus Logger Syntax Fix
2 parents 6958f1c + a05f4e9 commit 204ab86

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

src/Log/Prometheus.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function onPhpbuEnd(Event\App\End $event)
8787
$this->write('# HELP phpbu_backup_success Whether or not the backup succeeded' . PHP_EOL);
8888
$this->write('# TYPE phpbu_backup_success gauge' . PHP_EOL);
8989
foreach ($event->getResult()->getBackups() as $backupResult) {
90-
$this->write('phpbu_backup_success{name="' . $backupResult->getName() . '} ' . (int)$backupResult->allOk() . PHP_EOL);
90+
$this->write('phpbu_backup_success{name="' . $backupResult->getName() . '"} ' . (int)$backupResult->allOk() . PHP_EOL);
9191
}
9292

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

102102
$this->write(PHP_EOL);
103103

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

110110
$this->write(PHP_EOL);
111111

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

118118
}

tests/phpbu/Log/PrometheusTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
namespace phpbu\App\Log;
3+
4+
use phpbu\App\Configuration\Backup;
5+
6+
/**
7+
* Json Test
8+
*
9+
* @package phpbu
10+
* @subpackage tests
11+
* @author MoeBrowne
12+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
13+
* @since Class available since Release 6.0.0
14+
*/
15+
class PrometheusTest extends \PHPUnit\Framework\TestCase
16+
{
17+
18+
/**
19+
* Tests Prometheus::onBackupEnd
20+
*/
21+
public function testOutput()
22+
{
23+
// result mock
24+
$result = $this->getResultMock();
25+
26+
// config mock
27+
$backupConf = $this->createMock(Backup::class);
28+
$backupConf->method('getName')->willReturn('backupName');
29+
30+
// backup start event mock
31+
$backupStartEvent = $this->createMock(\phpbu\App\Event\Backup\Start::class);
32+
$backupStartEvent->method('getConfiguration')->willReturn($backupConf);
33+
34+
// backup end event mock
35+
$backupEndEvent = $this->createMock(\phpbu\App\Event\Backup\End::class);
36+
$backupEndEvent->method('getConfiguration')->willReturn($backupConf);
37+
38+
// phpbu end event mock
39+
$phpbuEndEvent = $this->createMock(\phpbu\App\Event\App\End::class);
40+
$phpbuEndEvent->method('getResult')->willReturn($result);
41+
42+
$prometheus = new Prometheus();
43+
$prometheus->setup(['target' => 'php://output']);
44+
45+
$prometheus->onBackupStart($backupStartEvent);
46+
$prometheus->onBackupEnd($backupEndEvent);
47+
48+
ob_flush();
49+
ob_start();
50+
$prometheus->onPhpbuEnd($phpbuEndEvent);
51+
$output = ob_get_clean();
52+
53+
$this->assertStringContainsString('phpbu_backup_success{name="foo"} 0', $output);
54+
$this->assertStringContainsString('phpbu_backup_duration{name="backupName"} ', $output);
55+
$this->assertStringContainsString('phpbu_backup_last_run{name="backupName"} ', $output);
56+
$this->assertStringContainsString('phpbu_backup_size{name="backupName"} ', $output);
57+
}
58+
59+
/**
60+
* Create a app result mock
61+
*
62+
* @return \phpbu\App\Result
63+
*/
64+
protected function getResultMock()
65+
{
66+
$result = $this->createMock(\phpbu\App\Result::class);
67+
$result->method('started')->willReturn(microtime(true));
68+
$result->method('allOk')->willReturn(true);
69+
$result->method('getErrors')->willReturn([new \Exception('foo bar')]);
70+
$result->method('getBackups')->willReturn([$this->getBackupResultMock()]);
71+
$result->method('backupsFailedCount')->willReturn(0);
72+
$result->method('errorCount')->willReturn(1);
73+
74+
return $result;
75+
}
76+
77+
/**
78+
* Create a backup result mock
79+
*
80+
* @return \phpbu\App\Result\Backup
81+
*/
82+
protected function getBackupResultMock()
83+
{
84+
$backup = $this->createMock(\phpbu\App\Result\Backup::class);
85+
$backup->method('getName')->willReturn('foo');
86+
$backup->method('wasSuccessful')->willReturn(true);
87+
$backup->method('checkCount')->willReturn(0);
88+
$backup->method('checkCountFailed')->willReturn(0);
89+
$backup->method('syncCount')->willReturn(0);
90+
$backup->method('syncCountSkipped')->willReturn(0);
91+
$backup->method('syncCountFailed')->willReturn(0);
92+
$backup->method('cleanupCount')->willReturn(0);
93+
$backup->method('cleanupCountSkipped')->willReturn(0);
94+
$backup->method('cleanupCountFailed')->willReturn(0);
95+
96+
return $backup;
97+
}
98+
}

0 commit comments

Comments
 (0)