Skip to content

Commit

Permalink
Fix tracking of float counters in Redis (#42)
Browse files Browse the repository at this point in the history
* Fix the check for new counters when using floats

Signed-off-by: Justin Nuß <[email protected]>

Co-authored-by: Lukas Kämmerling <[email protected]>
  • Loading branch information
nussjustin-hmmh and LKaemmerling authored Mar 5, 2021
1 parent fd1aaac commit 5d27b6d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
6 changes: 5 additions & 1 deletion examples/some_counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
}
$registry = new CollectorRegistry($adapter);

$count = preg_match('/^[0-9]+$/', $_GET['c'])
? intval($_GET['c'])
: floatval($_GET['c']);

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy($_GET['c'], ['blue']);
$counter->incBy($count, ['blue']);

echo "OK\n";
4 changes: 2 additions & 2 deletions src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ public function updateCounter(array $data): void
$this->redis->eval(
<<<LUA
local result = redis.call(ARGV[1], KEYS[1], ARGV[3], ARGV[2])
if result == tonumber(ARGV[2]) then
local added = redis.call('sAdd', KEYS[2], KEYS[1])
if added == 1 then
redis.call('hMSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
return result
LUA
Expand Down
26 changes: 23 additions & 3 deletions tests/Test/BlackBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,24 @@ public function gaugesShouldBeOverwritten(): void

/**
* @test
* @dataProvider countersDataProvider
* @param int|float $increment
*/
public function countersShouldIncrementAtomically(): void
public function countersShouldIncrementAtomically($increment): void
{
$start = microtime(true);
$promises = [];
$sum = 0;
$n = $increment;
for ($i = 0; $i < 1100; $i++) {
$promises[] = $this->client->getAsync('/examples/some_counter.php?c=' . $i . '&adapter=' . $this->adapter);
$sum += $i;
if (is_float($n)) {
$url = '/examples/some_counter.php?c=' . number_format($n, 2) . '&adapter=' . $this->adapter;
} else {
$url = '/examples/some_counter.php?c=' . $n . '&adapter=' . $this->adapter;
}
$promises[] = $this->client->getAsync($url);
$sum += $n;
$n += $increment;
}

Promise\settle($promises)->wait();
Expand All @@ -85,6 +94,17 @@ public function countersShouldIncrementAtomically(): void
self::assertThat($body, self::stringContains('test_some_counter{type="blue"} ' . $sum));
}

/**
* @return array<int, array<float|int>>
*/
public function countersDataProvider(): array
{
return [
[1],
[0.5]
];
}

/**
* @test
*/
Expand Down

0 comments on commit 5d27b6d

Please sign in to comment.