Skip to content

Commit

Permalink
Stat Can Be Enum (#13)
Browse files Browse the repository at this point in the history
* initial commit

* LeagueStatsDClientAdapter converts stats

* DatadogStatsDClientAdapter converts stats

* InMemoryClientAdapter converts stats

* lines should not exceed 80 chars

* remove one-line method and replace with static call

* rename return value

* add some tests

* test for string

* tests for enums

* use common enums

* clean up

* DogStatsDSpy

* confirm that DataDogStatsDClientAdapter converts enums and array of enums
  • Loading branch information
cosmastech authored Jul 21, 2024
1 parent 6c4d841 commit 71f8b89
Show file tree
Hide file tree
Showing 24 changed files with 777 additions and 146 deletions.
32 changes: 32 additions & 0 deletions src/Adapters/Concerns/ConvertsStatTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Cosmastech\StatsDClientAdapter\Adapters\Concerns;

use Cosmastech\StatsDClientAdapter\Utility\EnumConverter;

trait ConvertsStatTrait
{
/**
* @param mixed $value
* @return string|array<int, string>
* @phpstan-return ($value is array ? array<int, string> : string)
*/
protected function convertStat(mixed $value): string|array
{
if (is_array($value)) {
$convertedStats = [];
foreach($value as $element) {
$convertedStats[] = $this->convertValueToString($element);
}

return $convertedStats;
}

return $this->convertValueToString($value);
}

protected function convertValueToString(mixed $value): string
{
return (string) EnumConverter::convertIfEnum($value);
}
}
10 changes: 8 additions & 2 deletions src/Adapters/Concerns/TimeClosureTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

namespace Cosmastech\StatsDClientAdapter\Adapters\Concerns;

use UnitEnum;

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"));

$result = $closure();
Expand Down
83 changes: 59 additions & 24 deletions src/Adapters/Datadog/DatadogStatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cosmastech\StatsDClientAdapter\Adapters\Datadog;

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 @@ -12,9 +13,11 @@
use Cosmastech\StatsDClientAdapter\Utility\Clock;
use DataDog\DogStatsd;
use Psr\Clock\ClockInterface;
use UnitEnum;

class DatadogStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAware
{
use ConvertsStatTrait;
use HasDefaultTagsTrait;
use TagNormalizerAwareTrait;
use TimeClosureTrait;
Expand Down Expand Up @@ -44,10 +47,14 @@ 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,
$this->convertStat($stat),
$durationMs,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags))
Expand All @@ -57,10 +64,14 @@ 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,
$this->convertStat($stat),
$value,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags))
Expand All @@ -70,10 +81,14 @@ 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,
$this->convertStat($stat),
$value,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags))
Expand All @@ -83,10 +98,14 @@ 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,
$this->convertStat($stat),
$value,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags))
Expand All @@ -96,10 +115,14 @@ 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,
$this->convertStat($stat),
$value,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags))
Expand All @@ -109,10 +132,14 @@ 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,
$this->convertStat($stats),
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
$value
Expand All @@ -122,10 +149,14 @@ 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,
$this->convertStat($stats),
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags)),
$value
Expand All @@ -135,10 +166,14 @@ 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,
$this->convertStat($stats),
$delta,
$sampleRate,
$this->normalizeTags($this->mergeWithDefaultTags($tags))
Expand Down
Loading

0 comments on commit 71f8b89

Please sign in to comment.