new statsd class and implementation
This commit is contained in:
parent
23caa62550
commit
ffe9c99157
|
@ -18,7 +18,7 @@ App::init(function ($utopia, $request, $response, $project, $user, $register, $e
|
|||
/** @var Utopia\Registry\Registry $register */
|
||||
/** @var Appwrite\Event\Event $events */
|
||||
/** @var Appwrite\Event\Event $audits */
|
||||
/** @var Appwrite\Event\Event $usage */
|
||||
/** @var Appwrite\Statsd\Statsd $usage */
|
||||
/** @var Appwrite\Event\Event $deletes */
|
||||
/** @var Appwrite\Event\Event $database */
|
||||
/** @var Appwrite\Event\Event $functions */
|
||||
|
@ -176,7 +176,7 @@ App::shutdown(function ($utopia, $request, $response, $project, $register, $even
|
|||
/** @var Utopia\Database\Document $project */
|
||||
/** @var Appwrite\Event\Event $events */
|
||||
/** @var Appwrite\Event\Event $audits */
|
||||
/** @var Appwrite\Event\Event $usage */
|
||||
/** @var Appwrite\Statsd\Statsd $usage */
|
||||
/** @var Appwrite\Event\Event $deletes */
|
||||
/** @var Appwrite\Event\Event $database */
|
||||
/** @var Appwrite\Event\Event $functions */
|
||||
|
@ -218,32 +218,12 @@ App::shutdown(function ($utopia, $request, $response, $project, $register, $even
|
|||
&& $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();
|
||||
|
||||
$httpMethod = $usage->getParam('httpMethod') ?? '';
|
||||
$httpRequest = $usage->getParam('httpRequest') ?? 0;
|
||||
|
||||
$tags = ",project={$project->getId()},version=".App::getEnv('_APP_VERSION', 'UNKNOWN');
|
||||
|
||||
$statsd = $register->get('statsd');
|
||||
// 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);
|
||||
}
|
||||
$usage
|
||||
->setParam('networkRequestSize', $request->getSize() + $usage->getParam('storage'))
|
||||
->setParam('networkResponseSize', $response->getSize())
|
||||
->save()
|
||||
;
|
||||
}
|
||||
|
||||
}, ['utopia', 'request', 'response', 'project', 'register', 'events', 'audits', 'usage', 'deletes', 'database', 'mode'], 'api');
|
|
@ -26,6 +26,7 @@ use Appwrite\Database\Adapter\Redis as RedisAdapter;
|
|||
use Appwrite\Database\Document;
|
||||
use Appwrite\Event\Event;
|
||||
use Appwrite\OpenSSL\OpenSSL;
|
||||
use Appwrite\Statsd\Statsd;
|
||||
use Utopia\App;
|
||||
use Utopia\View;
|
||||
use Utopia\Config\Config;
|
||||
|
@ -378,7 +379,7 @@ App::setResource('audits', function($register) {
|
|||
}, ['register']);
|
||||
|
||||
App::setResource('usage', function($register) {
|
||||
return new Event(Event::USAGE_QUEUE_NAME, Event::USAGE_CLASS_NAME);
|
||||
return new Statsd($register->get('statsd'));
|
||||
}, ['register']);
|
||||
|
||||
App::setResource('mails', function($register) {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use Appwrite\Event\Event;
|
||||
use Appwrite\Resque\Worker;
|
||||
use Appwrite\Statsd\Statsd;
|
||||
use Appwrite\Utopia\Response\Model\Execution;
|
||||
use Cron\CronExpression;
|
||||
use Swoole\Runtime;
|
||||
|
@ -479,15 +480,18 @@ class FunctionsV1 extends Worker
|
|||
if(App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
|
||||
$statsd = $register->get('statsd');
|
||||
|
||||
$functionExecutionTime = $executionTime * 1000;
|
||||
$usage = new Statsd($statsd);
|
||||
|
||||
$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);
|
||||
$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)
|
||||
->save()
|
||||
;
|
||||
}
|
||||
|
||||
$this->cleanup();
|
||||
|
|
93
src/Appwrite/Statsd/Statsd.php
Normal file
93
src/Appwrite/Statsd/Statsd.php
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Statsd;
|
||||
|
||||
use Utopia\App;
|
||||
|
||||
class Statsd
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $params = [];
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $statsd;
|
||||
|
||||
/**
|
||||
* Event constructor.
|
||||
*
|
||||
* @param mixed $statsd
|
||||
*/
|
||||
public function __construct($statsd)
|
||||
{
|
||||
$this->statsd = $statsd;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setParam(string $key, $value): self
|
||||
{
|
||||
$this->params[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save to statsd.
|
||||
*/
|
||||
public function save(): void
|
||||
{
|
||||
$projectId = $this->params['projectId'] ?? '';
|
||||
|
||||
$storage = $this->params['storage'] ?? 0;
|
||||
|
||||
$networkRequestSize = $this->params['networkRequestSize'] ?? 0;
|
||||
$networkResponseSize = $this->params['networkResponseSize'] ?? 0;
|
||||
|
||||
$httpMethod = $this->params['httpMethod'] ?? '';
|
||||
$httpRequest = $this->params['httpRequest'] ?? 0;
|
||||
|
||||
$functionId = $this->params['functionId'] ?? '';
|
||||
$functionExecution = $this->params['functionExecution'] ?? 0;
|
||||
$functionExecutionTime = $this->params['functionExecutionTime'] ?? 0;
|
||||
$functionStatus = $this->params['functionStatus'] ?? '';
|
||||
|
||||
$tags = ",project={$projectId},version=" . App::getEnv('_APP_VERSION', 'UNKNOWN');
|
||||
|
||||
// the global namespace is prepended to every key (optional)
|
||||
$this->statsd->setNamespace('appwrite.usage');
|
||||
|
||||
if ($httpRequest >= 1) {
|
||||
$this->statsd->increment('requests.all' . $tags . ',method=' . \strtolower($httpMethod));
|
||||
}
|
||||
|
||||
if ($functionExecution >= 1) {
|
||||
$this->statsd->increment('executions.all' . $tags . ',functionId=' . $functionId . ',functionStatus=' . $functionStatus);
|
||||
$this->statsd->count('executions.time' . $tags . ',functionId=' . $functionId, $functionExecutionTime);
|
||||
}
|
||||
|
||||
$this->statsd->count('network.inbound' . $tags, $networkRequestSize);
|
||||
$this->statsd->count('network.outbound' . $tags, $networkResponseSize);
|
||||
$this->statsd->count('network.all' . $tags, $networkRequestSize + $networkResponseSize);
|
||||
|
||||
if ($storage >= 1) {
|
||||
$this->statsd->count('storage.all' . $tags, $storage);
|
||||
}
|
||||
|
||||
$this->reset();
|
||||
}
|
||||
|
||||
public function reset(): self
|
||||
{
|
||||
$this->params = [];
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue