Skip to content
This repository was archived by the owner on May 27, 2024. It is now read-only.

Commit 169b889

Browse files
authored
normalize sample rate (DataDog#128)
* normalize sample rate * lint
1 parent 6397e67 commit 169b889

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/DogStatsd.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private function flushTelemetry()
171171
*/
172172
public function timing($stat, $time, $sampleRate = 1.0, $tags = null)
173173
{
174-
$time = $this->normalizeStat($time);
174+
$time = $this->normalizeValue($time);
175175
$this->send(array($stat => "$time|ms"), $sampleRate, $tags);
176176
}
177177

@@ -200,7 +200,7 @@ public function microtiming($stat, $time, $sampleRate = 1.0, $tags = null)
200200
**/
201201
public function gauge($stat, $value, $sampleRate = 1.0, $tags = null)
202202
{
203-
$value = $this->normalizeStat($value);
203+
$value = $this->normalizeValue($value);
204204
$this->send(array($stat => "$value|g"), $sampleRate, $tags);
205205
}
206206

@@ -215,7 +215,7 @@ public function gauge($stat, $value, $sampleRate = 1.0, $tags = null)
215215
**/
216216
public function histogram($stat, $value, $sampleRate = 1.0, $tags = null)
217217
{
218-
$value = $this->normalizeStat($value);
218+
$value = $this->normalizeValue($value);
219219
$this->send(array($stat => "$value|h"), $sampleRate, $tags);
220220
}
221221

@@ -230,7 +230,7 @@ public function histogram($stat, $value, $sampleRate = 1.0, $tags = null)
230230
**/
231231
public function distribution($stat, $value, $sampleRate = 1.0, $tags = null)
232232
{
233-
$value = $this->normalizeStat($value);
233+
$value = $this->normalizeValue($value);
234234
$this->send(array($stat => "$value|d"), $sampleRate, $tags);
235235
}
236236

@@ -246,7 +246,7 @@ public function distribution($stat, $value, $sampleRate = 1.0, $tags = null)
246246
public function set($stat, $value, $sampleRate = 1.0, $tags = null)
247247
{
248248
if (!is_string($value)) {
249-
$value = $this->normalizeStat($value);
249+
$value = $this->normalizeValue($value);
250250
}
251251

252252
$this->send(array($stat => "$value|s"), $sampleRate, $tags);
@@ -295,7 +295,7 @@ public function decrement($stats, $sampleRate = 1.0, $tags = null, $value = -1)
295295
**/
296296
public function updateStats($stats, $delta = 1, $sampleRate = 1.0, $tags = null)
297297
{
298-
$delta = $this->normalizeStat($delta);
298+
$delta = $this->normalizeValue($delta);
299299
if (!is_array($stats)) {
300300
$stats = array($stats);
301301
}
@@ -381,6 +381,7 @@ private function normalizeTags($tags)
381381
**/
382382
public function send($data, $sampleRate = 1.0, $tags = null)
383383
{
384+
$sampleRate = $this->normalizeValue($sampleRate);
384385
$this->metrics_sent += count($data);
385386
// sampling
386387
$sampledData = array();
@@ -640,7 +641,7 @@ private function eventUdp($vals)
640641
*
641642
* @return string Formatted value
642643
*/
643-
private function normalizeStat($value)
644+
private function normalizeValue($value)
644645
{
645646
// Controlls the way things are converted to a string.
646647
// Otherwise localization settings impact float to string conversion (e.x 1.3 -> 1,3 and 10000 => 10,000)

tests/UnitTests/DogStatsd/SocketsTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,23 @@ public function testFloatLocalization()
13021302
setlocale(LC_ALL, $defaultLocale);
13031303
}
13041304

1305+
public function testSampleRateFloatLocalization()
1306+
{
1307+
$defaultLocale = setlocale(LC_ALL, 0);
1308+
setlocale(LC_ALL, 'de_DE');
1309+
$dog = new DogStatsd(array("disable_telemetry" => true));
1310+
1311+
while (!array_key_exists(0, $this->getSocketSpy()->argsFromSocketSendtoCalls)) {
1312+
$dog->timing('test', 21.21000, 0.3);
1313+
}
1314+
1315+
$this->assertSame(
1316+
'test:21.21|ms|@0.3',
1317+
$this->getSocketSpy()->argsFromSocketSendtoCalls[0][1]
1318+
);
1319+
setlocale(LC_ALL, $defaultLocale);
1320+
}
1321+
13051322
public function testMetricPrefix()
13061323
{
13071324
$dog = new DogStatsd(array("disable_telemetry" => false, "metric_prefix" => 'test_prefix'));

0 commit comments

Comments
 (0)