PHP code example of cpsit / monitoring

1. Go to this page and download the library: Download cpsit/monitoring library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cpsit / monitoring example snippets


namespace My\Vendor\Monitoring\Provider;

use CPSIT\Monitoring\Provider\MonitoringProvider;
use My\Vendor\Service\ApiService;

final class ApiMonitoringProvider implements MonitoringProvider
{
    public function __construct(
        private readonly ApiService $apiService,
    ) {}

    public function getName(): string
    {
        return 'api';
    }

    public function isHealthy(): bool
    {
        try {
            $response = $this->apiService->request('/health', 'HEAD');
            return $response->getStatusCode() < 400;
        } catch (\Exception) {
            return false;
        }
    }
}

use CPSIT\Monitoring\DependencyInjection\ServiceConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;

return static function (ContainerBuilder $container): void {
    ServiceConfigurator::configure($container);
};
diff
 namespace My\Vendor\Monitoring\Provider;

-use CPSIT\Monitoring\Provider\MonitoringProvider;
+use CPSIT\Monitoring\Provider\StatusInformationAwareMonitoringProvider;
 use My\Vendor\Service\ApiService;

-final class ApiMonitoringProvider implements MonitoringProvider
+final class ApiMonitoringProvider implements StatusInformationAwareMonitoringProvider
 {
     public function __construct(
         private readonly ApiService $apiService,
     ) {}

     public function getName(): string
     {
         return 'api';
     }

     public function isHealthy(): bool
     {
         try {
             $response = $this->apiService->request('/health', 'HEAD');
             return $response->getStatusCode() < 400;
         } catch (\Exception) {
             return false;
         }
     }
+
+    /**
+     * @return array<string, string>
+     */
+    public function getStatusInformation(): array
+    {
+        $lastProcessDate = $this->apiService->getLastProcessDate();
+
+        return [
+            'last_process_date' => $lastProcessDate->format(\DateTimeInterface::RFC2822),
+        ];
+    }
 }