1
0
Fork 0
mirror of synced 2024-05-20 12:42:39 +12:00

refactor usage update and fix functions worker

This commit is contained in:
Damodar Lohani 2021-08-02 12:41:12 +05:45
parent 7d38b83abf
commit bc4fede216
3 changed files with 43 additions and 63 deletions

View file

@ -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);
}
}
}, ['utopia', 'request', 'response', 'project', 'events', 'audits', 'statsd', 'usage', 'deletes', 'database', 'mode'], 'api');

View file

@ -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();

View file

@ -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