PHP code example of modernmcguire / overwatch

1. Go to this page and download the library: Download modernmcguire/overwatch 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/ */

    

modernmcguire / overwatch example snippets




use Modernmcguire\Overwatch\Metrics\PhpVersion;
use Modernmcguire\Overwatch\Metrics\LaravelVersion;

return [

    'secret' => env('OVERWATCH_SECRET'),

    'metrics' => [
        PhpVersion::class,
        LaravelVersion::class,
    ],
];



namespace App\Metrics;

use Modernmcguire\Overwatch\Metric;

class TotalUsers extends Metric
{
    public function handle()
    {
        return User::count();
    }
}



namespace App\Metrics;

use Modernmcguire\Overwatch\Metric;

class TotalUsers extends Metric
{
    const KEY = 'app_users';

    public function handle()
    {
        return User::count();
    }
}



use App\Metric\TotalUsers;
use Modernmcguire\Overwatch\Metrics\PhpVersion;
use Modernmcguire\Overwatch\Metrics\LaravelVersion;

return [
    'metrics' => [
        PhpVersion::class,
        LaravelVersion::class,

        TotalUsers::class,
    ],
];



use Illuminate\Encryption\Encrypter;

$newEncrypter = new Encrypter(
    $super_secret_key,
    strtolower(config('app.cipher'))
);

// adding a timestamp to the payload helps prevent replay attacks
$payload = json_encode([
    'timestamp' => now()->toDateTimeString()
]);

$metrics = Http::asJson()->post('https://awesome-application.com/overwatch', [
    'payload' => $newEncrypter->encrypt($payload),
])->json();
bash
php artisan overwatch:generate
bash
php artisan vendor:publish --tag="overwatch-config"
bash
php artisan overwatch:generate
bash
php artisan overwatch:metrics
bash
php artisan overwatch:metrics --json
json
{"php_version": "8.0.3", "laravel_version": "8.40.0", "app_users": 10}