From f13bf872674b1d840e53365fc8b7a08f96c24ca2 Mon Sep 17 00:00:00 2001 From: Hemachandar Date: Mon, 2 Feb 2026 17:37:43 +0530 Subject: [PATCH] Add span --- app/cli.php | 5 ++ src/Appwrite/Platform/Tasks/Interval.php | 62 ++++++++++++++++-------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/app/cli.php b/app/cli.php index 4b85ba35c6..af67f4e0e6 100644 --- a/app/cli.php +++ b/app/cli.php @@ -30,6 +30,9 @@ use Utopia\Pools\Group; use Utopia\Queue\Broker\Pool as BrokerPool; use Utopia\Queue\Publisher; use Utopia\Registry\Registry; +use Utopia\Span\Exporter; +use Utopia\Span\Span; +use Utopia\Span\Storage; use Utopia\System\System; use Utopia\Telemetry\Adapter\None as NoTelemetry; @@ -325,4 +328,6 @@ $cli $cli->shutdown()->action(fn () => Timer::clearAll()); Runtime::enableCoroutine(SWOOLE_HOOK_ALL); +Span::setStorage(new Storage\Coroutine()); +Span::addExporter(new Exporter\Stdout()); run($cli->run(...)); diff --git a/src/Appwrite/Platform/Tasks/Interval.php b/src/Appwrite/Platform/Tasks/Interval.php index fa642ff181..7ee9b02a95 100644 --- a/src/Appwrite/Platform/Tasks/Interval.php +++ b/src/Appwrite/Platform/Tasks/Interval.php @@ -13,6 +13,7 @@ use Utopia\Database\DateTime as DatabaseDateTime; use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Platform\Action; +use Utopia\Span\Span; use Utopia\System\System; class Interval extends Action @@ -58,15 +59,13 @@ class Interval extends Action foreach ($tasks as $task) { $timers[] = Timer::tick($task['interval'], function () use ($task, $dbForPlatform, $getProjectDB, $queueForCertificates) { $taskName = $task['name']; - $time = DatabaseDateTime::now(); - Console::info("[{$time}] Running task '{$taskName}'"); + Span::init("interval.{$taskName}"); try { $task['callback']($dbForPlatform, $getProjectDB, $queueForCertificates); - $time = DatabaseDateTime::now(); - Console::info("[{$time}] Completed task '{$taskName}'"); } catch (\Exception $e) { - $time = DatabaseDateTime::now(); - Console::error("[{$time}] Task '{$taskName}' ended with a failure: " . $e->getMessage()); + Span::error($e); + } finally { + Span::current()->finish(); } }); } @@ -109,22 +108,35 @@ class Interval extends Action Query::limit(100), // Reasonable pagination limit ]); - if (\count($rules) === 0) { - Console::log("[{$time}] No rules for domain verification."); + $scanned = \count($rules); + Span::add("interval.domain-verification.scanned", $scanned); + + if ($scanned === 0) { + Span::add("interval.domain-verification.processed", 0); + Span::add("interval.domain-verification.failed", 0); return; // No rules to verify } - Console::log("[{$time}] Found " . \count($rules) . " rules for domain verification, scheduling jobs."); + $processed = 0; + $failed = 0; foreach ($rules as $rule) { - $queueForCertificates - ->setDomain(new Document([ - 'domain' => $rule->getAttribute('domain'), - 'domainType' => $rule->getAttribute('deploymentResourceType', $rule->getAttribute('type')), - ])) - ->setAction(Certificate::ACTION_DOMAIN_VERIFICATION) - ->trigger(); + try { + $queueForCertificates + ->setDomain(new Document([ + 'domain' => $rule->getAttribute('domain'), + 'domainType' => $rule->getAttribute('deploymentResourceType', $rule->getAttribute('type')), + ])) + ->setAction(Certificate::ACTION_DOMAIN_VERIFICATION) + ->trigger(); + $processed++; + } catch (\Throwable $th) { + $failed++; + } } + + Span::add("interval.domain-verification.processed", $processed); + Span::add("interval.domain-verification.failed", $failed); } private function cleanupStaleExecutions(Database $dbForPlatform, callable $getProjectDB): void @@ -132,9 +144,13 @@ class Interval extends Action $time = DatabaseDateTime::now(); $staleThreshold = DatabaseDateTime::addSeconds(new DateTime(), -1200); // 20 minutes ago + $scanned = 0; + $processed = 0; + $failed = 0; + $dbForPlatform->foreach( 'projects', - function (Document $project) use ($getProjectDB, $time, $staleThreshold) { + function (Document $project) use ($getProjectDB, $time, $staleThreshold, &$scanned, &$processed, &$failed) { try { $dbForProject = $getProjectDB($project); @@ -144,19 +160,21 @@ class Interval extends Action Query::limit(100), ]); + $scanned += \count($staleExecutions); + if (\count($staleExecutions) === 0) { return; } - Console::log("[{$time}] Found " . \count($staleExecutions) . " stale executions in project {$project->getId()}"); - foreach ($staleExecutions as $execution) { $execution->setAttribute('status', 'failed'); $execution->setAttribute('errors', 'Execution timed out'); $dbForProject->updateDocument('executions', $execution->getId(), $execution); } + + $processed++; } catch (\Throwable $th) { - Console::error("[{$time}] Failed to cleanup stale executions for project {$project->getId()}: " . $th->getMessage()); + $failed++; } }, [ @@ -164,5 +182,9 @@ class Interval extends Action Query::limit(100), ] ); + + Span::add("interval.cleanup-stale-executions.scanned", $scanned); + Span::add("interval.cleanup-stale-executions.updated", $processed); + Span::add("interval.cleanup-stale-executions.failed", $failed); } }