Skip to content

Commit 1088114

Browse files
Learned how to wipe the storage adapter from the registry (#89)
* Learned how to wipe the storage adapter from the registry Signed-off-by: Mariano Benítez Mulet <pachicodev@gmail.com> * Learned how to wipe the storage adapter from the registry Signed-off-by: Mariano Benítez Mulet <pachicodev@gmail.com> * Learned how to wipe the storage adapter from the registry Signed-off-by: Mariano Benítez Mulet <pachicodev@gmail.com> Co-authored-by: Mariano Benítez Mulet <mariano.benitez@edreamsodigeo.com>
1 parent 0f7f022 commit 1088114

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Prometheus/CollectorRegistry.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public static function getDefault(): CollectorRegistry
6868
return self::$defaultRegistry ?? (self::$defaultRegistry = new self(new Redis())); /** @phpstan-ignore-line */
6969
}
7070

71+
/**
72+
* Removes all previously stored metrics from underlying storage adapter
73+
*
74+
* @return void
75+
*/
76+
public function wipeStorage(): void
77+
{
78+
$this->storageAdapter->wipeStorage();
79+
}
80+
7181
/**
7282
* @return MetricFamilySamples[]
7383
*/

src/Prometheus/RegistryInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
interface RegistryInterface
99
{
10+
/**
11+
* Removes all previously stored metrics from underlying storage adapter
12+
*
13+
* @return void
14+
*/
15+
public function wipeStorage(): void;
16+
1017
/**
1118
* @return MetricFamilySamples[]
1219
*/

tests/Test/Prometheus/AbstractCollectorRegistryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,23 @@ public function itShouldThrowAnExceptionOnInvalidMetricLabelDataProvider(): arra
418418
];
419419
}
420420

421+
/**
422+
* @test
423+
*/
424+
public function itShouldWipeTheUnderlyingStorageAdapterWhenInvokingWipeStorageMethod(): void
425+
{
426+
// Arrange
427+
$registry = new CollectorRegistry($this->adapter);
428+
$registry->registerCounter("foo", "myCounter", "Help text");
429+
$registry->registerGauge("foo", "myGauge", "Help text");
430+
$registry->registerHistogram("foo", "myHistogram", "Help text");
431+
432+
// Act
433+
$registry->wipeStorage();
434+
435+
// Assert
436+
self::assertEmpty($registry->getMetricFamilySamples());
437+
}
421438

422439
abstract public function configureAdapter(): void;
423440
}

0 commit comments

Comments
 (0)