Merge pull request #642 from MyIgel/db-stats

Metrics: Add database access time
This commit is contained in:
msquare 2019-08-21 15:46:26 +02:00 committed by GitHub
commit 1a6c7599d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 0 deletions

View File

@ -131,6 +131,11 @@ class Controller extends BaseController
'messages' => ['type' => 'gauge', $this->stats->messages()], 'messages' => ['type' => 'gauge', $this->stats->messages()],
'password_resets' => ['type' => 'gauge', $this->stats->passwordResets()], 'password_resets' => ['type' => 'gauge', $this->stats->passwordResets()],
'registration_enabled' => ['type' => 'gauge', $this->config->get('registration_enabled')], '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()], 'sessions' => ['type' => 'gauge', $this->stats->sessions()],
'log_entries' => [ 'log_entries' => [
'type' => 'counter', 'type' => 'counter',

View File

@ -2,7 +2,9 @@
namespace Engelsystem\Controllers\Metrics; namespace Engelsystem\Controllers\Metrics;
use Carbon\Carbon;
use Engelsystem\Database\Database; use Engelsystem\Database\Database;
use Engelsystem\Models\EventConfig;
use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression as QueryExpression; use Illuminate\Database\Query\Expression as QueryExpression;
@ -262,6 +264,35 @@ class Stats
->count(); ->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 * @param string $level
* @return int * @return int

View File

@ -35,6 +35,7 @@ class EventConfig extends BaseModel
'event_start' => 'date', 'event_start' => 'date',
'event_end' => 'date', 'event_end' => 'date',
'teardown_end' => 'date', 'teardown_end' => 'date',
'last_metrics' => 'datetime',
]; ];
/** @var bool It could be interesting to know when a value changed the last time */ /** @var bool It could be interesting to know when a value changed the last time */

View File

@ -54,6 +54,7 @@ class ControllerTest extends TestCase
$this->assertArrayHasKey('messages', $data); $this->assertArrayHasKey('messages', $data);
$this->assertArrayHasKey('password_resets', $data); $this->assertArrayHasKey('password_resets', $data);
$this->assertArrayHasKey('registration_enabled', $data); $this->assertArrayHasKey('registration_enabled', $data);
$this->assertArrayHasKey('database', $data);
$this->assertArrayHasKey('sessions', $data); $this->assertArrayHasKey('sessions', $data);
$this->assertArrayHasKey('log_entries', $data); $this->assertArrayHasKey('log_entries', $data);
$this->assertArrayHasKey('scrape_duration_seconds', $data); $this->assertArrayHasKey('scrape_duration_seconds', $data);

View File

@ -108,6 +108,25 @@ class StatsTest extends TestCase
$this->assertEquals(4, $stats->sessions()); $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 * @covers \Engelsystem\Controllers\Metrics\Stats::logEntries
*/ */