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

@@ -0,0 +1,340 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\ErrorHandling;
use App\Framework\Cache\Cache;
use App\Framework\Cache\CacheIdentifier;
use App\Framework\Cache\CacheItem;
use App\Framework\Cache\CacheKey;
use App\Framework\Cache\CacheResult;
use App\Framework\Core\ValueObjects\Duration;
use App\Framework\Core\ValueObjects\Timestamp;
use App\Framework\DateTime\Clock;
use App\Framework\ErrorAggregation\ErrorAggregator;
use App\Framework\ErrorAggregation\ErrorEvent;
use App\Framework\ErrorAggregation\Storage\InMemoryErrorStorage;
use App\Framework\Exception\Core\CacheErrorCode;
use App\Framework\Exception\Core\DatabaseErrorCode;
use App\Framework\Exception\Core\ErrorSeverity;
use App\Framework\Exception\Core\SystemErrorCode;
use App\Framework\Exception\ErrorHandlerContext;
use App\Framework\Exception\ExceptionContext;
use App\Framework\Exception\FrameworkException;
use App\Framework\Exception\RequestContext;
use App\Framework\Exception\SystemContext;
use App\Framework\Queue\InMemoryQueue;
// Helper function für Mock-Objekte
function createTestCache(): Cache
{
return new class implements Cache {
private array $data = [];
public function get(CacheIdentifier ...$identifiers): CacheResult
{
$items = [];
foreach ($identifiers as $identifier) {
if (isset($this->data[$identifier->toString()])) {
$items[] = $this->data[$identifier->toString()];
} else {
$items[] = CacheItem::miss($identifier instanceof CacheKey ? $identifier : CacheKey::fromString($identifier->toString()));
}
}
return CacheResult::fromItems(...$items);
}
public function set(CacheItem ...$items): bool
{
foreach ($items as $item) {
$this->data[$item->key->toString()] = $item;
}
return true;
}
public function has(CacheIdentifier ...$identifiers): array
{
$result = [];
foreach ($identifiers as $identifier) {
$key = (string) $identifier;
$result[$key] = isset($this->data[$key]);
}
return $result;
}
public function forget(CacheIdentifier ...$identifiers): bool
{
foreach ($identifiers as $identifier) {
$key = (string) $identifier;
unset($this->data[$key]);
}
return true;
}
public function clear(): bool
{
$this->data = [];
return true;
}
public function remember(CacheKey $key, callable $callback, ?Duration $ttl = null): CacheItem
{
$keyStr = $key->toString();
if (isset($this->data[$keyStr])) {
return $this->data[$keyStr];
}
$value = $callback();
$item = CacheItem::forSetting($key, $value, $ttl);
$this->data[$keyStr] = $item;
return $item;
}
};
}
function createTestClock(): Clock
{
return new class implements Clock {
public function now(): \DateTimeImmutable
{
return new \DateTimeImmutable();
}
public function fromTimestamp(Timestamp $timestamp): \DateTimeImmutable
{
return $timestamp->toDateTime();
}
public function fromString(string $dateTime, ?string $format = null): \DateTimeImmutable
{
return new \DateTimeImmutable($dateTime);
}
public function today(): \DateTimeImmutable
{
return new \DateTimeImmutable('today');
}
public function yesterday(): \DateTimeImmutable
{
return new \DateTimeImmutable('yesterday');
}
public function tomorrow(): \DateTimeImmutable
{
return new \DateTimeImmutable('tomorrow');
}
public function time(): Timestamp
{
return Timestamp::now();
}
};
}
describe('ErrorHandler ErrorAggregator Integration', function () {
it('processes ErrorHandlerContext through ErrorAggregator', function () {
// Setup ErrorAggregator
$storage = new InMemoryErrorStorage();
$cache = createTestCache();
$clock = createTestClock();
$alertQueue = new InMemoryQueue();
$errorAggregator = new ErrorAggregator(
storage: $storage,
cache: $cache,
clock: $clock,
alertQueue: $alertQueue
);
// Create ErrorHandlerContext (simulates what ErrorHandler does)
$exception = FrameworkException::create(
DatabaseErrorCode::QUERY_FAILED,
'Test database query failed'
);
$exceptionContext = ExceptionContext::empty()
->withOperation('test_operation', 'TestComponent')
->withData([
'test_key' => 'test_value',
'original_exception' => $exception, // Store exception for message extraction
'exception_message' => $exception->getMessage()
]);
$requestContext = RequestContext::fromGlobals();
$systemContext = SystemContext::current();
$errorHandlerContext = ErrorHandlerContext::create(
$exceptionContext,
$requestContext,
$systemContext,
['http_status' => 500]
);
// Process through ErrorAggregator (simulates ErrorHandler calling it)
$errorAggregator->processError($errorHandlerContext);
// Verify error was stored
$recentEvents = $errorAggregator->getRecentEvents(10);
expect($recentEvents)->toHaveCount(1);
$event = $recentEvents[0];
expect($event->errorMessage)->toBe('Test database query failed');
expect($event->severity)->toBe(ErrorSeverity::ERROR);
expect($event->component)->toBe('TestComponent');
expect($event->operation)->toBe('test_operation');
// Verify pattern was created
$activePatterns = $errorAggregator->getActivePatterns(10);
expect($activePatterns)->toHaveCount(1);
$pattern = $activePatterns[0];
expect($pattern->occurrenceCount)->toBe(1);
expect($pattern->severity)->toBe(ErrorSeverity::ERROR);
expect($pattern->component)->toBe('TestComponent');
});
it('creates error patterns from multiple identical errors', function () {
$storage = new InMemoryErrorStorage();
$cache = createTestCache();
$clock = createTestClock();
$alertQueue = new InMemoryQueue();
$errorAggregator = new ErrorAggregator(
storage: $storage,
cache: $cache,
clock: $clock,
alertQueue: $alertQueue
);
// Process same error 3 times
for ($i = 0; $i < 3; $i++) {
$exception = FrameworkException::create(
DatabaseErrorCode::QUERY_FAILED,
'Repeated database error'
);
$exceptionContext = ExceptionContext::empty()
->withOperation('query_execution', 'DatabaseManager')
->withData([
'query' => 'SELECT * FROM users',
'original_exception' => $exception,
'exception_message' => $exception->getMessage()
]);
$requestContext = RequestContext::fromGlobals();
$systemContext = SystemContext::current();
$errorHandlerContext = ErrorHandlerContext::create(
$exceptionContext,
$requestContext,
$systemContext,
['http_status' => 500]
);
$errorAggregator->processError($errorHandlerContext);
}
// Verify all 3 events were stored
$recentEvents = $errorAggregator->getRecentEvents(10);
expect($recentEvents)->toHaveCount(3);
// Verify single pattern with 3 occurrences (same fingerprint)
$activePatterns = $errorAggregator->getActivePatterns(10);
expect($activePatterns)->toHaveCount(1);
$pattern = $activePatterns[0];
expect($pattern->occurrenceCount)->toBe(3);
expect($pattern->component)->toBe('DatabaseManager');
expect($pattern->operation)->toBe('query_execution');
});
it('handles ErrorAggregator being null (optional dependency)', function () {
$errorAggregator = null;
// Simulate the nullable call pattern used in ErrorHandler
$exception = FrameworkException::create(
DatabaseErrorCode::QUERY_FAILED,
'Test error'
);
$exceptionContext = ExceptionContext::empty();
$requestContext = RequestContext::fromGlobals();
$systemContext = SystemContext::current();
$errorHandlerContext = ErrorHandlerContext::create(
$exceptionContext,
$requestContext,
$systemContext,
[]
);
// This should not throw an error
$errorAggregator?->processError($errorHandlerContext);
// If we get here, the null-safe operator worked correctly
expect(true)->toBeTrue();
});
it('processes errors with different severities correctly', function () {
$storage = new InMemoryErrorStorage();
$cache = createTestCache();
$clock = createTestClock();
$alertQueue = new InMemoryQueue();
$errorAggregator = new ErrorAggregator(
storage: $storage,
cache: $cache,
clock: $clock,
alertQueue: $alertQueue
);
// Process errors with different severities
$errorCodes = [
SystemErrorCode::RESOURCE_EXHAUSTED, // CRITICAL
DatabaseErrorCode::QUERY_FAILED, // ERROR
CacheErrorCode::READ_FAILED, // WARNING
];
foreach ($errorCodes as $errorCode) {
$exception = FrameworkException::create($errorCode, 'Test message');
$exceptionContext = ExceptionContext::empty()
->withOperation('test_op', 'TestComponent')
->withData([
'original_exception' => $exception,
'exception_message' => $exception->getMessage()
]);
$requestContext = RequestContext::fromGlobals();
$systemContext = SystemContext::current();
$errorHandlerContext = ErrorHandlerContext::create(
$exceptionContext,
$requestContext,
$systemContext,
[]
);
$errorAggregator->processError($errorHandlerContext);
}
// Verify all events were stored
$recentEvents = $errorAggregator->getRecentEvents(10);
expect($recentEvents)->toHaveCount(3);
// Verify patterns reflect correct severities
$activePatterns = $errorAggregator->getActivePatterns(10);
expect($activePatterns)->toHaveCount(3);
$severities = array_map(fn($p) => $p->severity, $activePatterns);
expect($severities)->toContain(ErrorSeverity::CRITICAL);
expect($severities)->toContain(ErrorSeverity::ERROR);
expect($severities)->toContain(ErrorSeverity::WARNING);
});
});

View File

@@ -0,0 +1,283 @@
<?php
declare(strict_types=1);
use App\Framework\Cache\Cache;
use App\Framework\Cache\CacheIdentifier;
use App\Framework\Cache\CacheKey;
use App\Framework\Cache\CacheItem;
use App\Framework\Cache\CacheResult;
use App\Framework\Core\ValueObjects\Duration;
use App\Framework\DateTime\SystemClock;
use App\Framework\ErrorAggregation\ErrorAggregator;
use App\Framework\ErrorAggregation\ErrorAggregatorInterface;
use App\Framework\ErrorAggregation\Storage\InMemoryErrorStorage;
use App\Framework\ErrorHandling\ErrorHandler;
use App\Framework\ErrorReporting\ErrorReporter;
use App\Framework\ErrorReporting\ErrorReporterInterface;
use App\Framework\ErrorReporting\Storage\InMemoryErrorReportStorage;
use App\Framework\Exception\Core\DatabaseErrorCode;
use App\Framework\Exception\Core\ErrorSeverity;
use App\Framework\Exception\Core\SystemErrorCode;
use App\Framework\Exception\DatabaseException;
use App\Framework\Exception\FrameworkException;
use App\Framework\Http\RequestIdGenerator;
use App\Framework\Http\ResponseEmitter;
use App\Framework\DI\DefaultContainer;
use App\Framework\Queue\InMemoryQueue;
describe('ErrorHandler Full Pipeline Integration', function () {
beforeEach(function () {
// Create all dependencies
$this->container = new DefaultContainer();
$this->emitter = new ResponseEmitter();
$this->requestIdGenerator = new RequestIdGenerator();
// Error Aggregation setup
$this->errorStorage = new InMemoryErrorStorage();
$this->cache = new class implements Cache {
private array $data = [];
public function get(CacheIdentifier ...$identifiers): CacheResult
{
$items = [];
foreach ($identifiers as $identifier) {
$keyStr = $identifier->toString();
if (isset($this->data[$keyStr])) {
$items[] = $this->data[$keyStr];
} else {
$items[] = CacheItem::miss($identifier instanceof CacheKey ? $identifier : CacheKey::fromString($keyStr));
}
}
return CacheResult::fromItems(...$items);
}
public function set(CacheItem ...$items): bool
{
foreach ($items as $item) {
$this->data[$item->key->toString()] = $item;
}
return true;
}
public function has(CacheIdentifier ...$identifiers): array
{
$result = [];
foreach ($identifiers as $identifier) {
$result[] = isset($this->data[$identifier->toString()]);
}
return $result;
}
public function forget(CacheIdentifier ...$identifiers): bool
{
foreach ($identifiers as $identifier) {
unset($this->data[$identifier->toString()]);
}
return true;
}
public function clear(): bool
{
$this->data = [];
return true;
}
public function remember(CacheKey $key, callable $callback, ?Duration $ttl = null): CacheItem
{
$keyStr = $key->toString();
if (isset($this->data[$keyStr])) {
return $this->data[$keyStr];
}
$value = $callback();
$item = $ttl ? CacheItem::forSet($key, $value, $ttl) : CacheItem::miss($key);
$this->data[$keyStr] = $item;
return $item;
}
};
$this->clock = new SystemClock();
$this->alertQueue = new InMemoryQueue();
$this->errorAggregator = new ErrorAggregator(
storage: $this->errorStorage,
cache: $this->cache,
clock: $this->clock,
alertQueue: $this->alertQueue,
logger: null,
batchSize: 100,
maxRetentionDays: 90
);
// Error Reporting setup
$this->errorReportStorage = new InMemoryErrorReportStorage();
$this->reportQueue = new InMemoryQueue();
$this->errorReporter = new ErrorReporter(
storage: $this->errorReportStorage,
clock: $this->clock,
logger: null,
queue: $this->reportQueue,
asyncProcessing: false, // Synchronous for testing
processors: [],
filters: []
);
// Create ErrorHandler with full pipeline
$this->errorHandler = new ErrorHandler(
emitter: $this->emitter,
container: $this->container,
requestIdGenerator: $this->requestIdGenerator,
errorAggregator: $this->errorAggregator,
errorReporter: $this->errorReporter,
logger: null,
isDebugMode: true,
securityHandler: null
);
});
it('processes errors through complete pipeline: ErrorHandler → ErrorAggregator → ErrorReporter', function () {
// Create a test exception
$exception = FrameworkException::create(
DatabaseErrorCode::QUERY_FAILED,
'Test database error'
);
// Create HTTP response (triggers processing through all systems)
$response = $this->errorHandler->createHttpResponse($exception);
// Verify ErrorAggregator processed the error
$events = $this->errorStorage->getRecentEvents(10);
expect($events)->toHaveCount(1);
expect($events[0]->errorMessage)->toBe('Test database error');
expect($events[0]->severity)->toBe(ErrorSeverity::ERROR);
// Verify ErrorReporter created a report
$reports = $this->errorReportStorage->findRecent(10);
expect($reports)->toHaveCount(1);
expect($reports[0]->exception)->toBe('App\Framework\Exception\FrameworkException');
expect($reports[0]->message)->toContain('Test database error');
expect($reports[0]->level)->toBe('error');
});
it('creates error patterns and error reports simultaneously', function () {
// Create multiple identical errors
$exception1 = FrameworkException::create(
SystemErrorCode::RESOURCE_EXHAUSTED,
'Memory limit exceeded'
);
$exception2 = FrameworkException::create(
SystemErrorCode::RESOURCE_EXHAUSTED,
'Memory limit exceeded'
);
$exception3 = FrameworkException::create(
SystemErrorCode::RESOURCE_EXHAUSTED,
'Memory limit exceeded'
);
// Process all exceptions
$this->errorHandler->createHttpResponse($exception1);
$this->errorHandler->createHttpResponse($exception2);
$this->errorHandler->createHttpResponse($exception3);
// Verify ErrorAggregator created patterns
$patterns = $this->errorStorage->getActivePatterns(10);
expect($patterns)->toHaveCount(1);
expect($patterns[0]->occurrenceCount)->toBe(3);
expect($patterns[0]->severity)->toBe(ErrorSeverity::CRITICAL);
// Verify ErrorReporter created individual reports
$reports = $this->errorReportStorage->findRecent(10);
expect($reports)->toHaveCount(3);
foreach ($reports as $report) {
expect($report->message)->toContain('Memory limit exceeded');
}
});
it('handles different error types through pipeline', function () {
// Database error using migrated DatabaseException
$dbException = DatabaseException::queryFailed(
sql: 'SELECT * FROM users WHERE id = ?',
error: 'Table users does not exist'
);
// System error
$sysException = FrameworkException::create(
SystemErrorCode::RESOURCE_EXHAUSTED,
'CPU limit exceeded'
);
// Process both
$this->errorHandler->createHttpResponse($dbException);
$this->errorHandler->createHttpResponse($sysException);
// Verify ErrorAggregator
$events = $this->errorStorage->getRecentEvents(10);
expect($events)->toHaveCount(2);
$severities = array_map(fn($e) => $e->severity, $events);
expect($severities)->toContain(ErrorSeverity::ERROR); // Database
expect($severities)->toContain(ErrorSeverity::CRITICAL); // System
// Verify ErrorReporter
$reports = $this->errorReportStorage->findRecent(10);
expect($reports)->toHaveCount(2);
});
it('propagates error context through entire pipeline', function () {
$exception = FrameworkException::create(
DatabaseErrorCode::QUERY_FAILED,
'Complex query failed'
)->withData([
'query' => 'SELECT * FROM large_table WHERE id IN (...)',
'execution_time' => 5.2
]);
// Process exception
$this->errorHandler->createHttpResponse($exception);
// Verify ErrorAggregator has context
$events = $this->errorStorage->getRecentEvents(10);
expect($events[0]->context)->toBeArray();
expect($events[0]->context)->toHaveKey('query');
expect($events[0]->context)->toHaveKey('execution_time');
// Verify ErrorReporter has full context
$reports = $this->errorReportStorage->findRecent(10);
expect($reports[0]->context)->toBeArray();
expect($reports[0]->context)->toHaveKey('exception');
});
it('uses interfaces for dependency injection', function () {
// Verify ErrorHandler accepts interfaces
expect($this->errorHandler)
->toBeInstanceOf(ErrorHandler::class);
// Verify dependencies are interface-based (via reflection)
$reflection = new \ReflectionClass(ErrorHandler::class);
$constructor = $reflection->getConstructor();
$aggregatorParam = null;
$reporterParam = null;
foreach ($constructor->getParameters() as $param) {
if ($param->getName() === 'errorAggregator') {
$aggregatorParam = $param;
}
if ($param->getName() === 'errorReporter') {
$reporterParam = $param;
}
}
// Verify parameters use interfaces
expect($aggregatorParam->getType()->getName())
->toBe(ErrorAggregatorInterface::class);
expect($reporterParam->getType()->getName())
->toBe(ErrorReporterInterface::class);
});
});