PHP code example of azaharizaman / nexus-telemetry
1. Go to this page and download the library: Download azaharizaman/nexus-telemetry 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/ */
use Nexus\Telemetry\Services\HealthCheckRunner;
use Nexus\Telemetry\HealthChecks\DatabaseHealthCheck;
// Register health checks
$runner = new HealthCheckRunner($cache, $logger);
$runner->register(new DatabaseHealthCheck($pdo));
// Execute all checks
$results = $runner->runAll();
// Check specific component
$dbHealth = $runner->runCheck('database');
echo $dbHealth->getStatus()->value; // HEALTHY, DEGRADED, OFFLINE, CRITICAL
use Nexus\Telemetry\Traits\MonitoringAwareTrait;
class OrderService
{
use MonitoringAwareTrait;
public function __construct(TelemetryTrackerInterface $telemetry)
{
$this->setTelemetry($telemetry);
}
public function processOrder(Order $order): void
{
// Automatic SLO tracking
$this->trackOperation('order.process', function() use ($order) {
// Your business logic
$this->saveOrder($order);
$this->notifyCustomer($order);
}, tags: ['payment_method' => $order->paymentMethod]);
// Manual metrics
$this->recordIncrement('orders.completed');
$this->recordGauge('orders.value', $order->total);
}
}
use Nexus\Telemetry\Services\MetricRetentionService;
use Nexus\Telemetry\Core\TimeBasedRetentionPolicy;
// Create retention policy
$policy = TimeBasedRetentionPolicy::days(30);
$retentionService = new MetricRetentionService(
$metricStorage,
$policy,
$logger
);
// Schedule cleanup (e.g., Laravel)
$schedule->call(function() use ($retentionService) {
if ($retentionService->needsCleanup(threshold: 10000)) {
$pruned = $retentionService->pruneExpiredMetrics(batchSize: 1000);
Log::info("Pruned {$pruned} expired metrics");
}
})->daily();