33namespace Cosmastech \StatsDClientAdapter \Adapters \League ;
44
55use Closure ;
6+ use Cosmastech \StatsDClientAdapter \Adapters \Concerns \ConvertsStatTrait ;
67use Cosmastech \StatsDClientAdapter \Adapters \Concerns \HasDefaultTagsTrait ;
78use Cosmastech \StatsDClientAdapter \Adapters \Concerns \TagNormalizerAwareTrait ;
89use Cosmastech \StatsDClientAdapter \Adapters \Concerns \TimeClosureTrait ;
1112use Cosmastech \StatsDClientAdapter \TagNormalizers \NoopTagNormalizer ;
1213use Cosmastech \StatsDClientAdapter \TagNormalizers \TagNormalizer ;
1314use Cosmastech \StatsDClientAdapter \Utility \Clock ;
15+ use Cosmastech \StatsDClientAdapter \Utility \EnumConverter ;
1416use Cosmastech \StatsDClientAdapter \Utility \SampleRateDecider \Contracts \SampleRateSendDecider as SampleRateSendDeciderInterface ;
1517use Cosmastech \StatsDClientAdapter \Utility \SampleRateDecider \SampleRateSendDecider ;
1618use League \StatsD \Client ;
1719use League \StatsD \Exception \ConfigurationException ;
1820use League \StatsD \Exception \ConnectionException ;
1921use League \StatsD \StatsDClient as LeagueStatsDClientInterface ;
2022use Psr \Clock \ClockInterface ;
23+ use UnitEnum ;
2124
2225class 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