1
0
Fork 0
mirror of synced 2024-06-27 02:31:04 +12:00
appwrite/src/Appwrite/Usage/Stats.php

211 lines
6.6 KiB
PHP
Raw Normal View History

2021-08-08 17:36:08 +12:00
<?php
2022-08-09 13:22:18 +12:00
namespace Appwrite\Usage;
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'] ?? '';
$functionBuildTime = $this->params['functionBuildTime'] ?? 0;
$functionBuild = $this->params['functionBuild'] ?? 0;
$functionBuildStatus = $this->params['functionBuildStatus'] ?? '';
$functionCompute = $functionExecutionTime + $functionBuildTime;
2021-08-08 17:36:08 +12:00
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) {
$this->statsd->increment('project.{scope}.network.requests' . $tags . ',method=' . \strtolower($httpMethod));
2021-08-08 17:36:08 +12:00
}
$this->statsd->count('project.{scope}.network.inbound' . $tags, $networkRequestSize);
$this->statsd->count('project.{scope}.network.outbound' . $tags, $networkResponseSize);
$this->statsd->count('project.{scope}.network.bandwidth' . $tags, $networkRequestSize + $networkResponseSize);
2021-08-08 17:36:08 +12:00
2022-08-09 13:22:18 +12:00
$usersMetrics = [
'users.{scope}.requests.create',
'users.{scope}.requests.read',
'users.{scope}.requests.update',
'users.{scope}.requests.delete',
2021-08-15 23:39:49 +12:00
];
2022-08-09 13:22:18 +12:00
foreach ($usersMetrics as $metric) {
2021-08-15 23:39:49 +12:00
$value = $this->params[$metric] ?? 0;
if ($value >= 1) {
2021-08-16 20:53:34 +12:00
$this->statsd->increment($metric . $tags);
2021-08-15 23:39:49 +12:00
}
}
2022-08-09 13:22:18 +12:00
$dbMetrics = [
'databases.{scope}.requests.create',
'databases.{scope}.requests.read',
'databases.{scope}.requests.update',
'databases.{scope}.requests.delete',
'collections.{scope}.requests.create',
'collections.{scope}.requests.read',
'collections.{scope}.requests.update',
'collections.{scope}.requests.delete',
'documents.{scope}.requests.create',
'documents.{scope}.requests.read',
'documents.{scope}.requests.update',
'documents.{scope}.requests.delete',
2021-08-16 19:25:20 +12:00
];
2022-08-09 13:22:18 +12:00
foreach ($dbMetrics as $metric) {
2021-08-16 19:25:20 +12:00
$value = $this->params[$metric] ?? 0;
if ($value >= 1) {
2022-08-09 13:22:18 +12:00
$dbTags = $tags . ",collectionId=" . ($this->params['collectionId'] ?? '') . ",databaseId=" . ($this->params['databaseId'] ?? '');
$this->statsd->increment($metric . $dbTags);
2021-08-16 20:53:34 +12:00
}
}
2022-08-09 13:22:18 +12:00
$storageMertics = [
'buckets.{scope}.requests.create',
'buckets.{scope}.requests.read',
'buckets.{scope}.requests.update',
'buckets.{scope}.requests.delete',
'files.{scope}.requests.create',
'files.{scope}.requests.read',
'files.{scope}.requests.update',
'files.{scope}.requests.delete',
2021-08-16 20:53:34 +12:00
];
2022-08-09 13:22:18 +12:00
foreach ($storageMertics as $metric) {
2021-08-16 20:53:34 +12:00
$value = $this->params[$metric] ?? 0;
if ($value >= 1) {
2022-08-09 13:22:18 +12:00
$storageTags = $tags . ",bucketId=" . ($this->params['bucketId'] ?? '');
$this->statsd->increment($metric . $storageTags);
2021-08-16 20:53:34 +12:00
}
}
$sessionsMetrics = [
'sessions.{scope}.requests.create',
2022-08-10 20:45:10 +12:00
'sessions.{scope}.requests.update',
'sessions.{scope}.requests.delete',
2021-08-16 20:53:34 +12:00
];
foreach ($sessionsMetrics as $metric) {
$value = $this->params[$metric] ?? 0;
if ($value >= 1) {
2022-08-09 13:22:18 +12:00
$sessionTags = $tags . ",provider=" . ($this->params['provider'] ?? '');
$this->statsd->count($metric . $sessionTags, $value);
2021-08-16 19:25:20 +12:00
}
}
2021-08-08 17:36:08 +12:00
if ($storage >= 1) {
2022-08-09 13:22:18 +12:00
$storageTags = $tags . ",bucketId=" . ($this->params['bucketId'] ?? '');
$this->statsd->count('storage.all' . $storageTags, $storage);
}
if ($functionExecution >= 1) {
$this->statsd->increment('executions.{scope}.compute' . $tags . ',functionId=' . $functionId . ',functionStatus=' . $functionStatus);
$this->statsd->count('executions.{scope}.compute.time' . $tags . ',functionId=' . $functionId, $functionExecutionTime);
2022-08-09 13:22:18 +12:00
}
if ($functionBuild >= 1) {
$this->statsd->increment('builds.{scope}.compute' . $tags . ',functionId=' . $functionId . ',functionBuildStatus=' . $functionBuildStatus);
$this->statsd->count('builds.{scope}.compute.time' . $tags . ',functionId=' . $functionId, $functionExecutionTime);
2022-08-09 13:22:18 +12:00
}
if ($functionBuild + $functionExecution >= 1) {
$this->statsd->count('project.{scope}.compute.time' . $tags . ',functionId=' . $functionId, $functionCompute);
2021-08-08 17:36:08 +12:00
}
$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;
}
}