name = $name; $this->timeout = $timeout; if ($errorRateThreshold < 0.0 || $errorRateThreshold > 1.0) { throw new \InvalidArgumentException('errorRateThreshold must be between 0.0 and 1.0'); } } /** * Perform health check */ public function check(): HealthCheckResult { $metrics = $this->metricsCollector->getMetrics(); $errorRate = $this->calculateErrorRate($metrics); if ($errorRate >= $this->errorRateThreshold) { return HealthCheckResult::unhealthy( "Exception error rate too high: " . number_format($errorRate * 100, 2) . "%", [ 'error_rate' => $errorRate, 'threshold' => $this->errorRateThreshold, 'total_exceptions' => $metrics->totalCount, ] ); } if ($errorRate >= $this->errorRateThreshold * 0.5) { return HealthCheckResult::warning( "Exception error rate elevated: " . number_format($errorRate * 100, 2) . "%", [ 'error_rate' => $errorRate, 'threshold' => $this->errorRateThreshold, 'total_exceptions' => $metrics->totalCount, ] ); } return HealthCheckResult::healthy( "Exception error rate normal: " . number_format($errorRate * 100, 2) . "%", [ 'error_rate' => $errorRate, 'total_exceptions' => $metrics->totalCount, ] ); } /** * Calculate error rate * * Simplified implementation - in production, would calculate based on request count. */ private function calculateErrorRate(ExceptionMetrics $metrics): float { // Simplified: use total count as proxy for error rate // In production, would compare against total request count if ($metrics->totalCount === 0) { return 0.0; } // Normalize to 0-1 range (simplified) return min(1.0, $metrics->totalCount / 1000.0); } /** * Get health check category */ public function getCategory(): \App\Framework\Health\HealthCheckCategory { return \App\Framework\Health\HealthCheckCategory::APPLICATION; } }