1
0
Fork 0
mirror of synced 2024-05-19 04:02:34 +12:00
appwrite/src/Appwrite/Stats/Stats.php

201 lines
5.4 KiB
PHP
Raw Normal View History

2021-08-08 17:36:08 +12:00
<?php
2021-08-08 18:31:20 +12:00
namespace Appwrite\Stats;
2021-08-08 17:36:08 +12:00
use Utopia\App;
2021-08-08 18:31:20 +12:00
class Stats
2021-08-08 17:36:08 +12:00
{
/**
* @var array
*/
protected $params = [];
/**
* @var mixed
*/
protected $statsd;
2021-08-08 21:23:44 +12:00
/**
* @var string
*/
protected $namespace = 'appwrite.usage';
2021-08-08 17:36:08 +12:00
/**
* 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;
}
2021-08-08 18:31:20 +12:00
/**
* @param string $key
*
* @return mixed|null
*/
public function getParam(string $key)
{
return (isset($this->params[$key])) ? $this->params[$key] : null;
}
2021-08-08 21:23:44 +12:00
/**
* @param string $namespace
*
* @return $this
*/
public function setNamespace(string $namespace): self
{
$this->namespace = $namespace;
return $this;
}
/**
* @return string
*/
public function getNamespace()
{
return $this->namespace;
}
2021-08-08 17:36:08 +12:00
/**
* Submit data to StatsD.
2021-08-08 17:36:08 +12:00
*/
2021-08-08 18:31:20 +12:00
public function submit(): void
2021-08-08 17:36:08 +12:00
{
$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'] ?? '';
2021-08-16 18:58:34 +12:00
$tags = ",projectId={$projectId},version=" . App::getEnv('_APP_VERSION', 'UNKNOWN');
2021-08-08 17:36:08 +12:00
// the global namespace is prepended to every key (optional)
2021-08-08 21:23:44 +12:00
$this->statsd->setNamespace($this->namespace);
2021-08-08 17:36:08 +12:00
if ($httpRequest >= 1) {
2021-08-15 23:39:49 +12:00
$this->statsd->increment('requests.all' . $tags . ',method=' . \strtolower($httpMethod));
2021-08-08 17:36:08 +12:00
}
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);
2021-08-15 23:39:49 +12:00
$dbMetrics = [
Database layer (#3338) * database response model * database collection config * new database scopes * database service update * database execption codes * remove read write permission from database model * updating tests and fixing some bugs * server side tests are now passing * databases api * tests for database endpoint * composer update * fix error * formatting * formatting fixes * get database test * more updates to events and usage * more usage updates * fix delete type * fix test * delete database * more fixes * databaseId in attributes and indexes * more fixes * fix issues * fix index subquery * fix console scope and index query * updating tests as required * fix phpcs errors and warnings * updates to review suggestions * UI progress * ui updates and cleaning up * fix type * rework database events * update tests * update types * event generation fixed * events config updated * updating context to support multiple * realtime updates * fix ids * update context * validator updates * fix naming conflict * fix tests * fix lint errors * fix wprler and realtime tests * fix webhooks test * fix event validator and other tests * formatting fixes * removing leftover var_dumps * remove leftover comment * update usage params * usage metrics updates * update database usage * fix usage * specs update * updates to usage * fix UI and usage * fix lints * internal id fixes * fixes for internal Id * renaming services and related files * rename tests * rename doc link * rename readme * fix test name * tests: fixes for 0.15.x sync Co-authored-by: Torsten Dittmann <torsten.dittmann@googlemail.com>
2022-06-22 22:51:49 +12:00
'databases.create',
'databases.read',
'databases.update',
'databases.delete',
'databases.collections.create',
'databases.collections.read',
'databases.collections.update',
'databases.collections.delete',
'databases.documents.create',
'databases.documents.read',
'databases.documents.update',
'databases.documents.delete',
2021-08-15 23:39:49 +12:00
];
foreach ($dbMetrics as $metric) {
$value = $this->params[$metric] ?? 0;
if ($value >= 1) {
Database layer (#3338) * database response model * database collection config * new database scopes * database service update * database execption codes * remove read write permission from database model * updating tests and fixing some bugs * server side tests are now passing * databases api * tests for database endpoint * composer update * fix error * formatting * formatting fixes * get database test * more updates to events and usage * more usage updates * fix delete type * fix test * delete database * more fixes * databaseId in attributes and indexes * more fixes * fix issues * fix index subquery * fix console scope and index query * updating tests as required * fix phpcs errors and warnings * updates to review suggestions * UI progress * ui updates and cleaning up * fix type * rework database events * update tests * update types * event generation fixed * events config updated * updating context to support multiple * realtime updates * fix ids * update context * validator updates * fix naming conflict * fix tests * fix lint errors * fix wprler and realtime tests * fix webhooks test * fix event validator and other tests * formatting fixes * removing leftover var_dumps * remove leftover comment * update usage params * usage metrics updates * update database usage * fix usage * specs update * updates to usage * fix UI and usage * fix lints * internal id fixes * fixes for internal Id * renaming services and related files * rename tests * rename doc link * rename readme * fix test name * tests: fixes for 0.15.x sync Co-authored-by: Torsten Dittmann <torsten.dittmann@googlemail.com>
2022-06-22 22:51:49 +12:00
$tags = ",projectId={$projectId},collectionId=" . ($this->params['collectionId'] ?? '') . ",databaseId=" . ($this->params['databaseId'] ?? '');
2021-08-16 20:53:34 +12:00
$this->statsd->increment($metric . $tags);
2021-08-15 23:39:49 +12:00
}
}
2021-08-16 19:25:20 +12:00
$storageMertics = [
2021-09-10 20:49:16 +12:00
'storage.buckets.create',
'storage.buckets.read',
'storage.buckets.update',
'storage.buckets.delete',
2021-08-16 19:25:20 +12:00
'storage.files.create',
'storage.files.read',
'storage.files.update',
'storage.files.delete',
];
foreach ($storageMertics as $metric) {
$value = $this->params[$metric] ?? 0;
if ($value >= 1) {
2021-08-16 20:53:34 +12:00
$tags = ",projectId={$projectId},bucketId=" . ($this->params['bucketId'] ?? '');
$this->statsd->increment($metric . $tags);
}
}
$usersMetrics = [
'users.create',
'users.read',
'users.update',
'users.delete',
];
foreach ($usersMetrics as $metric) {
$value = $this->params[$metric] ?? 0;
if ($value >= 1) {
$tags = ",projectId={$projectId}";
$this->statsd->increment($metric . $tags);
}
}
$sessionsMetrics = [
'users.sessions.create',
'users.sessions.delete',
];
foreach ($sessionsMetrics as $metric) {
$value = $this->params[$metric] ?? 0;
if ($value >= 1) {
2022-05-24 02:54:50 +12:00
$tags = ",projectId={$projectId},provider=" . ($this->params['provider'] ?? '');
2021-08-17 17:23:43 +12:00
$this->statsd->count($metric . $tags, $value);
2021-08-16 19:25:20 +12:00
}
}
2021-08-08 17:36:08 +12:00
if ($storage >= 1) {
2021-08-25 17:59:12 +12:00
$tags = ",projectId={$projectId},bucketId=" . ($this->params['bucketId'] ?? '');
2021-08-08 17:36:08 +12:00
$this->statsd->count('storage.all' . $tags, $storage);
}
$this->reset();
}
public function reset(): self
{
$this->params = [];
2021-08-08 21:23:44 +12:00
$this->namespace = 'appwrite.usage';
2021-08-08 17:36:08 +12:00
return $this;
}
}