1
0
Fork 0
mirror of synced 2024-06-18 18:54:55 +12:00
appwrite/src/Appwrite/Usage/Stats.php

226 lines
7.4 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.
2022-10-28 21:40:04 +13:00
* Send various metrics to StatsD based on the parameters that are set
* @return void
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'] ?? '';
$projectInternalId = $this->params['projectInternalId'];
$tags = ",projectInternalId={$projectInternalId},projectId={$projectId},version=" . App::getEnv('_APP_VERSION', 'UNKNOWN');
2022-08-17 23:10:17 +12:00
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);
2022-08-17 23:10:17 +12:00
2022-08-17 22:55:01 +12:00
$httpRequest = $this->params['project.{scope}.network.requests'] ?? 0;
$httpMethod = $this->params['httpMethod'] ?? '';
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
}
2022-08-17 22:55:01 +12:00
2022-10-18 16:53:07 +13:00
$inbound = $this->params['project.{scope}.network.inbound'] ?? 0;
$outbound = $this->params['project.{scope}.network.outbound'] ?? 0;
2022-08-17 22:55:01 +12:00
$this->statsd->count('project.{scope}.network.inbound' . $tags, $inbound);
$this->statsd->count('project.{scope}.network.outbound' . $tags, $outbound);
$this->statsd->count('project.{scope}.network.bandwidth' . $tags, $inbound + $outbound);
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',
2022-10-17 19:59:27 +13:00
'users.{scope}.count.total',
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 || $value === -1) {
2022-10-17 19:59:27 +13:00
$this->statsd->count($metric . $tags, $value);
2022-09-21 21:36:42 +12:00
}
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',
2022-10-17 19:59:27 +13:00
'databases.{scope}.count.total',
'collections.{scope}.count.total',
'documents.{scope}.count.total'
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 || $value === -1) {
2022-08-09 13:22:18 +12:00
$dbTags = $tags . ",collectionId=" . ($this->params['collectionId'] ?? '') . ",databaseId=" . ($this->params['databaseId'] ?? '');
2022-10-17 19:59:27 +13:00
$this->statsd->count($metric . $dbTags, $value);
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',
2022-10-17 19:59:27 +13:00
'buckets.{scope}.count.total',
2022-10-18 19:53:17 +13:00
'files.{scope}.count.total',
'files.{scope}.storage.size'
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 !== 0) {
2022-08-09 13:22:18 +12:00
$storageTags = $tags . ",bucketId=" . ($this->params['bucketId'] ?? '');
2022-10-17 19:59:27 +13:00
$this->statsd->count($metric . $storageTags, $value);
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
}
}
$functionId = $this->params['functionId'] ?? '';
$functionExecution = $this->params['executions.{scope}.compute'] ?? 0;
$functionExecutionTime = ($this->params['executionTime'] ?? 0) * 1000; // ms
$functionExecutionStatus = $this->params['executionStatus'] ?? '';
$functionBuild = $this->params['builds.{scope}.compute'] ?? 0;
$functionBuildTime = ($this->params['buildTime'] ?? 0) * 1000; // ms
$functionBuildStatus = $this->params['buildStatus'] ?? '';
$functionCompute = $functionExecutionTime + $functionBuildTime;
2022-10-18 19:53:17 +13:00
$functionTags = $tags . ',functionId=' . $functionId;
2022-10-18 20:26:28 +13:00
2022-10-18 19:53:17 +13:00
$deploymentSize = $this->params['deployment.{scope}.storage.size'] ?? 0;
$storageSize = $this->params['files.{scope}.storage.size'] ?? 0;
2022-10-18 21:28:12 +13:00
if ($deploymentSize + $storageSize > 0 || $deploymentSize + $storageSize <= -1) {
2022-10-18 19:53:17 +13:00
$this->statsd->count('project.{scope}.storage.size' . $tags, $deploymentSize + $storageSize);
}
if ($deploymentSize !== 0) {
2022-10-18 19:53:17 +13:00
$this->statsd->count('deployments.{scope}.storage.size' . $functionTags, $deploymentSize);
}
2022-08-09 13:22:18 +12:00
if ($functionExecution >= 1) {
2022-10-18 19:53:17 +13:00
$this->statsd->increment('executions.{scope}.compute' . $functionTags . ',functionStatus=' . $functionExecutionStatus);
2022-08-11 18:18:37 +12:00
if ($functionExecutionTime > 0) {
2022-10-18 19:53:17 +13:00
$this->statsd->count('executions.{scope}.compute.time' . $functionTags, $functionExecutionTime);
}
2022-08-09 13:22:18 +12:00
}
if ($functionBuild >= 1) {
2022-10-18 19:53:17 +13:00
$this->statsd->increment('builds.{scope}.compute' . $functionTags . ',functionBuildStatus=' . $functionBuildStatus);
$this->statsd->count('builds.{scope}.compute.time' . $functionTags, $functionBuildTime);
2022-08-09 13:22:18 +12:00
}
if ($functionBuild + $functionExecution >= 1) {
2022-10-18 19:53:17 +13:00
$this->statsd->count('project.{scope}.compute.time' . $functionTags, $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;
}
}