Skip to content

Commit 2c4dc3f

Browse files
committed
fix(storage): Fix RedisStorage doesn't clear histograms
1 parent e40e64f commit 2c4dc3f

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"ext-redis": "*",
1515
"laravel/framework": "^9.0 || ^10.0",
1616
"webmozart/assert": "^1.11",
17-
"zlodes/prometheus-client": "^1.1"
17+
"zlodes/prometheus-client": "^1.1.2"
1818
},
1919
"require-dev": {
2020
"ergebnis/composer-normalize": "dev-main",

src/Storage/RedisStorage.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ public function clear(): void
4343
$this->connection->command('DEL', [self::SIMPLE_HASH_NAME]);
4444
$this->connection->command('DEL', [self::HISTOGRAM_SUM_HASH_NAME]);
4545
$this->connection->command('DEL', [self::HISTOGRAM_COUNT_HASH_NAME]);
46+
47+
// Using leading asterisk to ignore Laravel Redis connection prefix (like laravel_database_)
48+
$histogramKeyPattern = ['*' . self::HISTOGRAM_HASH_NAME_PREFIX . '*'];
49+
4650
$this->connection->command('EVAL', [
47-
"return redis.call('del', unpack(redis.call('keys', KEYS[1])))",
48-
[self::HISTOGRAM_HASH_NAME_PREFIX],
51+
"return redis.call('del', unpack(redis.call('keys', ARGV[1])))",
52+
$histogramKeyPattern,
4953
0,
5054
]);
5155
} catch (Exception $e) {

tests/Storage/RedisStorageTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ public function setUp(): void
3535
$redis->command('FLUSHALL');
3636
}
3737

38+
public function testClear(): void
39+
{
40+
/** @var Connection $redis */
41+
$redis = $this->app->make(Connection::class);
42+
43+
$storage = $this->app->make(RedisStorage::class, [
44+
'connection' => $redis,
45+
]);
46+
47+
$storage->setValue(new MetricValue(
48+
new MetricNameWithLabels('foo', []),
49+
42,
50+
));
51+
52+
$storage->persistHistogram(
53+
new MetricValue(
54+
new MetricNameWithLabels('bar'),
55+
0.5,
56+
),
57+
[0.1, 0.2, 0.3]
58+
);
59+
60+
$storage->clear();
61+
62+
$keys = $redis->command('KEYS', ['*']);
63+
64+
self::assertEquals([], $keys);
65+
}
66+
3867
public function testRedisExceptionWhileFetch(): void
3968
{
4069
$storage = new RedisStorage(

0 commit comments

Comments
 (0)