Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Jul 20, 2024
1 parent 6c4d841 commit 8c8d7b5
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 50 deletions.
13 changes: 13 additions & 0 deletions src/Adapters/Concerns/ConvertsStatTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Cosmastech\StatsDClientAdapter\Adapters\Concerns;

use Cosmastech\StatsDClientAdapter\Utility\EnumConverter;

trait ConvertsStatTrait
{
protected function convertStat(mixed $value): mixed
{
return EnumConverter::convertIfEnum($value);
}
}
2 changes: 1 addition & 1 deletion src/Adapters/Concerns/TimeClosureTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait TimeClosureTrait
/**
* @inheritDoc
*/
public function time(callable $closure, string $stat, float $sampleRate = 1.0, array $tags = [])
public function time(callable $closure, string|\UnitEnum $stat, float $sampleRate = 1.0, array $tags = [])
{
$startTime = intval($this->clock->now()->format("Uv"));

Expand Down
16 changes: 8 additions & 8 deletions src/Adapters/Datadog/DatadogStatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
/**
* @inheritDoc
*/
public function timing(string $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->datadogClient->timing(
$stat,
Expand All @@ -57,7 +57,7 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
/**
* @inheritDoc
*/
public function gauge(string $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->datadogClient->gauge(
$stat,
Expand All @@ -70,7 +70,7 @@ public function gauge(string $stat, float $value, float $sampleRate = 1.0, array
/**
* @inheritDoc
*/
public function histogram(string $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->datadogClient->histogram(
$stat,
Expand All @@ -83,7 +83,7 @@ public function histogram(string $stat, float $value, float $sampleRate = 1.0, a
/**
* @inheritDoc
*/
public function distribution(string $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->datadogClient->distribution(
$stat,
Expand All @@ -96,7 +96,7 @@ public function distribution(string $stat, float $value, float $sampleRate = 1.0
/**
* @inheritDoc
*/
public function set(string $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->datadogClient->set(
$stat,
Expand All @@ -109,7 +109,7 @@ public function set(string $stat, float|string $value, float $sampleRate = 1.0,
/**
* @inheritDoc
*/
public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
public function increment(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
{
$this->datadogClient->increment(
$stats,
Expand All @@ -122,7 +122,7 @@ public function increment(array|string $stats, float $sampleRate = 1.0, array $t
/**
* @inheritDoc
*/
public function decrement(array|string $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
{
$this->datadogClient->decrement(
$stats,
Expand All @@ -135,7 +135,7 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
/**
* @inheritDoc
*/
public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, array $tags = null): void
public function updateStats(array|string|\UnitEnum $stats, int $delta = 1, $sampleRate = 1.0, array $tags = null): void
{
$this->datadogClient->updateStats(
$stats,
Expand Down
16 changes: 8 additions & 8 deletions src/Adapters/InMemory/InMemoryClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function flush(): void
/**
* @inheritDoc
*/
public function timing(string $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(
Expand All @@ -74,7 +74,7 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
/**
* @inheritDoc
*/
public function gauge(string $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(
Expand All @@ -90,7 +90,7 @@ public function gauge(string $stat, float $value, float $sampleRate = 1.0, array
/**
* @inheritDoc
*/
public function histogram(string $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(
Expand All @@ -106,7 +106,7 @@ public function histogram(string $stat, float $value, float $sampleRate = 1.0, a
/**
* @inheritDoc
*/
public function distribution(string $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(
Expand All @@ -122,7 +122,7 @@ public function distribution(string $stat, float $value, float $sampleRate = 1.0
/**
* @inheritDoc
*/
public function set(string $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(
Expand All @@ -138,15 +138,15 @@ public function set(string $stat, float|string $value, float $sampleRate = 1.0,
/**
* @inheritDoc
*/
public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
public function increment(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
{
$this->updateStats($stats, $value, $sampleRate, $tags);
}

/**
* @inheritDoc
*/
public function decrement(array|string $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;
Expand All @@ -158,7 +158,7 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
/**
* @inheritDoc
*/
public function updateStats(array|string $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();
Expand Down
34 changes: 19 additions & 15 deletions src/Adapters/League/LeagueStatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cosmastech\StatsDClientAdapter\Adapters\League;

use Closure;
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 @@ -11,16 +12,19 @@
use Cosmastech\StatsDClientAdapter\TagNormalizers\NoopTagNormalizer;
use Cosmastech\StatsDClientAdapter\TagNormalizers\TagNormalizer;
use Cosmastech\StatsDClientAdapter\Utility\Clock;
use Cosmastech\StatsDClientAdapter\Utility\EnumConverter;
use Cosmastech\StatsDClientAdapter\Utility\SampleRateDecider\Contracts\SampleRateSendDecider as SampleRateSendDeciderInterface;
use Cosmastech\StatsDClientAdapter\Utility\SampleRateDecider\SampleRateSendDecider;
use League\StatsD\Client;
use League\StatsD\Exception\ConfigurationException;
use League\StatsD\Exception\ConnectionException;
use League\StatsD\StatsDClient as LeagueStatsDClientInterface;
use Psr\Clock\ClockInterface;
use UnitEnum;

class LeagueStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAware
{
use ConvertsStatTrait;
use HasDefaultTagsTrait;
use TagNormalizerAwareTrait;
use TimeClosureTrait;
Expand All @@ -32,7 +36,7 @@ class LeagueStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAwa
protected readonly ClockInterface $clock;

/**
* @var Closure(string, float, float, array<mixed, mixed>):void
* @var Closure(string|UnitEnum, float, float, array<mixed, mixed>):void
*/
protected Closure $unavailableStatHandler;

Expand Down Expand Up @@ -126,22 +130,22 @@ protected function getUnavailableStatHandler(): Closure


/**
* @param string $stat
* @param string|UnitEnum $stat
* @param float $durationMs
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*
* @throws ConnectionException
*/
public function timing(string $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
{
if (! $this->sampleRateSendDecider->decide($sampleRate)) {
return;
}

$this->leagueStatsDClient->timing(
$stat,
$this->convertStat($stat),
$durationMs,
$this->normalizeTags($this->mergeWithDefaultTags($tags))
);
Expand All @@ -150,40 +154,40 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
/**
* @throws ConnectionException
*/
public function gauge(string $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
{
if (! $this->sampleRateSendDecider->decide($sampleRate)) {
return;
}

$this->leagueStatsDClient->gauge(
$stat,
$this->convertStat($stat),
$value,
$this->normalizeTags($this->mergeWithDefaultTags($tags))
);
}

public function histogram(string $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->handleUnavailableStat($stat, $value, $sampleRate, $tags);
$this->handleUnavailableStat($this->convertStat($stat), $value, $sampleRate, $tags);
}

public function distribution(string $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->handleUnavailableStat($stat, $value, $sampleRate, $tags);
$this->handleUnavailableStat($this->convertStat($stat), $value, $sampleRate, $tags);
}

/**
* @throws ConnectionException
*/
public function set(string $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
{
if (! $this->sampleRateSendDecider->decide($sampleRate)) {
return;
}

$this->leagueStatsDClient->set(
$stat,
$this->convertStat($stat),
$value,
$this->normalizeTags($this->mergeWithDefaultTags($tags))
);
Expand All @@ -192,7 +196,7 @@ public function set(string $stat, float|string $value, float $sampleRate = 1.0,
/**
* @throws ConnectionException
*/
public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
public function increment(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
{
$this->leagueStatsDClient->increment(
$stats,
Expand All @@ -205,7 +209,7 @@ public function increment(array|string $stats, float $sampleRate = 1.0, array $t
/**
* @throws ConnectionException
*/
public function decrement(array|string $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
{
$this->leagueStatsDClient->decrement(
$stats,
Expand All @@ -218,7 +222,7 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
/**
* @throws ConnectionException
*/
public function updateStats(array|string $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
{
$this->increment(
$stats,
Expand Down
Loading

0 comments on commit 8c8d7b5

Please sign in to comment.