PHP code example of govigilant / vigilant-healthchecks-base

1. Go to this page and download the library: Download govigilant/vigilant-healthchecks-base 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/ */

    

govigilant / vigilant-healthchecks-base example snippets


use Vigilant\HealthChecksBase\Checks\Check;
use Vigilant\HealthChecksBase\Data\ResultData;
use Vigilant\HealthChecksBase\Enums\Status;

class DatabaseCheck extends Check
{
    protected string $type = 'database';

    public function available(): bool
    {
        return true; // Check if this healthcheck is available
    }

    public function run(): ResultData
    {
        // Perform your check logic here
        $isConnected = $this->checkDatabaseConnection();

        return ResultData::make([
            'type' => $this->type,
            'key' => $this->key(),
            'status' => $isConnected ? Status::Healthy : Status::Unhealthy,
            'message' => $isConnected ? 'Database is connected' : 'Cannot connect to database',
            'data' => ['host' => 'localhost', 'port' => 3306], // Optional additional data
        ]);
    }
}

use Vigilant\HealthChecksBase\Checks\MetricCheck;
use Vigilant\HealthChecksBase\Data\MetricData;

class ResponseTimeCheck extends MetricCheck
{
    protected string $type = 'response-time';

    public function available(): bool
    {
        return true;
    }

    public function measure(): MetricData
    {
        $responseTime = $this->measureResponseTime();

        return MetricData::make([
            'type' => $this->type,
            'key' => $this->key(),
            'value' => $responseTime,
            'unit' => 'ms', // Optional unit
        ]);
    }
}

use Vigilant\HealthChecksBase\Data\ResultData;
use Vigilant\HealthChecksBase\Enums\Status;

$result = ResultData::make([
    'type' => 'database',
    'key' => 'mysql-primary',
    'status' => Status::Healthy,
    'message' => 'Connected',
    'data' => ['connections' => 5],
]);

use Vigilant\HealthChecksBase\Data\MetricData;

$metric = MetricData::make([
    'type' => 'cpu-usage',
    'key' => 'server-1',
    'value' => 45.2,
    'unit' => 'percent',
]);

// Simple configuration without arguments
$config = DatabaseCheck::make();

// Configuration with constructor arguments
$config = DatabaseCheck::configure('localhost', 3306);

// Add a unique key to differentiate multiple instances
$config = DatabaseCheck::configure('localhost', 3306)->key('mysql-primary');

use Vigilant\HealthChecksBase\BuildResponse;

$builder = new BuildResponse();

$response = $builder->build(
    checks: [
        DatabaseCheck::make(),
        CacheCheck::configure('redis')->key('cache-1'),
    ],
    metricChecks: [
        ResponseTimeCheck::make(),
        MemoryUsageCheck::make(),
    ]
);

class RedisCheck extends Check
{
    public function available(): bool
    {
        // Only run this check if Redis extension is loaded
        return extension_loaded('redis');
    }

    public function run(): ResultData
    {
        // This will only be called if available() returns true
        // ...
    }
}