diff --git a/src/Prometheus/CollectorRegistry.php b/src/Prometheus/CollectorRegistry.php index 8395c53e..37412a7e 100644 --- a/src/Prometheus/CollectorRegistry.php +++ b/src/Prometheus/CollectorRegistry.php @@ -78,6 +78,19 @@ public function wipeStorage(): void $this->storageAdapter->wipeStorage(); } + /** + * Removes a specific previously stored metric from underlying storage adapter + * + * @param string $type + * @param string $name + * + * @return void + */ + public function wipeKey(string $type, string $name): void + { + $this->storageAdapter->wipeKey($type, $name); + } + /** * @return MetricFamilySamples[] */ diff --git a/src/Prometheus/RegistryInterface.php b/src/Prometheus/RegistryInterface.php index a66ae5e6..9bbf9ef6 100644 --- a/src/Prometheus/RegistryInterface.php +++ b/src/Prometheus/RegistryInterface.php @@ -14,6 +14,16 @@ interface RegistryInterface */ public function wipeStorage(): void; + /** + * Removes a specific previously stored metric from underlying storage adapter + * + * @param string $type + * @param string $name + * + * @return void + */ + public function wipeKey(string $type, string $name): void; + /** * @return MetricFamilySamples[] */ diff --git a/src/Prometheus/Storage/APC.php b/src/Prometheus/Storage/APC.php index 5f747231..0cf02428 100644 --- a/src/Prometheus/Storage/APC.php +++ b/src/Prometheus/Storage/APC.php @@ -230,6 +230,14 @@ public function wipeStorage(): void } } + /** + * @inheritDoc + */ + public function wipeKey(string $type, string $key): void + { + // not implemented yet + } + /** * @param mixed[] $data * @return string diff --git a/src/Prometheus/Storage/APCng.php b/src/Prometheus/Storage/APCng.php index 5448fc95..6ba10369 100644 --- a/src/Prometheus/Storage/APCng.php +++ b/src/Prometheus/Storage/APCng.php @@ -303,6 +303,14 @@ public function wipeStorage(): void apcu_delete($this->metainfoCacheKey); } + /** + * @inheritDoc + */ + public function wipeKey(string $type, string $key): void + { + // not implemented yet + } + /** * Scans the APCu keyspace for all metainfo keys. A new metainfo cache array is built, * which references all metadata keys in APCu at that moment. This prevents a corner-case diff --git a/src/Prometheus/Storage/Adapter.php b/src/Prometheus/Storage/Adapter.php index fc39dd81..bbc1c8d4 100644 --- a/src/Prometheus/Storage/Adapter.php +++ b/src/Prometheus/Storage/Adapter.php @@ -49,4 +49,14 @@ public function updateCounter(array $data): void; * @return void */ public function wipeStorage(): void; + + /** + * Removes a specific previously stored metric from underlying storage + * + * @param string $type + * @param string $key + * @throws StorageException + * @return void + */ + public function wipeKey(string $type, string $key): void; } diff --git a/src/Prometheus/Storage/InMemory.php b/src/Prometheus/Storage/InMemory.php index a79714c7..ae43f8ff 100644 --- a/src/Prometheus/Storage/InMemory.php +++ b/src/Prometheus/Storage/InMemory.php @@ -61,6 +61,14 @@ public function wipeStorage(): void $this->summaries = []; } + /** + * @inheritDoc + */ + public function wipeKey(string $type, string $key): void + { + // not implemented yet + } + /** * @return MetricFamilySamples[] */ diff --git a/src/Prometheus/Storage/Redis.php b/src/Prometheus/Storage/Redis.php index 122d11a3..686c170a 100644 --- a/src/Prometheus/Storage/Redis.php +++ b/src/Prometheus/Storage/Redis.php @@ -139,6 +139,42 @@ public function wipeStorage(): void ); } + /** + * @inheritDoc + */ + public function wipeKey(string $type, string $key): void + { + $this->ensureOpenConnection(); + + $searchPattern = ""; + + $globalPrefix = $this->redis->getOption(\Redis::OPT_PREFIX); + // @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int + if (is_string($globalPrefix)) { + $searchPattern .= $globalPrefix; + } + + $searchPattern .= self::$prefix; + $searchPattern .= ":" . $type . ":" . $key; + + $this->redis->eval( + <<ensureOpenConnection(); + + $searchPattern = ""; + + $globalPrefix = $this->redis->getOption(\Redis::OPT_PREFIX); + // @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int + if (is_string($globalPrefix)) { + $searchPattern .= $globalPrefix; + } + + $searchPattern .= self::$prefix; + $searchPattern .= ":" . $type . ":" . $key; + + $this->redis->eval( + <<