Добавлен Yii2 компонент для отправки событий в statsd

This commit is contained in:
ErickSkrauch 2017-11-16 19:55:00 +03:00
parent c8c6401e14
commit 42b6bc561e
4 changed files with 102 additions and 0 deletions

View File

@ -35,6 +35,12 @@ RABBITMQ_USER=ely-accounts-app
RABBITMQ_PASS=ely-accounts-app-password
RABBITMQ_VHOST=/ely.by
## Параметры Statsd
STATSD_HOST=statsd.ely.by
STATSD_PORT=8125
# This value can be blank
STATSD_NAMESPACE=
## Конфигурация для Dev.
XDEBUG_CONFIG=remote_host=10.254.254.254
PHP_IDE_CONFIG=serverName=docker

View File

@ -24,6 +24,7 @@ class Yii extends \yii\BaseYii {
* @property \common\components\EmailRenderer $emailRenderer
* @property \mito\sentry\Component $sentry
* @property \api\components\OAuth2\Component $oauth
* @property \common\components\StatsD $statsd
*/
abstract class BaseApplication extends yii\base\Application {
}

View File

@ -0,0 +1,89 @@
<?php
namespace common\components;
use Domnikl\Statsd\Client;
use Domnikl\Statsd\Connection;
use yii\base\Component;
class StatsD extends Component {
/**
* @var string
*/
public $host;
/**
* @var int
*/
public $port = 8125;
/**
* @var string
*/
public $namespace = '';
private $client;
public function inc(string $key): void {
$this->getClient()->increment($key);
}
public function dec(string $key): void {
$this->getClient()->decrement($key);
}
public function count(string $key, int $value): void {
$this->getClient()->count($key, $value);
}
public function time(string $key, float $time): void {
$this->getClient()->timing($key, floor($time));
}
public function startTiming(string $key): void {
$this->getClient()->startTiming($key);
}
public function endTiming(string $key): void {
$this->getClient()->endTiming($key);
}
public function peakMemoryUsage(string $key): void {
$this->getClient()->memory($key);
}
/**
* Pass delta values as a string.
* Accepts both positive (+11) and negative (-4) delta values.
* $statsd->gauge('foobar', 3);
* $statsd->gauge('foobar', '+11');
*
* @param string $key
* @param string|int $value
*/
public function gauge(string $key, $value): void {
$this->getClient()->gauge($key, $value);
}
public function set(string $key, int $value): void {
$this->getClient()->set($key, $value);
}
public function getClient(): Client {
if ($this->client === null) {
$connection = $this->createConnection();
$this->client = new Client($connection, $this->namespace);
}
return $this->client;
}
protected function createConnection(): Connection {
if (!empty($this->host) && !empty($this->port)) {
return new Connection\UdpSocket($this->host, $this->port);
}
return new Connection\Blackhole();
}
}

View File

@ -90,6 +90,12 @@ return [
'itemFile' => '@common/rbac/.generated/items.php',
'ruleFile' => '@common/rbac/.generated/rules.php',
],
'statsd' => [
'class' => common\components\StatsD::class,
'host' => getenv('STATSD_HOST'),
'port' => getenv('STATSD_PORT') ?: 8125,
'namespace' => getenv('STATSD_NAMESPACE') ?: 'ely.accounts.' . gethostname() . '.app',
],
],
'container' => [
'definitions' => [