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

Make DB connection name configurable #14 #23

Closed
wants to merge 12 commits into from
8 changes: 6 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Configuration
General
-------

The configuration can be set in `App.Heartbeat`.
The configuration can be set in `App.Heartbeat`.
It takes the following subkeys:
- `name` The name of your application (will be used for the title of the hearbeat status page)
- `layout` To override the layout (see below)
Expand All @@ -26,6 +26,9 @@ $config['App']['Heartbeat'] = [
'severity' => 3,
'class' => OrcaServices\Heartbeat\Heartbeat\Sensor\DBConnection::class,
'cached' => true,
'settings' => [
'connection_name' => 'default',
],
],
'DB up to date' => [
'enabled' => false,
Expand All @@ -41,7 +44,7 @@ $config['App']['Heartbeat'] = [
Sensors
-------

You can add your own sensor to your heartbeat status. See [How to write a Sensor](Sensors.md) for details.
You can add your own sensor to your heartbeat status. See [How to write a Sensor](Sensors.md) for details.
To add your sensor to your status page, you have to add it to your configuration array.
The sensor array takes the names of your registered sensors as subkeys, which in turn contain configuration arrays.

Expand All @@ -56,6 +59,7 @@ Those take the following subkeys:
- If set to false, the value will not be cached.
- If set to true, the value will be cached for 30 seconds (by default).
- Can be set to a relative time string e.g. '+10 minutes' to cache the value for 10 minutes.
- `settings` Additional settings for the sensor. e.g. the name of the database connection to test.

Layout overriding
-----------------
Expand Down
15 changes: 15 additions & 0 deletions docs/Sensors.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ Built-in Sensors
#### DB Connection
Checks whether a connection to the database server can be established.

To check a different connection than ``default``, e.g. ``external``,
the ``connection_name``setting can be used.

```php
$config['App']['Heartbeat']['Sensors']['DB Connection'] = [
'enabled' => true,
'severity' => 3,
'class' => OrcaServices\Heartbeat\Heartbeat\Sensor\DBConnection::class,
'cached' => true,
'settings' => [
'connection_name' => 'external'
],
];
```

#### DB Up to date
Uses the Migrations Plugin to check whether all migrations have been run.

Expand Down
17 changes: 17 additions & 0 deletions src/Heartbeat/Sensor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cake\Cache\Cache;
use Cake\Chronos\Chronos;
use Cake\Utility\Hash;
use Cake\Utility\Text;
use OrcaServices\Heartbeat\Heartbeat\Sensor\Config;
use OrcaServices\Heartbeat\Heartbeat\Sensor\Status;
Expand Down Expand Up @@ -124,4 +125,20 @@ protected function _getNonCachedStatus(): Status
* @return mixed The sensor status.
*/
abstract protected function _getStatus();

/**
* Get the given setting or an optional default value
*
* @param string $name The name of the setting to retrieve.
* @param null|mixed $default The optional default value, if the setting is not set.
* @return string The name of the connection to check.
* @todo Cover by a test.
*/
protected function _getSetting(string $name, $default = null): string
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ravage84 I'm not sure how to test this.
Maybe it should be public? Or maybe you have an idea on how to test it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't need to. Sure it would make testing easier, but code doesn't need to change just for the sake of testability.

This can be tested by a test dummy. A class that extends Sensor and then implements a public method which simply calls _getSettings() and returns it.
This public method can be called in the test case and its return value be assserted. The to-be-asserted value can be put into the configuration, beforehand, naturally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second thought, we might want to move this method to the Config class and make it public. This way the code is where it should be AND is easier testable... Let's discuss this tomorow.

{
$settings = $this->config->getSettings();
$setting = Hash::get($settings, $name, $default);

return $setting;
}
}
31 changes: 31 additions & 0 deletions src/Heartbeat/Sensor/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class Config
*/
protected $cached = false;

/**
* Additional settings
*
* @var array
*/
protected $settings = [];

/**
* The default config
*
Expand All @@ -54,6 +61,7 @@ class Config
'severity' => Status::STATUS_NONCRITICAL,
'class' => null,
'cached' => false,
'settings' => [],
];

/**
Expand All @@ -72,6 +80,7 @@ public function __construct($name, array $config)
$this->setSeverity($config['severity']);
$this->setClass($config['class']);
$this->setCached($config['cached']);
$this->setSettings($config['settings']);
}

/**
Expand Down Expand Up @@ -201,4 +210,26 @@ public function setCached($cached)
$this->cached = $cached;
}

/**
* Get additional settings for the sensor
*
* @return array The settings of the sensor.
* @todo Cover by a test.
*/
public function getSettings(): array
{
return $this->settings;
}

/**
* Set the additional settings of the sensor
*
* @param array $settings The settings of the sensor.
* @todo Cover by a test.
*/
public function setSettings(array $settings)
{
$this->settings = $settings;
}

}
8 changes: 6 additions & 2 deletions src/Heartbeat/Sensor/DBConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OrcaServices\Heartbeat\Heartbeat\Sensor;

use Cake\Database\DriverInterface;
use OrcaServices\Heartbeat\Heartbeat\Sensor;
use Cake\Datasource\ConnectionManager;

Expand All @@ -20,11 +21,14 @@ class DBConnection extends Sensor
protected function _getStatus()
{
try {
$connection = ConnectionManager::get('default');
$connectionName = $this->_getSetting('connection_name', 'default');

/** @var DriverInterface $connection */
$connection = ConnectionManager::get($connectionName);

return $connection->connect();
} catch (\Exception $e) {
return false;
}
}

}