From bc4fede216cd0a14ecd654005b0b3afa3c825913 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 2 Aug 2021 12:41:12 +0545 Subject: [PATCH] refactor usage update and fix functions worker --- app/controllers/shared/api.php | 69 +++++++++++----------------------- app/workers/functions.php | 35 +++++++++-------- docker-compose.yml | 2 + 3 files changed, 43 insertions(+), 63 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 901178a94..29333f648 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -218,56 +218,31 @@ App::shutdown(function ($utopia, $request, $response, $project, $events, $audits && $project->getId() && $mode !== APP_MODE_ADMIN //TODO: add check to make sure user is admin && !empty($route->getLabel('sdk.namespace', null))) { // Don't calculate console usage on admin mode + + $storage = $usage->getParam('storage') ?? 0; + + $networkRequestSize = $request->getSize() + $usage->getParam('storage'); + $networkResponseSize = $response->getSize(); - $usage - ->setParam('networkRequestSize', $request->getSize() + $usage->getParam('storage')) - ->setParam('networkResponseSize', $response->getSize()); + $httpMethod = $usage->getParam('httpMethod') ?? ''; + $httpRequest = $usage->getParam('httpRequest') ?? 0; - statsdUpdate($statsd, $usage); + $tags = ",project={$project->getId()},version=".App::getEnv('_APP_VERSION', 'UNKNOWN'); + // the global namespace is prepended to every key (optional) + $statsd->setNamespace('appwrite.usage'); + if($httpRequest >= 1) { + $statsd->increment('requests.all'.$tags.',method='.\strtolower($httpMethod)); + } + + $statsd->count('network.inbound'.$tags, $networkRequestSize); + $statsd->count('network.outbound'.$tags, $networkResponseSize); + $statsd->count('network.all'.$tags, $networkRequestSize + $networkResponseSize); + + if($storage >= 1) { + $statsd->count('storage.all'.$tags, $storage); + } } -}, ['utopia', 'request', 'response', 'project', 'events', 'audits', 'statsd', 'usage', 'deletes', 'database', 'mode'], 'api'); - -function statsdUpdate($statsd, $usage): void -{ - /** @var Appwrite\Event\Event $usage */ - - $projectId = $usage->getParam('projectId') ?? ''; - - $storage = $usage->getParam('storage') ?? 0; - - $networkRequestSize = $usage->getParam('networkRequestSize') ?? 0; - $networkResponseSize = $usage->getParam('networkResponseSize') ?? 0; - - $httpMethod = $usage->getParam('httpMethod') ?? ''; - $httpRequest = $usage->getParam('httpRequest') ?? 0; - - $functionId = $usage->getParam('functionId') ?? ''; - $functionExecution = $usage->getParam('functionExecution') ?? 0; - $functionExecutionTime = $usage->getParam('functionExecutionTime') ?? 0; - $functionStatus = $usage->getParam('functionStatus') ?? ''; - - $tags = ",project={$projectId},version=".App::getEnv('_APP_VERSION', 'UNKNOWN'); - - // the global namespace is prepended to every key (optional) - $statsd->setNamespace('appwrite.usage'); - - if($httpRequest >= 1) { - $statsd->increment('requests.all'.$tags.',method='.\strtolower($httpMethod)); - } - - if($functionExecution >= 1) { - $statsd->increment('executions.all'.$tags.',functionId='.$functionId.',functionStatus='.$functionStatus); - $statsd->count('executions.time'.$tags.',functionId='.$functionId, $functionExecutionTime); - } - - $statsd->count('network.inbound'.$tags, $networkRequestSize); - $statsd->count('network.outbound'.$tags, $networkResponseSize); - $statsd->count('network.all'.$tags, $networkRequestSize + $networkResponseSize); - - if($storage >= 1) { - $statsd->count('storage.all'.$tags, $storage); - } -} \ No newline at end of file +}, ['utopia', 'request', 'response', 'project', 'events', 'audits', 'statsd', 'usage', 'deletes', 'database', 'mode'], 'api'); \ No newline at end of file diff --git a/app/workers/functions.php b/app/workers/functions.php index 510346fb4..6941c6976 100644 --- a/app/workers/functions.php +++ b/app/workers/functions.php @@ -134,8 +134,6 @@ class FunctionsV1 extends Worker public function run(): void { - global $register; - $projectId = $this->args['projectId'] ?? ''; $functionId = $this->args['functionId'] ?? ''; $webhooks = $this->args['webhooks'] ?? []; @@ -279,7 +277,7 @@ class FunctionsV1 extends Worker */ public function execute(string $trigger, string $projectId, string $executionId, Database $database, Document $function, string $event = '', string $eventData = '', string $data = '', array $webhooks = [], string $userId = '', string $jwt = ''): void { - global $list; + global $list, $register; $runtimes = Config::getParam('runtimes'); @@ -477,21 +475,26 @@ class FunctionsV1 extends Worker ->setParam('eventData', $execution->getArrayCopy(array_keys($executionModel->getRules()))); $executionUpdate->trigger(); - - $usage = new Event('v1-usage', 'UsageV1'); - - $usage - ->setParam('projectId', $projectId) - ->setParam('functionId', $function->getId()) - ->setParam('functionExecution', 1) - ->setParam('functionStatus', $functionStatus) - ->setParam('functionExecutionTime', $executionTime * 1000) // ms - ->setParam('networkRequestSize', 0) - ->setParam('networkResponseSize', 0) - ; if(App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { - $usage->trigger(); + $statsd = $register->get('statsd'); + + $storage = 0; + + $functionExecutionTime = $executionTime * 1000; + + $tags = ",project={$projectId},version=".App::getEnv('_APP_VERSION', 'UNKNOWN'); + + // the global namespace is prepended to every key (optional) + $statsd->setNamespace('appwrite.usage'); + + $statsd->increment('executions.all'.$tags.',functionId='.$function->getId().',functionStatus='.$functionStatus); + $statsd->count('executions.time'.$tags.',functionId='.$function->getId(), $functionExecutionTime); + + if($storage >= 1) { + $statsd->count('storage.all'.$tags, $storage); + } + } $this->cleanup(); diff --git a/docker-compose.yml b/docker-compose.yml index 89c88c68d..51ecde502 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -315,6 +315,8 @@ services: - _APP_FUNCTIONS_MEMORY - _APP_FUNCTIONS_MEMORY_SWAP - _APP_USAGE_STATS + - _APP_STATSD_HOST + - _APP_STATSD_PORT - DOCKERHUB_PULL_USERNAME - DOCKERHUB_PULL_PASSWORD