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

131 lines
3.1 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'] ?? '';
2021-08-13 22:00:25 +12:00
$httpPath = $this->params['httpPath'] ?? '';
2021-08-08 17:36:08 +12:00
$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)
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 20:30:51 +12:00
$this->statsd->increment('requests.all' . $tags . ',method=' . \strtolower($httpMethod) . ',path=' . str_replace(':', '*', $httpPath));
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);
if ($storage >= 1) {
$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;
}
}