diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 2b39208fcd..db1bf99edb 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -786,125 +786,132 @@ Http::shutdown() Http::shutdown() ->groups(['api']) ->inject('route') - ->inject('request') ->inject('response') ->inject('project') - ->inject('user') ->inject('queueForEvents') - ->inject('auditContext') - ->inject('publisherForAudits') - ->inject('usage') - ->inject('publisherForUsage') ->inject('publisherForFunctions') ->inject('queueForWebhooks') ->inject('queueForRealtime') ->inject('dbForProject') - ->inject('authorization') - ->inject('timelimit') ->inject('eventProcessor') - ->inject('bus') - ->inject('apiKey') - ->inject('mode') - ->action(function (Route $route, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, Context $usage, UsagePublisher $publisherForUsage, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, Authorization $authorization, callable $timelimit, EventProcessor $eventProcessor, Bus $bus, ?Key $apiKey, string $mode) use ($parseLabel) { + ->action(function (Route $route, Response $response, Document $project, Event $queueForEvents, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, EventProcessor $eventProcessor) { + if (empty($queueForEvents->getEvent())) { + return; + } - $responsePayload = $response->getPayload(); + if (empty($queueForEvents->getPayload())) { + $queueForEvents->setPayload($response->getPayload()); + } - if (! empty($queueForEvents->getEvent())) { - if (empty($queueForEvents->getPayload())) { - $queueForEvents->setPayload($responsePayload); - } + // Get project and function/webhook events (cached) + $functionsEvents = $eventProcessor->getFunctionsEvents($project, $dbForProject); + $webhooksEvents = $eventProcessor->getWebhooksEvents($project); - // Get project and function/webhook events (cached) - $functionsEvents = $eventProcessor->getFunctionsEvents($project, $dbForProject); - $webhooksEvents = $eventProcessor->getWebhooksEvents($project); + // Generate events for this operation + $generatedEvents = Event::generateEvents( + $queueForEvents->getEvent(), + $queueForEvents->getParams() + ); - // Generate events for this operation - $generatedEvents = Event::generateEvents( - $queueForEvents->getEvent(), - $queueForEvents->getParams() - ); + $allowedOnConsole = !empty(\array_intersect($route->getGroups(), Realtime::CONSOLE_ALLOWLIST)); + if ($project->getId() !== 'console' || $allowedOnConsole) { + $queueForRealtime + ->from($queueForEvents) + ->trigger(); + } - $allowedOnConsole = !empty(\array_intersect($route->getGroups(), Realtime::CONSOLE_ALLOWLIST)); - if ($project->getId() !== 'console' || $allowedOnConsole) { - $queueForRealtime - ->from($queueForEvents) - ->trigger(); - } - - // Only trigger functions if there are matching function events - if (! empty($functionsEvents)) { - foreach ($generatedEvents as $event) { - if (isset($functionsEvents[$event])) { - $publisherForFunctions->enqueue(FunctionMessage::fromEvent( - event: $queueForEvents->getEvent(), - params: $queueForEvents->getParams(), - project: $queueForEvents->getProject(), - user: $queueForEvents->getUser(), - userId: $queueForEvents->getUserId(), - payload: $queueForEvents->getPayload(), - platform: $queueForEvents->getPlatform(), - )); - break; - } - } - } - - // Only trigger webhooks if there are matching webhook events - if (! empty($webhooksEvents)) { - foreach ($generatedEvents as $event) { - if (isset($webhooksEvents[$event])) { - $queueForWebhooks - ->from($queueForEvents) - ->trigger(); - break; - } + // Only trigger functions if there are matching function events + if (! empty($functionsEvents)) { + foreach ($generatedEvents as $event) { + if (isset($functionsEvents[$event])) { + $publisherForFunctions->enqueue(FunctionMessage::fromEvent( + event: $queueForEvents->getEvent(), + params: $queueForEvents->getParams(), + project: $queueForEvents->getProject(), + user: $queueForEvents->getUser(), + userId: $queueForEvents->getUserId(), + payload: $queueForEvents->getPayload(), + platform: $queueForEvents->getPlatform(), + )); + break; } } } - $requestParams = $route->getParamsValues(); + // Only trigger webhooks if there are matching webhook events + if (! empty($webhooksEvents)) { + foreach ($generatedEvents as $event) { + if (isset($webhooksEvents[$event])) { + $queueForWebhooks + ->from($queueForEvents) + ->trigger(); + break; + } + } + } + }); - /** - * Abuse labels - */ +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('timelimit') + ->action(function (Route $route, Request $request, Response $response, Document $project, User $user, callable $timelimit) { $abuseEnabled = System::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled'; $abuseResetCode = $route->getLabel('abuse-reset', []); $abuseResetCode = \is_array($abuseResetCode) ? $abuseResetCode : [$abuseResetCode]; - if ($abuseEnabled && \count($abuseResetCode) > 0 && \in_array($response->getStatusCode(), $abuseResetCode)) { - $abuseKeyLabel = $route->getLabel('abuse-key', 'url:{url},ip:{ip}'); - $abuseKeyLabel = (! is_array($abuseKeyLabel)) ? [$abuseKeyLabel] : $abuseKeyLabel; - - foreach ($abuseKeyLabel as $abuseKey) { - $start = $request->getContentRangeStart(); - $end = $request->getContentRangeEnd(); - $timeLimit = $timelimit($abuseKey, $route->getLabel('abuse-limit', 0), $route->getLabel('abuse-time', 3600)); - $timeLimit - ->setParam('{projectId}', $project->getId()) - ->setParam('{userId}', $user->getId()) - ->setParam('{userAgent}', $request->getUserAgent('')) - ->setParam('{ip}', $request->getIP()) - ->setParam('{url}', $request->getHostname() . $route->getPath()) - ->setParam('{method}', $request->getMethod()) - ->setParam('{chunkId}', (int) ($start / ($end + 1 - $start))); - - foreach ($request->getParams() as $key => $value) { // Set request params as potential abuse keys - if (! empty($value)) { - $timeLimit->setParam('{param-' . $key . '}', (\is_array($value)) ? \json_encode($value) : $value); - } - } - - $abuse = new Abuse($timeLimit); - $abuse->reset(); - } + if (! $abuseEnabled || \count($abuseResetCode) === 0 || ! \in_array($response->getStatusCode(), $abuseResetCode)) { + return; } - /** - * Audit labels - */ + $abuseKeyLabel = $route->getLabel('abuse-key', 'url:{url},ip:{ip}'); + $abuseKeyLabel = (! is_array($abuseKeyLabel)) ? [$abuseKeyLabel] : $abuseKeyLabel; + + foreach ($abuseKeyLabel as $abuseKey) { + $start = $request->getContentRangeStart(); + $end = $request->getContentRangeEnd(); + $timeLimit = $timelimit($abuseKey, $route->getLabel('abuse-limit', 0), $route->getLabel('abuse-time', 3600)); + $timeLimit + ->setParam('{projectId}', $project->getId()) + ->setParam('{userId}', $user->getId()) + ->setParam('{userAgent}', $request->getUserAgent('')) + ->setParam('{ip}', $request->getIP()) + ->setParam('{url}', $request->getHostname() . $route->getPath()) + ->setParam('{method}', $request->getMethod()) + ->setParam('{chunkId}', (int) ($start / ($end + 1 - $start))); + + foreach ($request->getParams() as $key => $value) { // Set request params as potential abuse keys + if (! empty($value)) { + $timeLimit->setParam('{param-' . $key . '}', (\is_array($value)) ? \json_encode($value) : $value); + } + } + + $abuse = new Abuse($timeLimit); + $abuse->reset(); + } + }); + +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('params') + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('auditContext') + ->inject('publisherForAudits') + ->inject('mode') + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, AuditContext $auditContext, Audit $publisherForAudits, string $mode) use ($parseLabel) { + $responsePayload = $response->getPayload(); + $pattern = $route->getLabel('audits.resource', null); if (! empty($pattern)) { - $resource = $parseLabel($pattern, $responsePayload, $requestParams, $user, $project); + $resource = $parseLabel($pattern, $responsePayload, $params, $user, $project); if (! empty($resource) && $resource !== $pattern) { $auditContext->resource = $resource; } @@ -951,97 +958,129 @@ Http::shutdown() $publisherForAudits->enqueue(AuditMessage::fromContext($auditContext)); } + }); - // Cache label - $useCache = $route->getLabel('cache', false); - if ($useCache) { - $resource = $resourceType = null; - $data = $response->getPayload(); - $statusCode = $response->getStatusCode(); - if (! empty($data['payload']) && $statusCode >= 200 && $statusCode < 300) { - $pattern = $route->getLabel('cache.resource', null); - if (! empty($pattern)) { - $resource = $parseLabel($pattern, $responsePayload, $requestParams, $user, $project); - } +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('params') + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('dbForProject') + ->inject('authorization') + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Database $dbForProject, Authorization $authorization) use ($parseLabel) { + if (! $route->getLabel('cache', false)) { + return; + } - $pattern = $route->getLabel('cache.resourceType', null); - if (! empty($pattern)) { - $resourceType = $parseLabel($pattern, $responsePayload, $requestParams, $user, $project); - } + $data = $response->getPayload(); + $statusCode = $response->getStatusCode(); + if (empty($data['payload']) || $statusCode < 200 || $statusCode >= 300) { + return; + } - $cache = new Cache( - new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) - ); + $cache = new Cache( + new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) + ); + $key = $request->cacheIdentifier(); + $signature = md5($data['payload']); + $now = DateTime::now(); + $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); - $key = $request->cacheIdentifier(); - $signature = md5($data['payload']); + // First request for this resource: record it and persist the payload. + if ($cacheLog->isEmpty()) { + // Resolve resource labels lazily — only needed when creating the entry. + $requestParams = []; + foreach ($route->getParams() as $paramKey => $param) { + $requestParams[$paramKey] = $params[$paramKey] ?? $request->getParam($paramKey, $param['default']); + } + + $resourcePattern = $route->getLabel('cache.resource', null); + $resourceTypePattern = $route->getLabel('cache.resourceType', null); + + try { + $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ + '$id' => $key, + 'resource' => empty($resourcePattern) ? null : $parseLabel($resourcePattern, $data, $requestParams, $user, $project), + 'resourceType' => empty($resourceTypePattern) ? null : $parseLabel($resourceTypePattern, $data, $requestParams, $user, $project), + 'mimeType' => $response->getContentType(), + 'accessedAt' => $now, + 'signature' => $signature, + ]))); + $cache->save($key, $data['payload']); + + return; + } catch (DuplicateException) { + // Race condition: another concurrent request already created the entry. $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); - $accessedAt = $cacheLog->getAttribute('accessedAt', 0); - $now = DateTime::now(); - if ($cacheLog->isEmpty()) { - try { - $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ - '$id' => $key, - 'resource' => $resource, - 'resourceType' => $resourceType, - 'mimeType' => $response->getContentType(), - 'accessedAt' => $now, - 'signature' => $signature, - ]))); - } catch (DuplicateException) { - // Race condition: another concurrent request already created the cache document - $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); - } - } elseif (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $accessedAt) { - $cacheLog->setAttribute('accessedAt', $now); - $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ - 'accessedAt' => $cacheLog->getAttribute('accessedAt') - ]))); - // Overwrite the file every APP_CACHE_UPDATE seconds to update the file modified time that is used in the TTL checks in cache->load() - $cache->save($key, $data['payload']); - } - - if ($signature !== $cacheLog->getAttribute('signature')) { - $cache->save($key, $data['payload']); - } } } - if ($project->getId() !== 'console') { - if (! $user->isPrivileged($authorization->getRoles())) { - $bus->dispatch(new RequestCompleted( - project: $project->getArrayCopy(), - request: $request, - response: $response, - )); - } + // Existing entry: refresh the access time once per APP_CACHE_UPDATE window + // (keeps the file mtime current for TTL checks) and re-persist on content change. + $stale = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $cacheLog->getAttribute('accessedAt', 0); + if ($stale) { + $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ + 'accessedAt' => $now, + ]))); + } - // Publish usage metrics if context has data - if (! $usage->isEmpty()) { - $metrics = $usage->getMetrics(); + if ($stale || $signature !== $cacheLog->getAttribute('signature')) { + $cache->save($key, $data['payload']); + } + }); - // Filter out API key disabled metrics using suffix pattern matching - $disabledMetrics = $apiKey?->getDisabledMetrics() ?? []; - if (! empty($disabledMetrics)) { - $metrics = array_values(array_filter($metrics, function ($metric) use ($disabledMetrics) { - foreach ($disabledMetrics as $pattern) { - if (str_ends_with($metric['key'], $pattern)) { - return false; - } +Http::shutdown() + ->groups(['api']) + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('usage') + ->inject('publisherForUsage') + ->inject('authorization') + ->inject('bus') + ->inject('apiKey') + ->action(function (Request $request, Response $response, Document $project, User $user, Context $usage, UsagePublisher $publisherForUsage, Authorization $authorization, Bus $bus, ?Key $apiKey) { + if ($project->getId() === 'console') { + return; + } + + if (! $user->isPrivileged($authorization->getRoles())) { + $bus->dispatch(new RequestCompleted( + project: $project->getArrayCopy(), + request: $request, + response: $response, + )); + } + + // Publish usage metrics if context has data + if (! $usage->isEmpty()) { + $metrics = $usage->getMetrics(); + + // Filter out API key disabled metrics using suffix pattern matching + $disabledMetrics = $apiKey?->getDisabledMetrics() ?? []; + if (! empty($disabledMetrics)) { + $metrics = array_values(array_filter($metrics, function ($metric) use ($disabledMetrics) { + foreach ($disabledMetrics as $pattern) { + if (str_ends_with($metric['key'], $pattern)) { + return false; } + } - return true; - })); - } - - $message = new UsageMessage( - project: $project, - metrics: $metrics, - reduce: $usage->getReduce() - ); - - $publisherForUsage->enqueue($message); + return true; + })); } + + $message = new UsageMessage( + project: $project, + metrics: $metrics, + reduce: $usage->getReduce() + ); + + $publisherForUsage->enqueue($message); } }); diff --git a/app/init/worker/message.php b/app/init/worker/message.php index ea6ba13aec..7cbfe19b0f 100644 --- a/app/init/worker/message.php +++ b/app/init/worker/message.php @@ -18,6 +18,7 @@ use Utopia\Logger\Log; use Utopia\Pools\Group; use Utopia\Queue\Publisher; use Utopia\Registry\Registry; +use Utopia\Span\Span; use Utopia\Storage\Device\Telemetry as TelemetryDevice; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; @@ -55,7 +56,11 @@ return function (Container $container): void { return $project; } - return $dbForPlatform->getDocument('projects', $project->getId()); + $project = $dbForPlatform->getDocument('projects', $project->getId()); + + Span::add('project.id', $project->getId()); + + return $project; }, ['message', 'dbForPlatform']); $container->set('dbForProject', fn (DatabaseFactory $databaseFactory, Document $project, Database $dbForPlatform) => $project->isEmpty() || $project->getId() === 'console' diff --git a/composer.json b/composer.json index 3588743d06..ad18771699 100644 --- a/composer.json +++ b/composer.json @@ -67,7 +67,7 @@ "utopia-php/emails": "0.7.*", "utopia-php/dns": "1.7.*", "utopia-php/dsn": "0.2.1", - "utopia-php/http": "2.0.0-rc3", + "utopia-php/http": "^2.0@RC", "utopia-php/fetch": "^1.1", "utopia-php/validators": "0.2.*", "utopia-php/image": "0.8.*", @@ -85,7 +85,7 @@ "utopia-php/registry": "0.5.*", "utopia-php/storage": "2.*", "utopia-php/system": "0.10.*", - "utopia-php/telemetry": "0.2.*", + "utopia-php/telemetry": "^0.4", "utopia-php/vcs": "4.*", "utopia-php/websocket": "1.0.*", "matomo/device-detector": "6.4.*", diff --git a/composer.lock b/composer.lock index 6174709d56..6bcfda13f1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "95a83cce1d03ceb2bfc777309ebe79e9", + "content-hash": "5e251e32e77d0eeac8d0aad25188f08a", "packages": [ { "name": "adhocore/jwt", @@ -2712,16 +2712,16 @@ }, { "name": "symfony/http-client", - "version": "v7.4.9", + "version": "v7.4.13", "source": { "type": "git", "url": "https://github.com/symfony/http-client.git", - "reference": "7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6" + "reference": "e8a112b8415707265a7e614278136a9d92989a6a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-client/zipball/7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6", - "reference": "7e941c6abf4e3bf7dca160bf0e11ef36a9f832f6", + "url": "https://api.github.com/repos/symfony/http-client/zipball/e8a112b8415707265a7e614278136a9d92989a6a", + "reference": "e8a112b8415707265a7e614278136a9d92989a6a", "shasum": "" }, "require": { @@ -2789,7 +2789,7 @@ "http" ], "support": { - "source": "https://github.com/symfony/http-client/tree/v7.4.9" + "source": "https://github.com/symfony/http-client/tree/v7.4.13" }, "funding": [ { @@ -2809,7 +2809,7 @@ "type": "tidelift" } ], - "time": "2026-04-29T13:25:15+00:00" + "time": "2026-05-24T09:57:54+00:00" }, { "name": "symfony/http-client-contracts", @@ -2980,16 +2980,16 @@ }, { "name": "symfony/polyfill-php82", - "version": "v1.37.0", + "version": "v1.38.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php82.git", - "reference": "34808efe3e68f69685796f7c253a2f1d8ea9df59" + "reference": "002dc0cfe5fd4ed6033d48f27d4f19a486c4b04b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php82/zipball/34808efe3e68f69685796f7c253a2f1d8ea9df59", - "reference": "34808efe3e68f69685796f7c253a2f1d8ea9df59", + "url": "https://api.github.com/repos/symfony/polyfill-php82/zipball/002dc0cfe5fd4ed6033d48f27d4f19a486c4b04b", + "reference": "002dc0cfe5fd4ed6033d48f27d4f19a486c4b04b", "shasum": "" }, "require": { @@ -3036,7 +3036,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php82/tree/v1.37.0" + "source": "https://github.com/symfony/polyfill-php82/tree/v1.38.1" }, "funding": [ { @@ -3056,20 +3056,20 @@ "type": "tidelift" } ], - "time": "2026-04-10T16:19:22+00:00" + "time": "2026-05-26T12:45:58+00:00" }, { "name": "symfony/polyfill-php83", - "version": "v1.37.0", + "version": "v1.38.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php83.git", - "reference": "3600c2cb22399e25bb226e4a135ce91eeb2a6149" + "reference": "8339098cae28673c15cce00d80734af0453054e2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php83/zipball/3600c2cb22399e25bb226e4a135ce91eeb2a6149", - "reference": "3600c2cb22399e25bb226e4a135ce91eeb2a6149", + "url": "https://api.github.com/repos/symfony/polyfill-php83/zipball/8339098cae28673c15cce00d80734af0453054e2", + "reference": "8339098cae28673c15cce00d80734af0453054e2", "shasum": "" }, "require": { @@ -3116,7 +3116,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php83/tree/v1.37.0" + "source": "https://github.com/symfony/polyfill-php83/tree/v1.38.1" }, "funding": [ { @@ -3136,7 +3136,7 @@ "type": "tidelift" } ], - "time": "2026-04-10T17:25:58+00:00" + "time": "2026-05-26T12:51:13+00:00" }, { "name": "symfony/polyfill-php85", @@ -3669,16 +3669,16 @@ }, { "name": "utopia-php/circuit-breaker", - "version": "0.3.0", + "version": "0.3.1", "source": { "type": "git", "url": "https://github.com/utopia-php/circuit-breaker.git", - "reference": "064243c1667778c00abf027ff53a735a228776de" + "reference": "db5d77f6c99ebce2ee81bd8ed4ae8f41bd2b0828" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/circuit-breaker/zipball/064243c1667778c00abf027ff53a735a228776de", - "reference": "064243c1667778c00abf027ff53a735a228776de", + "url": "https://api.github.com/repos/utopia-php/circuit-breaker/zipball/db5d77f6c99ebce2ee81bd8ed4ae8f41bd2b0828", + "reference": "db5d77f6c99ebce2ee81bd8ed4ae8f41bd2b0828", "shasum": "" }, "require": { @@ -3688,7 +3688,7 @@ "laravel/pint": "^1.29", "phpstan/phpstan": "^2.1", "phpunit/phpunit": "^10.0", - "utopia-php/telemetry": "0.2.*" + "utopia-php/telemetry": "^0.4" }, "suggest": { "ext-opentelemetry": "Required by utopia-php/telemetry when using OpenTelemetry metrics.", @@ -3725,9 +3725,9 @@ ], "support": { "issues": "https://github.com/utopia-php/circuit-breaker/issues", - "source": "https://github.com/utopia-php/circuit-breaker/tree/0.3.0" + "source": "https://github.com/utopia-php/circuit-breaker/tree/0.3.1" }, - "time": "2026-05-12T04:27:08+00:00" + "time": "2026-05-29T12:12:23+00:00" }, { "name": "utopia-php/cli", @@ -4346,16 +4346,16 @@ }, { "name": "utopia-php/http", - "version": "2.0.0-rc3", + "version": "2.0.0-rc4", "source": { "type": "git", "url": "https://github.com/utopia-php/http.git", - "reference": "e1a51a2eb906a3d461378fb551a647ef01714f1c" + "reference": "0396a959c6a3be7c16b4b72283347327a75d1f92" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/e1a51a2eb906a3d461378fb551a647ef01714f1c", - "reference": "e1a51a2eb906a3d461378fb551a647ef01714f1c", + "url": "https://api.github.com/repos/utopia-php/http/zipball/0396a959c6a3be7c16b4b72283347327a75d1f92", + "reference": "0396a959c6a3be7c16b4b72283347327a75d1f92", "shasum": "" }, "require": { @@ -4363,7 +4363,7 @@ "utopia-php/compression": "0.1.*", "utopia-php/di": "0.3.*", "utopia-php/servers": "0.4.*", - "utopia-php/telemetry": "0.2.*", + "utopia-php/telemetry": "^0.4", "utopia-php/validators": "0.2.*" }, "require-dev": { @@ -4396,9 +4396,9 @@ ], "support": { "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/2.0.0-rc3" + "source": "https://github.com/utopia-php/http/tree/2.0.0-rc4" }, - "time": "2026-05-21T14:42:41+00:00" + "time": "2026-06-02T04:17:22+00:00" }, { "name": "utopia-php/image", @@ -4976,25 +4976,26 @@ }, { "name": "utopia-php/queue", - "version": "0.18.4", + "version": "0.18.6", "source": { "type": "git", "url": "https://github.com/utopia-php/queue.git", - "reference": "1323b52f39b899c580dbb7af8562fc53d4e13e62" + "reference": "954cd3788b37697b83fd2757adcd93422f21a846" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/queue/zipball/1323b52f39b899c580dbb7af8562fc53d4e13e62", - "reference": "1323b52f39b899c580dbb7af8562fc53d4e13e62", + "url": "https://api.github.com/repos/utopia-php/queue/zipball/954cd3788b37697b83fd2757adcd93422f21a846", + "reference": "954cd3788b37697b83fd2757adcd93422f21a846", "shasum": "" }, "require": { "php": ">=8.3", "php-amqplib/php-amqplib": "^3.7", "utopia-php/di": "0.3.*", + "utopia-php/lock": "0.2.*", "utopia-php/pools": "1.*", "utopia-php/servers": "0.4.*", - "utopia-php/telemetry": "0.2.*", + "utopia-php/telemetry": "0.4.*", "utopia-php/validators": "0.2.*" }, "require-dev": { @@ -5036,9 +5037,9 @@ ], "support": { "issues": "https://github.com/utopia-php/queue/issues", - "source": "https://github.com/utopia-php/queue/tree/0.18.4" + "source": "https://github.com/utopia-php/queue/tree/0.18.6" }, - "time": "2026-05-21T14:43:24+00:00" + "time": "2026-06-01T19:47:57+00:00" }, { "name": "utopia-php/registry", @@ -5192,16 +5193,16 @@ }, { "name": "utopia-php/storage", - "version": "2.0.3", + "version": "2.0.4", "source": { "type": "git", "url": "https://github.com/utopia-php/storage.git", - "reference": "37129cf0bfcc03210172000e4388d4d3495ae013" + "reference": "a604b0ee011c4f366d53167caf2cf6b0f1d62d7e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/storage/zipball/37129cf0bfcc03210172000e4388d4d3495ae013", - "reference": "37129cf0bfcc03210172000e4388d4d3495ae013", + "url": "https://api.github.com/repos/utopia-php/storage/zipball/a604b0ee011c4f366d53167caf2cf6b0f1d62d7e", + "reference": "a604b0ee011c4f366d53167caf2cf6b0f1d62d7e", "shasum": "" }, "require": { @@ -5211,7 +5212,7 @@ "ext-zlib": "*", "php": ">=8.1", "utopia-php/system": "0.*.*", - "utopia-php/telemetry": "0.2.*", + "utopia-php/telemetry": "^0.4", "utopia-php/validators": "0.2.*" }, "require-dev": { @@ -5238,9 +5239,9 @@ ], "support": { "issues": "https://github.com/utopia-php/storage/issues", - "source": "https://github.com/utopia-php/storage/tree/2.0.3" + "source": "https://github.com/utopia-php/storage/tree/2.0.4" }, - "time": "2026-05-15T09:42:32+00:00" + "time": "2026-06-02T04:17:22+00:00" }, { "name": "utopia-php/system", @@ -5300,16 +5301,16 @@ }, { "name": "utopia-php/telemetry", - "version": "0.2.0", + "version": "0.4.0", "source": { "type": "git", "url": "https://github.com/utopia-php/telemetry.git", - "reference": "9997ebf59bb77920a7223ad73d834a76b09152c3" + "reference": "e0630df7d8176833cd4882f78814a5b893dcb0e0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/9997ebf59bb77920a7223ad73d834a76b09152c3", - "reference": "9997ebf59bb77920a7223ad73d834a76b09152c3", + "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/e0630df7d8176833cd4882f78814a5b893dcb0e0", + "reference": "e0630df7d8176833cd4882f78814a5b893dcb0e0", "shasum": "" }, "require": { @@ -5349,9 +5350,9 @@ ], "support": { "issues": "https://github.com/utopia-php/telemetry/issues", - "source": "https://github.com/utopia-php/telemetry/tree/0.2.0" + "source": "https://github.com/utopia-php/telemetry/tree/0.4.0" }, - "time": "2025-12-17T07:56:38+00:00" + "time": "2026-05-29T11:57:04+00:00" }, { "name": "utopia-php/validators", @@ -8550,7 +8551,9 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": {}, + "stability-flags": { + "utopia-php/http": 5 + }, "prefer-stable": true, "prefer-lowest": false, "platform": { diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index ee8494b382..a6b13857a5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -5,7 +5,6 @@ namespace Appwrite\Platform\Modules\Databases\Workers; use Appwrite\Event\Message\Database as DatabaseMessage; use Appwrite\Event\Realtime; use Exception; -use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception as DatabaseException; @@ -18,6 +17,7 @@ use Utopia\Database\Query; use Utopia\Logger\Log; use Utopia\Platform\Action; use Utopia\Queue\Message; +use Utopia\Span\Span; class Databases extends Action { @@ -62,10 +62,21 @@ class Databases extends Action } $databaseMessage = DatabaseMessage::fromArray($payload); + $type = $databaseMessage->type; + Span::add('payload.type', $type); + $document = $databaseMessage->row ?? $databaseMessage->document ?? new Document(); + Span::add('database.document.id', $document->getId()); + $collection = $databaseMessage->table ?? $databaseMessage->collection ?? new Document(); + Span::add('database.collection.id', $collection->getId()); + $database = $databaseMessage->database ?? new Document(); + Span::add('database.id', $database->getId()); + Span::add('database.namespace', $dbForProject->getNamespace()); + Span::add('database.tenant', $dbForProject->getTenant()); + /** * @var Database $dbForDatabases */ @@ -88,14 +99,6 @@ class Databases extends Action DATABASE_TYPE_DELETE_INDEX => $this->deleteIndex($database, $collection, $document, $project, $dbForPlatform, $dbForProject, $dbForDatabases, $queueForRealtime), default => throw new Exception('No database operation for type: ' . \strval($type)), }; - - Console::info("Finished processing database operation: \n" . \json_encode([ - 'type' => $type, - 'projectId' => $project->getId(), - 'databaseId' => $database->getId(), - 'collectionId' => $collection->getId(), - 'documentId' => $document->getId(), - ], JSON_PRETTY_PRINT)); } /** @@ -206,8 +209,6 @@ class Databases extends Action $dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available')); } catch (\Throwable $e) { - Console::error($e->getMessage()); - if ($e instanceof DatabaseException) { $attribute->setAttribute('error', $e->getMessage()); if (! $relatedAttribute->isEmpty()) { @@ -309,7 +310,7 @@ class Databases extends Action } } catch (NotFound $e) { - Console::error($e->getMessage()); + Span::add('database.attribute.not_found', $e->getMessage()); $dbForProject->deleteDocument('attributes', $attribute->getId()); @@ -318,8 +319,6 @@ class Databases extends Action } } catch (\Throwable $e) { - Console::error($e->getMessage()); - if ($e instanceof DatabaseException) { $attribute->setAttribute('error', $e->getMessage()); if (!$relatedAttribute->isEmpty()) { @@ -456,7 +455,6 @@ class Databases extends Action } $dbForProject->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'available')); } catch (\Throwable $e) { - Console::error($e->getMessage()); if ($e instanceof DatabaseException) { $index->setAttribute('error', $e->getMessage()); } @@ -512,8 +510,6 @@ class Databases extends Action $dbForProject->deleteDocument('indexes', $index->getId()); $index->setAttribute('status', 'deleted'); } catch (\Throwable $e) { - Console::error($e->getMessage()); - if ($e instanceof DatabaseException) { $index->setAttribute('error', $e->getMessage()); } @@ -613,7 +609,7 @@ class Databases extends Action */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { - $start = \microtime(true); + Span::add('delete_by_group.collection.id', $collectionId); try { $count = $database->deleteDocuments( @@ -623,13 +619,11 @@ class Databases extends Action $callback ); } catch (\Throwable $th) { - $tenant = $database->getSharedTables() ? 'Tenant:'.$database->getTenant() : ''; - Console::error("Failed to delete documents/rows for collection/table: {$database->getNamespace()}_{$collectionId} {$tenant} :{$th->getMessage()}"); + Span::add('delete_by_group.error', $th->getMessage()); return; } - $end = \microtime(true); - Console::info("Deleted {$count} documents/rows by group in " . ($end - $start) . " seconds"); + Span::add('delete_by_group.count', $count); } /** diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 92e00cd4e6..dd0f9d541b 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -297,13 +297,9 @@ class Builds extends Action $templateReferenceValue = $template->getAttribute('referenceValue', ''); $templateRootDirectory = $template->getAttribute('rootDirectory', ''); - $templateRootDirectory = \trim($templateRootDirectory, '/'); - while (\str_starts_with($templateRootDirectory, './')) { - $templateRootDirectory = \substr($templateRootDirectory, 2); - } - if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { - throw new \Exception('Invalid template root directory'); - } + $templateRootDirectory = \rtrim($templateRootDirectory, '/'); + $templateRootDirectory = \ltrim($templateRootDirectory, '.'); + $templateRootDirectory = \ltrim($templateRootDirectory, '/'); if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) { $stdout = ''; @@ -366,10 +362,9 @@ class Builds extends Action // VCS and VCS+Temaplte $tmpDirectory = '/tmp/builds/' . $deploymentId . '/code'; $rootDirectory = $resource->getAttribute('providerRootDirectory', ''); - $rootDirectory = \trim($rootDirectory, '/'); - if ($rootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $rootDirectory)) { - throw new \Exception('Invalid root directory'); - } + $rootDirectory = \rtrim($rootDirectory, '/'); + $rootDirectory = \ltrim($rootDirectory, '.'); + $rootDirectory = \ltrim($rootDirectory, '/'); $owner = $github->getOwnerName($providerInstallationId); $repositoryName = $github->getRepositoryName($providerRepositoryId); @@ -425,13 +420,9 @@ class Builds extends Action $templateReferenceValue = $template->getAttribute('referenceValue', ''); $templateRootDirectory = $template->getAttribute('rootDirectory', ''); - $templateRootDirectory = \trim($templateRootDirectory, '/'); - while (\str_starts_with($templateRootDirectory, './')) { - $templateRootDirectory = \substr($templateRootDirectory, 2); - } - if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { - throw new \Exception('Invalid template root directory'); - } + $templateRootDirectory = \rtrim($templateRootDirectory, '/'); + $templateRootDirectory = \ltrim($templateRootDirectory, '.'); + $templateRootDirectory = \ltrim($templateRootDirectory, '/'); if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) { // Clone template repo diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php index 348b19c039..69b27af25c 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php @@ -497,6 +497,8 @@ class Create extends Action ->setParam('bucketId', $bucket->getId()) ->setParam('fileId', $file->getId()) ->setContext('bucket', $bucket); + } else { + $queueForEvents->reset(); } $metadata = null; // was causing leaks as it was passed by reference