Skip to content

Commit 8c8d7b5

Browse files
committed
initial commit
1 parent 6c4d841 commit 8c8d7b5

File tree

7 files changed

+92
-50
lines changed

7 files changed

+92
-50
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Cosmastech\StatsDClientAdapter\Adapters\Concerns;
4+
5+
use Cosmastech\StatsDClientAdapter\Utility\EnumConverter;
6+
7+
trait ConvertsStatTrait
8+
{
9+
protected function convertStat(mixed $value): mixed
10+
{
11+
return EnumConverter::convertIfEnum($value);
12+
}
13+
}

src/Adapters/Concerns/TimeClosureTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait TimeClosureTrait
77
/**
88
* @inheritDoc
99
*/
10-
public function time(callable $closure, string $stat, float $sampleRate = 1.0, array $tags = [])
10+
public function time(callable $closure, string|\UnitEnum $stat, float $sampleRate = 1.0, array $tags = [])
1111
{
1212
$startTime = intval($this->clock->now()->format("Uv"));
1313

src/Adapters/Datadog/DatadogStatsDClientAdapter.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(
4444
/**
4545
* @inheritDoc
4646
*/
47-
public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
47+
public function timing(string|\UnitEnum $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
4848
{
4949
$this->datadogClient->timing(
5050
$stat,
@@ -57,7 +57,7 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
5757
/**
5858
* @inheritDoc
5959
*/
60-
public function gauge(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
60+
public function gauge(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
6161
{
6262
$this->datadogClient->gauge(
6363
$stat,
@@ -70,7 +70,7 @@ public function gauge(string $stat, float $value, float $sampleRate = 1.0, array
7070
/**
7171
* @inheritDoc
7272
*/
73-
public function histogram(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
73+
public function histogram(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
7474
{
7575
$this->datadogClient->histogram(
7676
$stat,
@@ -83,7 +83,7 @@ public function histogram(string $stat, float $value, float $sampleRate = 1.0, a
8383
/**
8484
* @inheritDoc
8585
*/
86-
public function distribution(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
86+
public function distribution(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
8787
{
8888
$this->datadogClient->distribution(
8989
$stat,
@@ -96,7 +96,7 @@ public function distribution(string $stat, float $value, float $sampleRate = 1.0
9696
/**
9797
* @inheritDoc
9898
*/
99-
public function set(string $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
99+
public function set(string|\UnitEnum $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
100100
{
101101
$this->datadogClient->set(
102102
$stat,
@@ -109,7 +109,7 @@ public function set(string $stat, float|string $value, float $sampleRate = 1.0,
109109
/**
110110
* @inheritDoc
111111
*/
112-
public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
112+
public function increment(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
113113
{
114114
$this->datadogClient->increment(
115115
$stats,
@@ -122,7 +122,7 @@ public function increment(array|string $stats, float $sampleRate = 1.0, array $t
122122
/**
123123
* @inheritDoc
124124
*/
125-
public function decrement(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = -1): void
125+
public function decrement(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = -1): void
126126
{
127127
$this->datadogClient->decrement(
128128
$stats,
@@ -135,7 +135,7 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
135135
/**
136136
* @inheritDoc
137137
*/
138-
public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, array $tags = null): void
138+
public function updateStats(array|string|\UnitEnum $stats, int $delta = 1, $sampleRate = 1.0, array $tags = null): void
139139
{
140140
$this->datadogClient->updateStats(
141141
$stats,

src/Adapters/InMemory/InMemoryClientAdapter.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function flush(): void
5858
/**
5959
* @inheritDoc
6060
*/
61-
public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
61+
public function timing(string|\UnitEnum $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
6262
{
6363
$this->stats->recordTiming(
6464
new InMemoryTimingRecord(
@@ -74,7 +74,7 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
7474
/**
7575
* @inheritDoc
7676
*/
77-
public function gauge(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
77+
public function gauge(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
7878
{
7979
$this->stats->recordGauge(
8080
new InMemoryGaugeRecord(
@@ -90,7 +90,7 @@ public function gauge(string $stat, float $value, float $sampleRate = 1.0, array
9090
/**
9191
* @inheritDoc
9292
*/
93-
public function histogram(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
93+
public function histogram(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
9494
{
9595
$this->stats->recordHistogram(
9696
new InMemoryHistogramRecord(
@@ -106,7 +106,7 @@ public function histogram(string $stat, float $value, float $sampleRate = 1.0, a
106106
/**
107107
* @inheritDoc
108108
*/
109-
public function distribution(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
109+
public function distribution(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
110110
{
111111
$this->stats->recordDistribution(
112112
new InMemoryDistributionRecord(
@@ -122,7 +122,7 @@ public function distribution(string $stat, float $value, float $sampleRate = 1.0
122122
/**
123123
* @inheritDoc
124124
*/
125-
public function set(string $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
125+
public function set(string|\UnitEnum $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
126126
{
127127
$this->stats->recordSet(
128128
new InMemorySetRecord(
@@ -138,15 +138,15 @@ public function set(string $stat, float|string $value, float $sampleRate = 1.0,
138138
/**
139139
* @inheritDoc
140140
*/
141-
public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
141+
public function increment(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
142142
{
143143
$this->updateStats($stats, $value, $sampleRate, $tags);
144144
}
145145

146146
/**
147147
* @inheritDoc
148148
*/
149-
public function decrement(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = -1): void
149+
public function decrement(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = -1): void
150150
{
151151
if ($value > 0) {
152152
$value *= -1;
@@ -158,7 +158,7 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
158158
/**
159159
* @inheritDoc
160160
*/
161-
public function updateStats(array|string $stats, int $delta = 1, float $sampleRate = 1.0, array $tags = []): void
161+
public function updateStats(array|string|\UnitEnum $stats, int $delta = 1, float $sampleRate = 1.0, array $tags = []): void
162162
{
163163
$stats = (array) $stats;
164164
$now = $this->clock->now();

src/Adapters/League/LeagueStatsDClientAdapter.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Cosmastech\StatsDClientAdapter\Adapters\League;
44

55
use Closure;
6+
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\ConvertsStatTrait;
67
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\HasDefaultTagsTrait;
78
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TagNormalizerAwareTrait;
89
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\TimeClosureTrait;
@@ -11,16 +12,19 @@
1112
use Cosmastech\StatsDClientAdapter\TagNormalizers\NoopTagNormalizer;
1213
use Cosmastech\StatsDClientAdapter\TagNormalizers\TagNormalizer;
1314
use Cosmastech\StatsDClientAdapter\Utility\Clock;
15+
use Cosmastech\StatsDClientAdapter\Utility\EnumConverter;
1416
use Cosmastech\StatsDClientAdapter\Utility\SampleRateDecider\Contracts\SampleRateSendDecider as SampleRateSendDeciderInterface;
1517
use Cosmastech\StatsDClientAdapter\Utility\SampleRateDecider\SampleRateSendDecider;
1618
use League\StatsD\Client;
1719
use League\StatsD\Exception\ConfigurationException;
1820
use League\StatsD\Exception\ConnectionException;
1921
use League\StatsD\StatsDClient as LeagueStatsDClientInterface;
2022
use Psr\Clock\ClockInterface;
23+
use UnitEnum;
2124

2225
class LeagueStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAware
2326
{
27+
use ConvertsStatTrait;
2428
use HasDefaultTagsTrait;
2529
use TagNormalizerAwareTrait;
2630
use TimeClosureTrait;
@@ -32,7 +36,7 @@ class LeagueStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAwa
3236
protected readonly ClockInterface $clock;
3337

3438
/**
35-
* @var Closure(string, float, float, array<mixed, mixed>):void
39+
* @var Closure(string|UnitEnum, float, float, array<mixed, mixed>):void
3640
*/
3741
protected Closure $unavailableStatHandler;
3842

@@ -126,22 +130,22 @@ protected function getUnavailableStatHandler(): Closure
126130

127131

128132
/**
129-
* @param string $stat
133+
* @param string|UnitEnum $stat
130134
* @param float $durationMs
131135
* @param float $sampleRate
132136
* @param array<mixed, mixed> $tags
133137
* @return void
134138
*
135139
* @throws ConnectionException
136140
*/
137-
public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
141+
public function timing(string|UnitEnum $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
138142
{
139143
if (! $this->sampleRateSendDecider->decide($sampleRate)) {
140144
return;
141145
}
142146

143147
$this->leagueStatsDClient->timing(
144-
$stat,
148+
$this->convertStat($stat),
145149
$durationMs,
146150
$this->normalizeTags($this->mergeWithDefaultTags($tags))
147151
);
@@ -150,40 +154,40 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
150154
/**
151155
* @throws ConnectionException
152156
*/
153-
public function gauge(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
157+
public function gauge(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
154158
{
155159
if (! $this->sampleRateSendDecider->decide($sampleRate)) {
156160
return;
157161
}
158162

159163
$this->leagueStatsDClient->gauge(
160-
$stat,
164+
$this->convertStat($stat),
161165
$value,
162166
$this->normalizeTags($this->mergeWithDefaultTags($tags))
163167
);
164168
}
165169

166-
public function histogram(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
170+
public function histogram(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
167171
{
168-
$this->handleUnavailableStat($stat, $value, $sampleRate, $tags);
172+
$this->handleUnavailableStat($this->convertStat($stat), $value, $sampleRate, $tags);
169173
}
170174

171-
public function distribution(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
175+
public function distribution(string|\UnitEnum $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
172176
{
173-
$this->handleUnavailableStat($stat, $value, $sampleRate, $tags);
177+
$this->handleUnavailableStat($this->convertStat($stat), $value, $sampleRate, $tags);
174178
}
175179

176180
/**
177181
* @throws ConnectionException
178182
*/
179-
public function set(string $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
183+
public function set(string|\UnitEnum $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void
180184
{
181185
if (! $this->sampleRateSendDecider->decide($sampleRate)) {
182186
return;
183187
}
184188

185189
$this->leagueStatsDClient->set(
186-
$stat,
190+
$this->convertStat($stat),
187191
$value,
188192
$this->normalizeTags($this->mergeWithDefaultTags($tags))
189193
);
@@ -192,7 +196,7 @@ public function set(string $stat, float|string $value, float $sampleRate = 1.0,
192196
/**
193197
* @throws ConnectionException
194198
*/
195-
public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
199+
public function increment(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
196200
{
197201
$this->leagueStatsDClient->increment(
198202
$stats,
@@ -205,7 +209,7 @@ public function increment(array|string $stats, float $sampleRate = 1.0, array $t
205209
/**
206210
* @throws ConnectionException
207211
*/
208-
public function decrement(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
212+
public function decrement(array|string|\UnitEnum $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void
209213
{
210214
$this->leagueStatsDClient->decrement(
211215
$stats,
@@ -218,7 +222,7 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
218222
/**
219223
* @throws ConnectionException
220224
*/
221-
public function updateStats(array|string $stats, int $delta = 1, float $sampleRate = 1.0, array $tags = []): void
225+
public function updateStats(array|string|\UnitEnum $stats, int $delta = 1, float $sampleRate = 1.0, array $tags = []): void
222226
{
223227
$this->increment(
224228
$stats,

0 commit comments

Comments
 (0)