Skip to content

Commit

Permalink
InMemoryClientAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Jul 20, 2024
1 parent 1707b5a commit c5582ed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
57 changes: 39 additions & 18 deletions src/Adapters/InMemory/InMemoryClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\StatsDClientAdapter\Adapters\InMemory;

use Cosmastech\StatsDClientAdapter\Adapters\Concerns\ConvertsStatTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\HasDefaultTagsTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TagNormalizerAwareTrait;
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TimeClosureTrait;
Expand All @@ -18,9 +19,11 @@
use Cosmastech\StatsDClientAdapter\TagNormalizers\TagNormalizer;
use Cosmastech\StatsDClientAdapter\Utility\Clock;
use Psr\Clock\ClockInterface;
use UnitEnum;

class InMemoryClientAdapter implements StatsDClientAdapter, TagNormalizerAware
{
use ConvertsStatTrait;
use HasDefaultTagsTrait;
use TagNormalizerAwareTrait;
use TimeClosureTrait;
Expand Down Expand Up @@ -58,11 +61,11 @@ public function flush(): void
/**
* @inheritDoc
*/
public function timing(string|\UnitEnum $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
public function timing(string|UnitEnum $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
{
$this->stats->recordTiming(
new InMemoryTimingRecord(
$stat,
$this->convertStat($stat),
$durationMs,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
Expand All @@ -74,11 +77,11 @@ public function timing(string|\UnitEnum $stat, float $durationMs, float $sampleR
/**
* @inheritDoc
*/
public function gauge(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
public function gauge(string|UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
{
$this->stats->recordGauge(
new InMemoryGaugeRecord(
$stat,
$this->convertStat($stat),
$value,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
Expand All @@ -90,11 +93,11 @@ public function gauge(string|\UnitEnum $stat, float $value, float $sampleRate =
/**
* @inheritDoc
*/
public function histogram(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
public function histogram(string|UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
{
$this->stats->recordHistogram(
new InMemoryHistogramRecord(
$stat,
$this->convertStat($stat),
$value,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
Expand All @@ -106,11 +109,11 @@ public function histogram(string|\UnitEnum $stat, float $value, float $sampleRat
/**
* @inheritDoc
*/
public function distribution(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
public function distribution(string|UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
{
$this->stats->recordDistribution(
new InMemoryDistributionRecord(
$stat,
$this->convertStat($stat),
$value,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
Expand All @@ -122,11 +125,11 @@ public function distribution(string|\UnitEnum $stat, float $value, float $sample
/**
* @inheritDoc
*/
public function set(string|\UnitEnum $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
public function set(string|UnitEnum $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
{
$this->stats->recordSet(
new InMemorySetRecord(
$stat,
$this->convertStat($stat),
$value,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
Expand All @@ -138,35 +141,53 @@ public function set(string|\UnitEnum $stat, float|string $value, float $sampleRa
/**
* @inheritDoc
*/
public function increment(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
{
$this->updateStats($stats, $value, $sampleRate, $tags);
public function increment(
array|string|UnitEnum $stats,
float $sampleRate = 1.0,
array $tags = [],
int $value = 1
): void {
$this->updateStats(
$this->convertStat($stats),
$value,
$sampleRate,
$tags
);
}

/**
* @inheritDoc
*/
public function decrement(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = -1): void
{
public function decrement(
array|string|UnitEnum $stats,
float $sampleRate = 1.0,
array $tags = [],
int $value = -1
): void {
if ($value > 0) {
$value *= -1;
}

$this->updateStats($stats, $value, $sampleRate, $tags);
$this->updateStats(
$this->convertStat($stats),
$value,
$sampleRate,
$tags
);
}

/**
* @inheritDoc
*/
public function updateStats(array|string|\UnitEnum $stats, int $delta = 1, float $sampleRate = 1.0, array $tags = []): void
public function updateStats(array|string|UnitEnum $stats, int $delta = 1, float $sampleRate = 1.0, array $tags = []): void
{
$stats = (array) $stats;
$now = $this->clock->now();

foreach ($stats as $stat) {
$this->stats->recordCount(
new InMemoryCountRecord(
$stat,
$this->convertStat($stat),
$delta,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
Expand Down
4 changes: 2 additions & 2 deletions src/Adapters/League/LeagueStatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ public function setUnavailableStatHandler(Closure $closure): self
}

/**
* @param string $stat
* @param string|UnitEnum $stat
* @param float $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*/
protected function handleUnavailableStat(
string $stat,
string|UnitEnum $stat,
float $value,
float $sampleRate = 1.0,
array $tags = []
Expand Down

0 comments on commit c5582ed

Please sign in to comment.