Metrics: Add database access time
This commit is contained in:
parent
4d4fb81cd3
commit
efaf29e5ef
|
@ -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',
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue