3
3
namespace Cosmastech \StatsDClientAdapter \Adapters \League ;
4
4
5
5
use Closure ;
6
+ use Cosmastech \StatsDClientAdapter \Adapters \Concerns \ConvertsStatTrait ;
6
7
use Cosmastech \StatsDClientAdapter \Adapters \Concerns \HasDefaultTagsTrait ;
7
8
use Cosmastech \StatsDClientAdapter \Adapters \Concerns \TagNormalizerAwareTrait ;
8
9
use Cosmastech \StatsDClientAdapter \Adapters \Concerns \TimeClosureTrait ;
11
12
use Cosmastech \StatsDClientAdapter \TagNormalizers \NoopTagNormalizer ;
12
13
use Cosmastech \StatsDClientAdapter \TagNormalizers \TagNormalizer ;
13
14
use Cosmastech \StatsDClientAdapter \Utility \Clock ;
15
+ use Cosmastech \StatsDClientAdapter \Utility \EnumConverter ;
14
16
use Cosmastech \StatsDClientAdapter \Utility \SampleRateDecider \Contracts \SampleRateSendDecider as SampleRateSendDeciderInterface ;
15
17
use Cosmastech \StatsDClientAdapter \Utility \SampleRateDecider \SampleRateSendDecider ;
16
18
use League \StatsD \Client ;
17
19
use League \StatsD \Exception \ConfigurationException ;
18
20
use League \StatsD \Exception \ConnectionException ;
19
21
use League \StatsD \StatsDClient as LeagueStatsDClientInterface ;
20
22
use Psr \Clock \ClockInterface ;
23
+ use UnitEnum ;
21
24
22
25
class LeagueStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAware
23
26
{
27
+ use ConvertsStatTrait;
24
28
use HasDefaultTagsTrait;
25
29
use TagNormalizerAwareTrait;
26
30
use TimeClosureTrait;
@@ -32,7 +36,7 @@ class LeagueStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAwa
32
36
protected readonly ClockInterface $ clock ;
33
37
34
38
/**
35
- * @var Closure(string, float, float, array<mixed, mixed>):void
39
+ * @var Closure(string|UnitEnum , float, float, array<mixed, mixed>):void
36
40
*/
37
41
protected Closure $ unavailableStatHandler ;
38
42
@@ -126,22 +130,22 @@ protected function getUnavailableStatHandler(): Closure
126
130
127
131
128
132
/**
129
- * @param string $stat
133
+ * @param string|UnitEnum $stat
130
134
* @param float $durationMs
131
135
* @param float $sampleRate
132
136
* @param array<mixed, mixed> $tags
133
137
* @return void
134
138
*
135
139
* @throws ConnectionException
136
140
*/
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
138
142
{
139
143
if (! $ this ->sampleRateSendDecider ->decide ($ sampleRate )) {
140
144
return ;
141
145
}
142
146
143
147
$ this ->leagueStatsDClient ->timing (
144
- $ stat ,
148
+ $ this -> convertStat ( $ stat) ,
145
149
$ durationMs ,
146
150
$ this ->normalizeTags ($ this ->mergeWithDefaultTags ($ tags ))
147
151
);
@@ -150,40 +154,40 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
150
154
/**
151
155
* @throws ConnectionException
152
156
*/
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
154
158
{
155
159
if (! $ this ->sampleRateSendDecider ->decide ($ sampleRate )) {
156
160
return ;
157
161
}
158
162
159
163
$ this ->leagueStatsDClient ->gauge (
160
- $ stat ,
164
+ $ this -> convertStat ( $ stat) ,
161
165
$ value ,
162
166
$ this ->normalizeTags ($ this ->mergeWithDefaultTags ($ tags ))
163
167
);
164
168
}
165
169
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
167
171
{
168
- $ this ->handleUnavailableStat ($ stat , $ value , $ sampleRate , $ tags );
172
+ $ this ->handleUnavailableStat ($ this -> convertStat ( $ stat) , $ value , $ sampleRate , $ tags );
169
173
}
170
174
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
172
176
{
173
- $ this ->handleUnavailableStat ($ stat , $ value , $ sampleRate , $ tags );
177
+ $ this ->handleUnavailableStat ($ this -> convertStat ( $ stat) , $ value , $ sampleRate , $ tags );
174
178
}
175
179
176
180
/**
177
181
* @throws ConnectionException
178
182
*/
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
180
184
{
181
185
if (! $ this ->sampleRateSendDecider ->decide ($ sampleRate )) {
182
186
return ;
183
187
}
184
188
185
189
$ this ->leagueStatsDClient ->set (
186
- $ stat ,
190
+ $ this -> convertStat ( $ stat) ,
187
191
$ value ,
188
192
$ this ->normalizeTags ($ this ->mergeWithDefaultTags ($ tags ))
189
193
);
@@ -192,7 +196,7 @@ public function set(string $stat, float|string $value, float $sampleRate = 1.0,
192
196
/**
193
197
* @throws ConnectionException
194
198
*/
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
196
200
{
197
201
$ this ->leagueStatsDClient ->increment (
198
202
$ stats ,
@@ -205,7 +209,7 @@ public function increment(array|string $stats, float $sampleRate = 1.0, array $t
205
209
/**
206
210
* @throws ConnectionException
207
211
*/
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
209
213
{
210
214
$ this ->leagueStatsDClient ->decrement (
211
215
$ stats ,
@@ -218,7 +222,7 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
218
222
/**
219
223
* @throws ConnectionException
220
224
*/
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
222
226
{
223
227
$ this ->increment (
224
228
$ stats ,
0 commit comments