Skip to content

Commit e413527

Browse files
committed
fix(fetcher): ignore metric existing in storage, but missed in registry
1 parent eec8db2 commit e413527

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/Fetcher/StoredMetricsFetcher.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Zlodes\PrometheusClient\Fetcher;
66

77
use Generator;
8+
use Zlodes\PrometheusClient\Exception\MetricNotFoundException;
89
use Zlodes\PrometheusClient\Fetcher\DTO\FetchedMetric;
910
use Zlodes\PrometheusClient\Metric\Counter;
1011
use Zlodes\PrometheusClient\Metric\Gauge;
@@ -58,12 +59,16 @@ private function fetchCounters(): Generator
5859
}
5960

6061
foreach ($countersByName as $metricName => $values) {
61-
$metric = $this->registry->getMetric($metricName, Counter::class);
62+
try {
63+
$metric = $this->registry->getMetric($metricName, Counter::class);
6264

63-
yield new FetchedMetric(
64-
$metric,
65-
$values
66-
);
65+
yield new FetchedMetric(
66+
$metric,
67+
$values
68+
);
69+
} catch (MetricNotFoundException) {
70+
// Skip. Metric might be removed from the registry after using
71+
}
6772
}
6873
}
6974

tests/Fetcher/StoredMetricsFetcherTest.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Generator;
88
use Mockery;
99
use PHPUnit\Framework\TestCase;
10+
use Psr\Log\NullLogger;
11+
use Zlodes\PrometheusClient\Collector\ByType\CounterCollector;
1012
use Zlodes\PrometheusClient\Fetcher\DTO\FetchedMetric;
1113
use Zlodes\PrometheusClient\Fetcher\StoredMetricsFetcher;
1214
use Zlodes\PrometheusClient\Metric\Counter;
@@ -22,6 +24,10 @@
2224
use Zlodes\PrometheusClient\Storage\DTO\MetricNameWithLabels;
2325
use Zlodes\PrometheusClient\Storage\DTO\MetricValue;
2426
use Zlodes\PrometheusClient\Storage\DTO\SummaryMetricValue;
27+
use Zlodes\PrometheusClient\Storage\InMemory\InMemoryCounterStorage;
28+
use Zlodes\PrometheusClient\Tests\Stubs\GaugeStorageStub;
29+
use Zlodes\PrometheusClient\Tests\Stubs\HistogramStorageStub;
30+
use Zlodes\PrometheusClient\Tests\Stubs\SummaryStorageStub;
2531

2632
/**
2733
* Kinda stupid test to check that the fetcher return metric as expected: with correct values and order
@@ -108,7 +114,7 @@ public function testFetch(): void
108114

109115
$summaryStorageMock
110116
->expects('fetchSummaries')
111-
->andReturnUsing(static function(): Generator {
117+
->andReturnUsing(static function (): Generator {
112118
yield new SummaryMetricValue(
113119
new MetricNameWithLabels('memory_usage'),
114120
[300, 500, 200, 500, 400]
@@ -245,4 +251,34 @@ public function testFetch(): void
245251
self::assertSame([], $fetchedSummary->values[3]->metricNameWithLabels->labels);
246252
self::assertSame(5, $fetchedSummary->values[3]->value);
247253
}
254+
255+
/**
256+
* Storage can return metric that was removed from the registry, it shouldn't break the fetcher
257+
*/
258+
public function testRemovedMetricExistsInStorage(): void
259+
{
260+
$counterStorage = new InMemoryCounterStorage();
261+
262+
$counter = new Counter('foo_counter', 'help');
263+
264+
$counterCollector = new CounterCollector(
265+
$counter,
266+
$counterStorage,
267+
new NullLogger(),
268+
);
269+
270+
$counterCollector->increment();
271+
272+
$fetcher = new StoredMetricsFetcher(
273+
registry: new ArrayRegistry(),
274+
counterStorage: $counterStorage,
275+
gaugeStorage: new GaugeStorageStub(),
276+
histogramStorage: new HistogramStorageStub(),
277+
summaryStorage: new SummaryStorageStub(),
278+
);
279+
280+
$fetched = [...$fetcher->fetch()];
281+
282+
self::assertEmpty($fetched);
283+
}
248284
}

0 commit comments

Comments
 (0)