Skip to content

Commit 37353de

Browse files
committed
Add comments and update readme
1 parent f859619 commit 37353de

19 files changed

+357
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Built for developers who care about observability and application resilience.
77
[![Tests](https://github.com/mrtolouei/laravel-health-monitor/actions/workflows/tests.yml/badge.svg)](https://github.com/mrtolouei/laravel-health-monitor/actions/workflows/tests.yml)
88
[![Latest Stable Version](https://poser.pugx.org/mrtolouei/laravel-health-monitor/v/stable)](https://packagist.org/packages/mrtolouei/laravel-health-monitor)
99

10+
![Screen shot](screenshot.png)
11+
1012
## Installation
1113
Install the package via Composer:
1214
```

config/health-monitor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
<?php
22

3+
/**
4+
* Health Monitor Configuration File
5+
*
6+
* This file contains all the configuration options for monitoring various
7+
* services in your Laravel application including internal services and
8+
* third-party APIs.
9+
*
10+
* Each monitor has an 'enabled' flag to toggle monitoring and an 'inspector'
11+
* class that implements the actual health check logic.
12+
*/
313

414
return [
515
'app-services' => [

routes/web.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
use HealthMonitor\Http\Controllers\HealthMonitorController;
44
use Illuminate\Support\Facades\Route;
55

6+
/**
7+
* Health Monitor Routes
8+
*
9+
* Defines all web routes for the health monitoring package.
10+
* All routes are protected by the 'viewHealthMonitor' gate/policy
11+
* to ensure only authorized users can access them.
12+
*/
13+
614
Route::middleware(['can:viewHealthMonitor'])->group(function () {
715
Route::get('/api-service-health', [HealthMonitorController::class, 'status']);
816
Route::get('/service-health', function () {

screenshot.png

60 KB
Loading

src/Contracts/HealthCheckerInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@
44

55
use HealthMonitor\Dto\HealthResult;
66

7+
/**
8+
* Interface for health check monitoring implementations
9+
*/
710
interface HealthCheckerInterface
811
{
12+
/**
13+
* Execute the health check monitoring
14+
*
15+
* @return HealthResult The health check result
16+
*/
917
public function monitor(): HealthResult;
1018

19+
/**
20+
* Get the connection driver name for the monitored service
21+
*
22+
* @return string|null The driver name or null if not applicable
23+
*/
1124
public function getConnectionDriver(): string|null;
1225
}

src/Dto/ExecutorResult.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,21 @@
22

33
namespace HealthMonitor\Dto;
44

5+
/**
6+
* Data Transfer Object for health check execution results
7+
*
8+
* Contains all metrics and status information from a health check execution
9+
*/
510
class ExecutorResult
611
{
12+
/**
13+
* @param string $name The name/identifier of the monitored service
14+
* @param string $category The category of the service
15+
* @param bool $status The health status (true = healthy, false = unhealthy)
16+
* @param float $responseTime The response time in seconds
17+
* @param string $driver The underlying driver/connection type
18+
* @param string|null $exception Exception message if check failed, null otherwise
19+
*/
720
public function __construct(
821
public string $name,
922
public string $category,
@@ -16,6 +29,11 @@ public function __construct(
1629
//
1730
}
1831

32+
/**
33+
* Convert the result to an array representation
34+
*
35+
* @return array The result data as associative array
36+
*/
1937
public function toArray(): array
2038
{
2139
return [
@@ -28,31 +46,55 @@ public function toArray(): array
2846
];
2947
}
3048

49+
/**
50+
* Get the service name
51+
* @return string
52+
*/
3153
public function getName(): string
3254
{
3355
return $this->name;
3456
}
3557

58+
/**
59+
* Get the service category
60+
* @return string
61+
*/
3662
public function getCategory(): string
3763
{
3864
return $this->category;
3965
}
4066

67+
/**
68+
* Get the health status
69+
* @return bool
70+
*/
4171
public function getStatus(): bool
4272
{
4373
return $this->status;
4474
}
4575

76+
/**
77+
* Get the rounded response time
78+
* @return float
79+
*/
4680
public function getResponseTime(): float
4781
{
4882
return round($this->responseTime, 2);
4983
}
5084

85+
/**
86+
* Get the connection driver
87+
* @return string
88+
*/
5189
public function getDriver(): string
5290
{
5391
return $this->driver;
5492
}
5593

94+
/**
95+
* Get the exception message if available
96+
* @return string|null
97+
*/
5698
public function getException(): ?string
5799
{
58100
return $this->exception;

src/Dto/HealthResult.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22

33
namespace HealthMonitor\Dto;
44

5+
/**
6+
* Represents the result of a health check operation
7+
* Contains the health status and related information for a monitored service
8+
*/
59
class HealthResult
610
{
11+
/**
12+
* @param string $name The name/identifier of the monitored service
13+
* @param string $category The category/type of the service being checked
14+
* @param bool $isHealthy Whether the service is healthy (true) or not (false)
15+
* @param string|null $exception The exception message if health check failed
16+
*/
717
public function __construct(
818
protected string $name,
919
protected string $category,
@@ -14,31 +24,56 @@ public function __construct(
1424
//
1525
}
1626

27+
/**
28+
* Gets the name of the monitored service
29+
* @return string The service name
30+
*/
1731
public function getName(): string
1832
{
1933
return $this->name;
2034
}
2135

36+
/**
37+
* Gets the category of the monitored service
38+
* @return string The service category
39+
*/
2240
public function getCategory(): string
2341
{
2442
return $this->category;
2543
}
2644

45+
/**
46+
* Checks if the service is healthy
47+
* @return bool True if healthy, false otherwise
48+
*/
2749
public function isHealthy(): bool
2850
{
2951
return $this->isHealthy;
3052
}
3153

54+
/**
55+
* Gets the cleaned exception message (if any)
56+
* @return string|null The sanitized exception message or null if no exception
57+
*/
3258
public function getException(): string|null
3359
{
3460
return $this->clean($this->exception);
3561
}
3662

63+
/**
64+
* Sanitizes exception messages by:
65+
* - Removing newlines and tabs
66+
* - Collapsing multiple spaces
67+
* - Trimming quotes and whitespace
68+
* - Removing slashes
69+
* @param string|null $message The raw exception message
70+
* @return string|null The cleaned message
71+
*/
3772
private function clean(string|null $message): string|null
3873
{
3974
$message = str_replace(["\n", "\r", "\t"], ' ', $message);
4075
$message = preg_replace('/\s+/', ' ', $message);
4176
$message = trim($message, "\"' ");
4277
return stripslashes($message);
4378
}
44-
}
79+
}

src/Facades/HealthMonitor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
use Illuminate\Support\Facades\Facade;
77

88
/**
9+
* Health Monitor Facade
10+
*
11+
* Provides static access to the HealthMonitorManager service
912
* @method static string runCheckers()
1013
*/
1114
class HealthMonitor extends Facade
1215
{
1316
/**
14-
* @return string
17+
* Get the registered name of the component
18+
*
19+
* @return string The service container binding key
1520
*/
1621
protected static function getFacadeAccessor(): string
1722
{

src/Factories/HealthCheckerFactory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@
66
use HealthMonitor\Monitors\ThirdPartyHealthChecker;
77
use InvalidArgumentException;
88

9+
/**
10+
* Factory class for creating health checker instances
11+
*
12+
* Responsible for instantiating all configured health checkers
13+
* based on the package configuration
14+
*/
915
class HealthCheckerFactory
1016
{
1117
/**
12-
* @return HealthCheckerInterface[]
18+
* Creates all enabled health checker instances
19+
*
20+
* @return HealthCheckerInterface[] Array of instantiated health checkers
21+
* @throws InvalidArgumentException If a checker class doesn't exist
1322
*/
1423
public static function create(): array
1524
{

src/HealthMonitorExecutor.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@
55
use HealthMonitor\Contracts\HealthCheckerInterface;
66
use HealthMonitor\Dto\ExecutorResult;
77

8+
/**
9+
* Health Monitor Executor
10+
*
11+
* Coordinates the execution of multiple health checkers
12+
* and measures their performance metrics
13+
*/
814
class HealthMonitorExecutor
915
{
1016
/**
11-
* @param HealthCheckerInterface[] $checkers
17+
* @param HealthCheckerInterface[] $checkers Array of health checkers to execute
1218
*/
1319
public function __construct(protected array $checkers)
1420
{
1521
//
1622
}
1723

1824
/**
19-
* @return ExecutorResult[]
25+
* Execute all registered health checkers
26+
*
27+
* Runs each checker while measuring execution time and collecting results
28+
*
29+
* @return ExecutorResult[] Array of health check results with performance metrics
2030
*/
2131
public function run(): array
2232
{

0 commit comments

Comments
 (0)