chore: trace database worker with Span::add instead of Console logging

Replace unstructured Console::info/error calls in the database worker
with structured Span::add tags. Operation identifiers (project, database,
collection, document) and queue metadata are recorded up front; swallowed
errors (deleteByGroup, attribute NotFound) are surfaced as spans so they
stay visible. Redundant Console::error calls before rethrows are dropped
since the entry-point harness owns span error lifecycle.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
loks0nandClaude Opus 4.8 committed 2026-06-01 13:14:39 +01:00
1 parent 57ef57a681
commit 374f858db2
1 file changed
+14 -22
@@ -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
{
@@ -73,12 +73,22 @@ class Databases extends Action
$log->addTag('projectId', $project->getId());
$log->addTag('type', $type);
Span::add('project.id', $project->getId());
Span::add('payload.type', $type);
Span::add('queue.pid', $message->getPid());
Span::add('queue.name', $message->getQueue());
Span::add('message.timestamp', (string) $message->getTimestamp());
Span::add('database.collection.id', $collection->getId());
Span::add('database.document.id', $document->getId());
if ($database->isEmpty()) {
throw new Exception('Missing database');
}
$log->addTag('databaseId', $database->getId());
Span::add('database.id', $database->getId());
match (\strval($type)) {
DATABASE_TYPE_DELETE_DATABASE => $this->deleteDatabase($database, $dbForProject, $dbForDatabases),
DATABASE_TYPE_DELETE_COLLECTION => $this->deleteCollection($database, $collection, $dbForProject, $dbForDatabases),
@@ -88,14 +98,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 +208,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 +309,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 +318,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 +454,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 +509,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,8 +608,6 @@ class Databases extends Action
*/
protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void
{
$start = \microtime(true);
try {
$count = $database->deleteDocuments(
$collectionId,
@@ -624,12 +617,11 @@ class Databases extends Action
);
} 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', "Failed to delete documents/rows for collection/table: {$database->getNamespace()}_{$collectionId} {$tenant} :{$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);
}
/**