Skip to content

Commit d0ca3f7

Browse files
authored
fix: Correctly register histograms with Redis (#47)
* fix: Correctly register histograms with Redis Signed-off-by: Niels Grewe <niels@zasta.de> * tests(redis histograms): Test for correct histogram observing/collecting values with unusual precision Signed-off-by: Niels Grewe <niels@zasta.de>
1 parent 9a51177 commit d0ca3f7

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Prometheus/Storage/Redis.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,13 @@ public function updateHistogram(array $data): void
218218

219219
$this->redis->eval(
220220
<<<LUA
221-
local increment = redis.call('hIncrByFloat', KEYS[1], ARGV[1], ARGV[3])
221+
local result = redis.call('hIncrByFloat', KEYS[1], ARGV[1], ARGV[3])
222222
redis.call('hIncrBy', KEYS[1], ARGV[2], 1)
223-
if increment == ARGV[3] then
223+
if tonumber(result) >= tonumber(ARGV[3]) then
224224
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
225225
redis.call('sAdd', KEYS[2], KEYS[1])
226226
end
227+
return result
227228
LUA
228229
,
229230
[

tests/Test/Prometheus/AbstractHistogramTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,28 @@ abstract class AbstractHistogramTest extends TestCase
2121
*/
2222
public $adapter;
2323

24+
25+
/**
26+
* @var string
27+
*/
28+
private $savedPrecision;
29+
30+
private const HIGH_PRECISION = "17";
31+
2432
public function setUp(): void
2533
{
2634
$this->configureAdapter();
35+
$savedPrecision = ini_get('serialize_precision');
36+
if (!is_string($savedPrecision)) {
37+
$savedPrecision = '-1';
38+
}
39+
$this->savedPrecision = $savedPrecision;
40+
ini_set('serialize_precision', self::HIGH_PRECISION);
41+
}
42+
43+
public function tearDown(): void
44+
{
45+
ini_set('serialize_precision', $this->savedPrecision);
2746
}
2847

2948
abstract public function configureAdapter(): void;
@@ -237,6 +256,27 @@ public function itShouldObserveValuesOfTypeDouble(): void
237256
);
238257
}
239258

259+
/**
260+
* @test
261+
*/
262+
public function itShouldObserveValuesOfTypeDoubleWithUnusualPrecision(): void
263+
{
264+
$histogram = new Histogram(
265+
$this->adapter,
266+
'test',
267+
'some_metric',
268+
'this is for testing',
269+
[],
270+
[1.0]
271+
);
272+
ini_set("serialize_precision", "17");
273+
$histogram->observe(1.1 * 2 ** 53);
274+
self::assertThat(
275+
$this->adapter->collect(),
276+
self::logicalNot(self::isEmpty())
277+
);
278+
}
279+
240280
/**
241281
* @test
242282
*/

0 commit comments

Comments
 (0)