File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
tests/Test/Prometheus/Storage Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -396,6 +396,9 @@ private function collectHistograms(): array
396396 $ histograms = [];
397397 foreach ($ keys as $ key ) {
398398 $ raw = $ this ->redis ->hGetAll (str_replace ($ this ->redis ->_prefix ('' ), '' , $ key ));
399+ if (!isset ($ raw ['__meta ' ])) {
400+ continue ;
401+ }
399402 $ histogram = json_decode ($ raw ['__meta ' ], true );
400403 unset($ raw ['__meta ' ]);
401404 $ histogram ['samples ' ] = [];
@@ -576,6 +579,9 @@ private function collectGauges(): array
576579 $ gauges = [];
577580 foreach ($ keys as $ key ) {
578581 $ raw = $ this ->redis ->hGetAll (str_replace ($ this ->redis ->_prefix ('' ), '' , $ key ));
582+ if (!isset ($ raw ['__meta ' ])) {
583+ continue ;
584+ }
579585 $ gauge = json_decode ($ raw ['__meta ' ], true );
580586 unset($ raw ['__meta ' ]);
581587 $ gauge ['samples ' ] = [];
@@ -605,6 +611,9 @@ private function collectCounters(): array
605611 $ counters = [];
606612 foreach ($ keys as $ key ) {
607613 $ raw = $ this ->redis ->hGetAll (str_replace ($ this ->redis ->_prefix ('' ), '' , $ key ));
614+ if (!isset ($ raw ['__meta ' ])) {
615+ continue ;
616+ }
608617 $ counter = json_decode ($ raw ['__meta ' ], true );
609618 unset($ raw ['__meta ' ]);
610619 $ counter ['samples ' ] = [];
Original file line number Diff line number Diff line change @@ -146,4 +146,33 @@ public function itShouldOnlyConnectOnceForInjectedRedisConnectionOnSubsequentCal
146146 $ this ->redisConnection ->rawCommand ('client ' , 'list ' )
147147 );
148148 }
149+
150+ /**
151+ * @test
152+ */
153+ public function itShouldCollectMetricsAndIgnoreInvalidMetricsWithoutMetaData (): void
154+ {
155+ $ redisConnection = $ this ->createMock (\Redis::class);
156+ $ redisConnection ->method ('isConnected ' )->willReturn (true );
157+ $ redisConnection ->method ('_prefix ' )->willReturnArgument (0 );
158+ $ redisConnection ->method ('keys ' )->with ('PROMETHEUS_summary_METRIC_KEYS:*:meta ' )->willReturn ([]);
159+ $ redisConnection ->method ('sMembers ' )
160+ ->withConsecutive (
161+ ['PROMETHEUS_histogram_METRIC_KEYS ' ],
162+ ['PROMETHEUS_gauge_METRIC_KEYS ' ],
163+ ['PROMETHEUS_counter_METRIC_KEYS ' ]
164+ )
165+ ->willReturnOnConsecutiveCalls (
166+ ['key_histogramm ' ],
167+ ['key_gauge ' ],
168+ ['key_counter ' ],
169+ )
170+ ;
171+ $ redisConnection ->method ('hGetAll ' )->willReturn (['any_invalid_data ' => '' ]);
172+
173+ $ redis = Redis::fromExistingConnection ($ redisConnection );
174+ $ metrics = $ redis ->collect ();
175+
176+ self ::assertEquals ([], $ metrics );
177+ }
149178}
You can’t perform that action at this time.
0 commit comments