-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(attribute.state): prevent missing state in sensor and metric
Introduced `SensorStateInterface` and `MetricStateInterface` to formalize state handling in sensors and metrics. Adjusted exception handling to throw `LogicException` instead of `RuntimeException` for unset states. Enhanced `CheckCommand` and `AttributeNormalizer` to incorporate the new interfaces and handle state retrieval more robustly.
- Loading branch information
Robin Lehrmann
committed
Aug 29, 2024
1 parent
82d093b
commit 5bd8905
Showing
13 changed files
with
197 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace whatwedo\MonitorBundle\Monitoring\Metric; | ||
|
||
use whatwedo\MonitorBundle\Enums\MetricStateEnum; | ||
|
||
interface MetricStateInterface | ||
{ | ||
/** | ||
* @throws \LogicException | ||
*/ | ||
public function getState(): MetricStateEnum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace whatwedo\MonitorBundle\Monitoring\Sensor; | ||
|
||
use whatwedo\MonitorBundle\Enums\SensorStateEnum; | ||
|
||
interface SensorStateInterface | ||
{ | ||
/** | ||
* @throws \LogicException | ||
*/ | ||
public function getState(): SensorStateEnum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace whatwedo\MonitorBundle\Tests\Normalizer; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use whatwedo\MonitorBundle\Monitoring\AttributeInterface; | ||
use whatwedo\MonitorBundle\Tests\Monitoring\Metric\Dummy\CriticalDummyMetric; | ||
use whatwedo\MonitorBundle\Tests\Monitoring\Metric\Dummy\LogicErrorDummyMetric; | ||
use whatwedo\MonitorBundle\Tests\Monitoring\Metric\Dummy\OkDummyMetric; | ||
use whatwedo\MonitorBundle\Tests\Monitoring\Metric\Dummy\WarningDummyMetric; | ||
use whatwedo\MonitorBundle\Tests\Monitoring\Sensor\Dummy\CriticalDummySensor; | ||
use whatwedo\MonitorBundle\Tests\Monitoring\Sensor\Dummy\LogicErrorDummySensor; | ||
use whatwedo\MonitorBundle\Tests\Monitoring\Sensor\Dummy\SuccessfulDummySensor; | ||
use whatwedo\MonitorBundle\Tests\Monitoring\Sensor\Dummy\WarningDummySensor; | ||
use whatwedo\MonitorBundle\Tests\UseTestKernelTrait; | ||
|
||
final class AttributeNormalizerTest extends KernelTestCase | ||
{ | ||
use UseTestKernelTrait; | ||
|
||
public static function provideDummyMetricsAndSensors(): array | ||
{ | ||
return [ | ||
CriticalDummyMetric::class => [ | ||
'attribute' => new CriticalDummyMetric(), | ||
'expectedNormalizedBody' => [ | ||
'name' => 'Critical Test', | ||
'state' => 'critical', | ||
'value' => 24, | ||
], | ||
], | ||
OkDummyMetric::class => [ | ||
'attribute' => new OkDummyMetric(), | ||
'expectedNormalizedBody' => [ | ||
'name' => 'Ok Test', | ||
'state' => 'ok', | ||
'value' => 80, | ||
], | ||
], | ||
WarningDummyMetric::class => [ | ||
'attribute' => new WarningDummyMetric(), | ||
'expectedNormalizedBody' => [ | ||
'name' => 'Warning Test', | ||
'state' => 'warning', | ||
'value' => 125, | ||
], | ||
], | ||
|
||
CriticalDummySensor::class => [ | ||
'attribute' => new CriticalDummySensor(), | ||
'expectedNormalizedBody' => [ | ||
'name' => 'Critical Test', | ||
'state' => 'critical', | ||
'details' => [], | ||
], | ||
], | ||
SuccessfulDummySensor::class => [ | ||
'attribute' => new SuccessfulDummySensor(), | ||
'expectedNormalizedBody' => [ | ||
'name' => 'Successful Test', | ||
'state' => 'successful', | ||
'details' => [], | ||
], | ||
], | ||
WarningDummySensor::class => [ | ||
'attribute' => new WarningDummySensor(), | ||
'expectedNormalizedBody' => [ | ||
'name' => 'Warning Test', | ||
'state' => 'warning', | ||
'details' => [], | ||
], | ||
], | ||
|
||
LogicErrorDummyMetric::class => [ | ||
'attribute' => new LogicErrorDummyMetric(), | ||
'expectedNormalizedBody' => [ | ||
'name' => 'Logic Error Test', | ||
'value' => null, | ||
], | ||
], | ||
LogicErrorDummySensor::class => [ | ||
'attribute' => new LogicErrorDummySensor(), | ||
'expectedNormalizedBody' => [ | ||
'name' => 'Logic Error Test', | ||
'details' => [], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideDummyMetricsAndSensors | ||
*/ | ||
public function testNormalizeWorksAsExpected(AttributeInterface $attribute, array $expectedNormalizedBody): void | ||
{ | ||
$attribute->run(); | ||
|
||
self::assertSame( | ||
$expectedNormalizedBody, | ||
$this->getContainer()->get(NormalizerInterface::class)->normalize($attribute) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
whatwedo\MonitorBundle\Tests\Monitoring\Sensor\Dummy\LogicErrorDummySensor: | ||
tags: [ 'whatwedo_monitor.attribute' ] | ||
|
||
whatwedo\MonitorBundle\Tests\Monitoring\Metric\Dummy\LogicErrorDummyMetric: | ||
tags: [ 'whatwedo_monitor.attribute' ] |
This file was deleted.
Oops, something went wrong.