From efaf29e5ef020694849039713afa0027f92f6bbe Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 21 Aug 2019 14:22:31 +0200 Subject: [PATCH] Metrics: Add database access time --- src/Controllers/Metrics/Controller.php | 5 +++ src/Controllers/Metrics/Stats.php | 31 +++++++++++++++++++ src/Models/EventConfig.php | 1 + .../Controllers/Metrics/ControllerTest.php | 1 + tests/Unit/Controllers/Metrics/StatsTest.php | 19 ++++++++++++ 5 files changed, 57 insertions(+) diff --git a/src/Controllers/Metrics/Controller.php b/src/Controllers/Metrics/Controller.php index ffb2a41b..99b71335 100644 --- a/src/Controllers/Metrics/Controller.php +++ b/src/Controllers/Metrics/Controller.php @@ -131,6 +131,11 @@ class Controller extends BaseController 'messages' => ['type' => 'gauge', $this->stats->messages()], 'password_resets' => ['type' => 'gauge', $this->stats->passwordResets()], 'registration_enabled' => ['type' => 'gauge', $this->config->get('registration_enabled')], + 'database' => [ + 'type' => 'gauge', + ['labels' => ['type' => 'read'], 'value' => $this->stats->databaseRead()], + ['labels' => ['type' => 'write'], 'value' => $this->stats->databaseWrite()], + ], 'sessions' => ['type' => 'gauge', $this->stats->sessions()], 'log_entries' => [ 'type' => 'counter', diff --git a/src/Controllers/Metrics/Stats.php b/src/Controllers/Metrics/Stats.php index a2b37d0c..7098d94f 100644 --- a/src/Controllers/Metrics/Stats.php +++ b/src/Controllers/Metrics/Stats.php @@ -2,7 +2,9 @@ namespace Engelsystem\Controllers\Metrics; +use Carbon\Carbon; use Engelsystem\Database\Database; +use Engelsystem\Models\EventConfig; use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Expression as QueryExpression; @@ -262,6 +264,35 @@ class Stats ->count(); } + /** + * @return float + */ + public function databaseRead() + { + $start = microtime(true); + + EventConfig::findOrNew('last_metrics'); + + return microtime(true) - $start; + } + + /** + * @return float + */ + public function databaseWrite() + { + $config = EventConfig::findOrNew('last_metrics'); + $config + ->setAttribute('name', 'last_metrics') + ->setAttribute('value', new Carbon()); + + $start = microtime(true); + + $config->save(); + + return microtime(true) - $start; + } + /** * @param string $level * @return int diff --git a/src/Models/EventConfig.php b/src/Models/EventConfig.php index 576a8f0e..2dc6a3d8 100644 --- a/src/Models/EventConfig.php +++ b/src/Models/EventConfig.php @@ -35,6 +35,7 @@ class EventConfig extends BaseModel 'event_start' => 'date', 'event_end' => 'date', 'teardown_end' => 'date', + 'last_metrics' => 'datetime', ]; /** @var bool It could be interesting to know when a value changed the last time */ diff --git a/tests/Unit/Controllers/Metrics/ControllerTest.php b/tests/Unit/Controllers/Metrics/ControllerTest.php index f203200c..eefe96ad 100644 --- a/tests/Unit/Controllers/Metrics/ControllerTest.php +++ b/tests/Unit/Controllers/Metrics/ControllerTest.php @@ -54,6 +54,7 @@ class ControllerTest extends TestCase $this->assertArrayHasKey('messages', $data); $this->assertArrayHasKey('password_resets', $data); $this->assertArrayHasKey('registration_enabled', $data); + $this->assertArrayHasKey('database', $data); $this->assertArrayHasKey('sessions', $data); $this->assertArrayHasKey('log_entries', $data); $this->assertArrayHasKey('scrape_duration_seconds', $data); diff --git a/tests/Unit/Controllers/Metrics/StatsTest.php b/tests/Unit/Controllers/Metrics/StatsTest.php index 9ecd4a66..fa78d8c3 100644 --- a/tests/Unit/Controllers/Metrics/StatsTest.php +++ b/tests/Unit/Controllers/Metrics/StatsTest.php @@ -108,6 +108,25 @@ class StatsTest extends TestCase $this->assertEquals(4, $stats->sessions()); } + /** + * @covers \Engelsystem\Controllers\Metrics\Stats::databaseRead + * @covers \Engelsystem\Controllers\Metrics\Stats::databaseWrite + */ + public function testDatabase() + { + $this->initDatabase(); + + $stats = new Stats($this->database); + + $read = $stats->databaseRead(); + $write = $stats->databaseWrite(); + + $this->assertIsFloat($read); + $this->assertNotEmpty($read); + $this->assertIsFloat($write); + $this->assertNotEmpty($write); + } + /** * @covers \Engelsystem\Controllers\Metrics\Stats::logEntries */