-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e095104
commit bfd3ddc
Showing
9 changed files
with
579 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lamoda\Metric\Adapters\Redis; | ||
|
||
use Lamoda\Metric\Common\MetricSourceInterface; | ||
use Lamoda\Metric\Storage\MetricStorageInterface; | ||
use Lamoda\Metric\Storage\MutableMetricInterface; | ||
|
||
abstract class AbstractRedisStorage implements \IteratorAggregate, MetricStorageInterface | ||
{ | ||
/** @var RedisConnectionInterface */ | ||
private $redisConnection; | ||
|
||
public function __construct(RedisConnectionInterface $redisConnection) | ||
{ | ||
$this->redisConnection = $redisConnection; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
final public function getIterator(): \Traversable | ||
{ | ||
return $this->getMetrics(); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
final public function receive(MetricSourceInterface $source): void | ||
{ | ||
$metrics = []; | ||
foreach ($source->getMetrics() as $metric) { | ||
$metrics[] = new MetricDto( | ||
$metric->getName(), | ||
$metric->resolve(), | ||
$metric->getTags() | ||
); | ||
} | ||
$this->redisConnection->setMetrics($metrics); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
final public function getMetrics(): \Traversable | ||
{ | ||
$metricsData = $this->redisConnection->getAllMetrics(); | ||
foreach ($metricsData as $metricDto) { | ||
yield new MetricWrapper( | ||
$this->redisConnection, | ||
$this->doCreateMetric($metricDto->name, $metricDto->value, $metricDto->tags) | ||
); | ||
} | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
final public function findMetric(string $name, array $tags = []): ?MutableMetricInterface | ||
{ | ||
$value = $this->redisConnection->getMetricValue($name, $tags); | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
return new MetricWrapper( | ||
$this->redisConnection, | ||
$this->doCreateMetric($name, $value, $tags) | ||
); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
final public function createMetric(string $name, float $value, array $tags = []): MutableMetricInterface | ||
{ | ||
$metric = new MetricWrapper( | ||
$this->redisConnection, | ||
$this->doCreateMetric($name, 0, $tags) | ||
); | ||
$metric->setValue($value); | ||
|
||
return $metric; | ||
} | ||
|
||
abstract protected function doCreateMetric(string $name, float $value, array $tags = []): MutableMetricInterface; | ||
|
||
/** {@inheritdoc} */ | ||
final public function setMetricValue(string $name, float $value, array $tags = []): void | ||
{ | ||
$this->redisConnection->setMetrics([new MetricDto($name, $value, $tags)]); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
final public function adjustMetricValue(string $name, float $value, array $tags = []): float | ||
{ | ||
return $this->redisConnection->adjustMetric($name, $value, $tags); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lamoda\Metric\Adapters\Redis; | ||
|
||
/** @internal */ | ||
final class MetricDto | ||
{ | ||
/** @var string */ | ||
public $name; | ||
/** @var float */ | ||
public $value; | ||
/** @var array */ | ||
public $tags; | ||
|
||
public function __construct(string $name, float $value, array $tags) | ||
{ | ||
$this->name = $name; | ||
$this->value = $value; | ||
$this->tags = $tags; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lamoda\Metric\Adapters\Redis; | ||
|
||
use Lamoda\Metric\Storage\MutableMetricInterface; | ||
|
||
/** @internal */ | ||
final class MetricWrapper implements MutableMetricInterface | ||
{ | ||
/** @var MutatorRedisConnectionInterface */ | ||
private $redisConnection; | ||
/** @var MutableMetricInterface */ | ||
private $metric; | ||
|
||
public function __construct(MutatorRedisConnectionInterface $redisConnection, MutableMetricInterface $metric) | ||
{ | ||
$this->redisConnection = $redisConnection; | ||
$this->metric = $metric; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function adjust(float $delta): void | ||
{ | ||
$value = $this->redisConnection->adjustMetric($this->getName(), $delta, $this->getTags()); | ||
$this->metric->setValue($value); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function setValue(float $value): void | ||
{ | ||
$this->redisConnection->setMetrics([new MetricDto($this->getName(), $value, $this->getTags())]); | ||
$this->metric->setValue($value); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function getName(): string | ||
{ | ||
return $this->metric->getName(); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function resolve(): float | ||
{ | ||
return $this->metric->resolve(); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function getTags(): array | ||
{ | ||
return $this->metric->getTags(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lamoda\Metric\Adapters\Redis; | ||
|
||
interface MutatorRedisConnectionInterface | ||
{ | ||
public function adjustMetric(string $key, float $delta, array $tags): float; | ||
|
||
/** | ||
* @param MetricDto[] $metricsData | ||
*/ | ||
public function setMetrics(array $metricsData): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lamoda\Metric\Adapters\Redis; | ||
|
||
/** @internal */ | ||
final class RedisConnection implements RedisConnectionInterface | ||
{ | ||
private const DEFAULT_METRICS_KEY = '__php_metrics'; | ||
|
||
/** @var \Predis\Client|\Redis */ | ||
private $client; | ||
/** @var string */ | ||
private $metricsKey; | ||
|
||
/** | ||
* @param \Predis\Client|\Redis $client | ||
* @param string|null $metricsKey | ||
*/ | ||
public function __construct($client, ?string $metricsKey = self::DEFAULT_METRICS_KEY) | ||
{ | ||
$this->client = $client; | ||
$this->metricsKey = $metricsKey; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function getAllMetrics(): array | ||
{ | ||
$rawMetricsData = $this->client->hgetall($this->metricsKey); | ||
$metrics = []; | ||
foreach ($rawMetricsData as $rawMetricData => $value) { | ||
$metricData = json_decode($rawMetricData, true); | ||
$metrics[] = new MetricDto( | ||
$metricData['name'], | ||
(float) $value, | ||
$this->convertTagsFromStorage($metricData['tags']) | ||
); | ||
} | ||
|
||
return $metrics; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function adjustMetric(string $key, float $delta, array $tags): float | ||
{ | ||
return $this->client->hincrbyfloat($this->metricsKey, $this->buildField($key, $tags), $delta); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function setMetrics(array $metricsData): void | ||
{ | ||
$fields = []; | ||
foreach ($metricsData as $metricDto) { | ||
$field = $this->buildField($metricDto->name, $metricDto->tags); | ||
$fields[$field] = $metricDto->value; | ||
} | ||
$this->client->hmset($this->metricsKey, $fields); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function getMetricValue(string $key, array $tags): ?float | ||
{ | ||
$value = $this->client->hget($this->metricsKey, $this->buildField($key, $tags)); | ||
if ($value === false) { | ||
return null; | ||
} | ||
|
||
return (float) $value; | ||
} | ||
|
||
private function buildField(string $name, array $tags) | ||
{ | ||
return json_encode([ | ||
'name' => $name, | ||
'tags' => $this->convertTagsForStorage($tags), | ||
]); | ||
} | ||
|
||
private function convertTagsForStorage(array $tags): string | ||
{ | ||
return json_encode($this->normalizeTags($tags)); | ||
} | ||
|
||
private function convertTagsFromStorage(string $tags): array | ||
{ | ||
return json_decode($tags, true); | ||
} | ||
|
||
private function normalizeTags(array $tags): array | ||
{ | ||
ksort($tags); | ||
|
||
return $tags; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Lamoda\Metric\Adapters\Redis; | ||
|
||
/** @internal */ | ||
interface RedisConnectionInterface extends MutatorRedisConnectionInterface | ||
{ | ||
/** | ||
* @return MetricDto[] | ||
*/ | ||
public function getAllMetrics(): array; | ||
|
||
public function getMetricValue(string $key, array $tags): ?float; | ||
} |
Oops, something went wrong.