PHP code example of polarity-labs / observer-agent

1. Go to this page and download the library: Download polarity-labs/observer-agent 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/ */

    

polarity-labs / observer-agent example snippets


'health_endpoints' => [
    ['url' => env('APP_URL').'/up', 'name' => 'app'],
    ['url' => 'https://api.myapp.com/health', 'name' => 'api', 'headers' => ['Authorization' => 'Bearer token']],
],

'health_endpoints' => [
    ['url' => 'https://myapp.com', 'name' => 'public-site', 'check_from' => 'external'],
],

use PolarityLabs\ObserverAgent\Observer;
use PolarityLabs\ObserverAgent\HealthCheckResult;

public function boot(): void
{
    $observer = app(Observer::class);

    $observer->healthCheck('redis', function () {
        try {
            Redis::ping();
            return HealthCheckResult::healthy()->responseTime(1);
        } catch (\Exception $e) {
            return HealthCheckResult::unhealthy()->errorMessage($e->getMessage());
        }
    });

    $observer->healthCheck('database', function () {
        $start = hrtime(true);
        DB::select('SELECT 1');
        $ms = (int) ((hrtime(true) - $start) / 1_000_000);
        return HealthCheckResult::healthy()->responseTime($ms);
    });
}

// Static constructors
HealthCheckResult::healthy();
HealthCheckResult::unhealthy();
HealthCheckResult::make(isHealthy: true, responseTimeMs: 42, errorMessage: null);

// Chainable methods
HealthCheckResult::healthy()->responseTime(42);
HealthCheckResult::unhealthy()->responseTime(5000)->errorMessage('Connection refused');

// Config-based (URL checks)
['url' => 'http://localhost/up', 'name' => 'local', 'leader_only' => false],

// Code-based (custom checks)
$observer->healthCheck('redis', $callback, leaderOnly: false);
bash
php artisan vendor:publish --tag=observer-config
bash
php artisan observer:status
bash
php artisan observer:start
bash
php artisan observer:restart
bash
php artisan observer:start --once