feat(Production): Complete production deployment infrastructure

- Add comprehensive health check system with multiple endpoints
- Add Prometheus metrics endpoint
- Add production logging configurations (5 strategies)
- Add complete deployment documentation suite:
  * QUICKSTART.md - 30-minute deployment guide
  * DEPLOYMENT_CHECKLIST.md - Printable verification checklist
  * DEPLOYMENT_WORKFLOW.md - Complete deployment lifecycle
  * PRODUCTION_DEPLOYMENT.md - Comprehensive technical reference
  * production-logging.md - Logging configuration guide
  * ANSIBLE_DEPLOYMENT.md - Infrastructure as Code automation
  * README.md - Navigation hub
  * DEPLOYMENT_SUMMARY.md - Executive summary
- Add deployment scripts and automation
- Add DEPLOYMENT_PLAN.md - Concrete plan for immediate deployment
- Update README with production-ready features

All production infrastructure is now complete and ready for deployment.
This commit is contained in:
2025-10-25 19:18:37 +02:00
parent caa85db796
commit fc3d7e6357
83016 changed files with 378904 additions and 20919 deletions

View File

@@ -7,13 +7,14 @@ namespace App\Framework\ErrorReporting;
use App\Framework\DateTime\Clock;
use App\Framework\ErrorReporting\Storage\ErrorReportStorageInterface;
use App\Framework\Logging\Logger;
use App\Framework\Logging\ValueObjects\LogContext;
use App\Framework\Queue\Queue;
use Throwable;
/**
* Central error reporting service
*/
final readonly class ErrorReporter
final readonly class ErrorReporter implements ErrorReporterInterface
{
public function __construct(
private ErrorReportStorageInterface $storage,
@@ -263,16 +264,16 @@ final readonly class ErrorReporter
private function logDebug(string $message, array $context = []): void
{
$this->logger?->debug("[ErrorReporter] {$message}", $context);
$this->logger?->debug("[ErrorReporter] {$message}", LogContext::withData($context));
}
private function logInfo(string $message, array $context = []): void
{
$this->logger?->info("[ErrorReporter] {$message}", $context);
$this->logger?->info("[ErrorReporter] {$message}", LogContext::withData($context));
}
private function logError(string $message, array $context = []): void
{
$this->logger?->error("[ErrorReporter] {$message}", $context);
$this->logger?->error("[ErrorReporter] {$message}", LogContext::withData($context));
}
}

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Framework\ErrorReporting;
use Throwable;
/**
* Interface for error reporting services
*
* Provides comprehensive error reporting with context enrichment
*/
interface ErrorReporterInterface
{
/**
* Report an error from Throwable
*/
public function reportThrowable(
Throwable $throwable,
string $level = 'error',
array $context = []
): string;
/**
* Report a manual error
*/
public function reportError(
string $level,
string $message,
array $context = [],
?Throwable $exception = null
): string;
/**
* Report an error with full context
*/
public function report(ErrorReport $report): string;
/**
* Get error report by ID
*/
public function getReport(string $reportId): ?ErrorReport;
/**
* Get recent error reports
*/
public function getRecentReports(int $limit = 100, int $offset = 0): array;
}

View File

@@ -25,8 +25,17 @@ final readonly class ErrorReportingInitializer
#[Initializer]
public function initialize(Container $container): void
{
$enabled = (bool) ($_ENV['ERROR_REPORTING_ENABLED'] ?? true);
// Storage
$container->bind(ErrorReportStorageInterface::class, function (Container $container) {
$container->bind(ErrorReportStorageInterface::class, function (Container $container) use ($enabled) {
if (! $enabled) {
// Return storage even if disabled (might be used for queries)
return new DatabaseErrorReportStorage(
connection: $container->get(ConnectionInterface::class)
);
}
return new DatabaseErrorReportStorage(
connection: $container->get(ConnectionInterface::class)
);
@@ -40,8 +49,57 @@ final readonly class ErrorReportingInitializer
);
});
// Error Reporter
$container->bind(ErrorReporter::class, function (Container $container) {
// Error Reporter Interface - bind to concrete or Null implementation
$container->bind(ErrorReporterInterface::class, function (Container $container) use ($enabled) {
if (! $enabled) {
return new NullErrorReporter();
}
$processors = [];
$filters = [];
// Add built-in processors
if ($container->has(RequestContextProcessor::class)) {
$processors[] = $container->get(RequestContextProcessor::class);
}
if ($container->has(UserContextProcessor::class)) {
$processors[] = $container->get(UserContextProcessor::class);
}
// Add environment-based filters
if (($_ENV['ERROR_REPORTING_FILTER_LEVELS'] ?? null)) {
$allowedLevels = explode(',', $_ENV['ERROR_REPORTING_FILTER_LEVELS']);
$filters[] = function (ErrorReport $report) use ($allowedLevels) {
return in_array($report->level, $allowedLevels);
};
}
// Add environment filter for production
if (($_ENV['APP_ENV'] ?? 'production') === 'production') {
$filters[] = function (ErrorReport $report) {
// Don't report debug/info in production
return ! in_array($report->level, ['debug', 'info']);
};
}
return new ErrorReporter(
storage: $container->get(ErrorReportStorageInterface::class),
clock: $container->get(Clock::class),
logger: $container->has(Logger::class) ? $container->get(Logger::class) : null,
queue: $container->has(Queue::class) ? $container->get(Queue::class) : null,
asyncProcessing: (bool) ($_ENV['ERROR_REPORTING_ASYNC'] ?? true),
processors: $processors,
filters: $filters
);
});
// Error Reporter (concrete class) - delegate to interface
$container->bind(ErrorReporter::class, function (Container $container) use ($enabled) {
if (! $enabled) {
throw new \RuntimeException('ErrorReporter is disabled. Use ErrorReporterInterface instead.');
}
$processors = [];
$filters = [];

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace App\Framework\ErrorReporting;
use Throwable;
/**
* Null Object implementation for ErrorReporter
*
* Used when error reporting is disabled or unavailable.
* All operations are no-ops that return empty/default values.
*/
final readonly class NullErrorReporter implements ErrorReporterInterface
{
public function reportThrowable(
Throwable $throwable,
string $level = 'error',
array $context = []
): string {
// No-op: Error reporting is disabled
return 'null-report-' . bin2hex(random_bytes(8));
}
public function reportError(
string $level,
string $message,
array $context = [],
?Throwable $exception = null
): string {
// No-op: Error reporting is disabled
return 'null-report-' . bin2hex(random_bytes(8));
}
public function report(ErrorReport $report): string
{
// No-op: Error reporting is disabled
return $report->id;
}
public function getReport(string $reportId): ?ErrorReport
{
return null;
}
public function getRecentReports(int $limit = 100, int $offset = 0): array
{
return [];
}
}

View File

@@ -0,0 +1,220 @@
<?php
declare(strict_types=1);
namespace App\Framework\ErrorReporting\Storage;
use App\Framework\ErrorReporting\ErrorReport;
use App\Framework\ErrorReporting\ErrorReportCriteria;
use App\Framework\ErrorReporting\ErrorStatistics;
use DateTimeImmutable;
/**
* In-memory implementation of error report storage for testing
*/
final class InMemoryErrorReportStorage implements ErrorReportStorageInterface
{
/** @var array<string, ErrorReport> */
private array $reports = [];
public function store(ErrorReport $report): void
{
$this->reports[$report->id] = $report;
}
public function storeBatch(array $reports): void
{
foreach ($reports as $report) {
$this->store($report);
}
}
public function find(string $reportId): ?ErrorReport
{
return $this->reports[$reportId] ?? null;
}
public function findRecent(int $limit = 100, int $offset = 0): array
{
$reports = array_values($this->reports);
// Sort by timestamp descending
usort($reports, fn ($a, $b) => $b->timestamp <=> $a->timestamp);
return array_slice($reports, $offset, $limit);
}
public function findByCriteria(ErrorReportCriteria $criteria): array
{
return array_filter($this->reports, function (ErrorReport $report) use ($criteria) {
// Simple criteria filtering - can be expanded
if ($criteria->level && $report->level !== $criteria->level) {
return false;
}
if ($criteria->exception && $report->exception !== $criteria->exception) {
return false;
}
if ($criteria->from && $report->timestamp < $criteria->from) {
return false;
}
if ($criteria->to && $report->timestamp > $criteria->to) {
return false;
}
return true;
});
}
public function countByCriteria(ErrorReportCriteria $criteria): int
{
return count($this->findByCriteria($criteria));
}
public function getStatistics(DateTimeImmutable $from, DateTimeImmutable $to): ErrorStatistics
{
$reportsInRange = array_filter(
$this->reports,
fn (ErrorReport $r) => $r->timestamp >= $from && $r->timestamp <= $to
);
$totalCount = count($reportsInRange);
$byLevel = [];
$byException = [];
foreach ($reportsInRange as $report) {
$byLevel[$report->level] = ($byLevel[$report->level] ?? 0) + 1;
$byException[$report->exception] = ($byException[$report->exception] ?? 0) + 1;
}
return new ErrorStatistics(
totalCount: $totalCount,
byLevel: $byLevel,
byException: $byException,
period: [
'from' => $from->format('c'),
'to' => $to->format('c'),
]
);
}
public function getTrends(DateTimeImmutable $from, DateTimeImmutable $to, string $groupBy = 'hour'): array
{
$reportsInRange = array_filter(
$this->reports,
fn (ErrorReport $r) => $r->timestamp >= $from && $r->timestamp <= $to
);
$trends = [];
foreach ($reportsInRange as $report) {
$key = match ($groupBy) {
'hour' => $report->timestamp->format('Y-m-d H:00'),
'day' => $report->timestamp->format('Y-m-d'),
'week' => $report->timestamp->format('Y-W'),
'month' => $report->timestamp->format('Y-m'),
default => $report->timestamp->format('Y-m-d H:00'),
};
$trends[$key] = ($trends[$key] ?? 0) + 1;
}
return $trends;
}
public function getTopErrors(DateTimeImmutable $from, DateTimeImmutable $to, int $limit = 10): array
{
$reportsInRange = array_filter(
$this->reports,
fn (ErrorReport $r) => $r->timestamp >= $from && $r->timestamp <= $to
);
$errorCounts = [];
foreach ($reportsInRange as $report) {
$fingerprint = $report->getFingerprint();
if (!isset($errorCounts[$fingerprint])) {
$errorCounts[$fingerprint] = [
'fingerprint' => $fingerprint,
'count' => 0,
'first_seen' => $report->timestamp,
'last_seen' => $report->timestamp,
'example' => $report,
];
}
$errorCounts[$fingerprint]['count']++;
$errorCounts[$fingerprint]['last_seen'] = max(
$errorCounts[$fingerprint]['last_seen'],
$report->timestamp
);
}
// Sort by count descending
usort($errorCounts, fn ($a, $b) => $b['count'] <=> $a['count']);
return array_slice($errorCounts, 0, $limit);
}
public function findByFingerprint(string $fingerprint, int $limit = 100): array
{
$matching = array_filter(
$this->reports,
fn (ErrorReport $r) => $r->getFingerprint() === $fingerprint
);
// Sort by timestamp descending
$matching = array_values($matching);
usort($matching, fn ($a, $b) => $b->timestamp <=> $a->timestamp);
return array_slice($matching, 0, $limit);
}
public function deleteOlderThan(DateTimeImmutable $before): int
{
$count = 0;
foreach ($this->reports as $id => $report) {
if ($report->timestamp < $before) {
unset($this->reports[$id]);
$count++;
}
}
return $count;
}
public function deleteByCriteria(ErrorReportCriteria $criteria): int
{
$toDelete = $this->findByCriteria($criteria);
$count = 0;
foreach ($toDelete as $report) {
unset($this->reports[$report->id]);
$count++;
}
return $count;
}
public function getHealthInfo(): array
{
return [
'status' => 'healthy',
'total_reports' => count($this->reports),
'storage_type' => 'in_memory',
'memory_usage' => memory_get_usage(true),
];
}
/**
* Clear all reports (for testing)
*/
public function clear(): void
{
$this->reports = [];
}
}