35 lines
1.2 KiB
PHP
35 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Monitoring;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
/**
|
|
* Registers uptime targets with the configured monitoring service. If none is
|
|
* configured, returns a deterministic id (no-op) so provisioning still records a
|
|
* stable breadcrumb. Not unit-tested (live I/O).
|
|
*/
|
|
class HttpMonitoringClient implements MonitoringClient
|
|
{
|
|
public function registerTarget(string $name, string $url): string
|
|
{
|
|
$endpoint = (string) config('services.monitoring.url');
|
|
if (blank($endpoint)) {
|
|
return 'mon-'.substr(sha1($url), 0, 12);
|
|
}
|
|
|
|
return (string) Http::withToken((string) config('services.monitoring.token'))
|
|
->post(rtrim($endpoint, '/').'/monitors', ['friendly_name' => $name, 'url' => $url, 'type' => 'https'])
|
|
->throw()->json('monitor.id');
|
|
}
|
|
|
|
public function deregisterTarget(string $externalId): void
|
|
{
|
|
$endpoint = (string) config('services.monitoring.url');
|
|
if (filled($endpoint)) {
|
|
Http::withToken((string) config('services.monitoring.token'))
|
|
->delete(rtrim($endpoint, '/').'/monitors/'.$externalId)->throw();
|
|
}
|
|
}
|
|
}
|