@@ -169,12 +169,12 @@ private function valueKey(array $data): string
169169 * @return MetricFamilySamples[]
170170 * @throws StorageException
171171 */
172- public function collect (): array
172+ public function collect (bool $ sortMetrics = true ): array
173173 {
174174 $ this ->ensureOpenConnection ();
175175 $ metrics = $ this ->collectHistograms ();
176- $ metrics = array_merge ($ metrics , $ this ->collectGauges ());
177- $ metrics = array_merge ($ metrics , $ this ->collectCounters ());
176+ $ metrics = array_merge ($ metrics , $ this ->collectGauges ($ sortMetrics ));
177+ $ metrics = array_merge ($ metrics , $ this ->collectCounters ($ sortMetrics ));
178178 $ metrics = array_merge ($ metrics , $ this ->collectSummaries ());
179179 return array_map (
180180 function (array $ metric ): MetricFamilySamples {
@@ -572,7 +572,7 @@ private function collectSummaries(): array
572572 /**
573573 * @return mixed[]
574574 */
575- private function collectGauges (): array
575+ private function collectGauges (bool $ sortMetrics = true ): array
576576 {
577577 $ keys = $ this ->redis ->sMembers (self ::$ prefix . Gauge::TYPE . self ::PROMETHEUS_METRIC_KEYS_SUFFIX );
578578 sort ($ keys );
@@ -593,9 +593,13 @@ private function collectGauges(): array
593593 'value ' => $ value ,
594594 ];
595595 }
596- usort ($ gauge ['samples ' ], function ($ a , $ b ): int {
597- return strcmp (implode ("" , $ a ['labelValues ' ]), implode ("" , $ b ['labelValues ' ]));
598- });
596+
597+ if ($ sortMetrics ) {
598+ usort ($ gauge ['samples ' ], function ($ a , $ b ): int {
599+ return strcmp (implode ("" , $ a ['labelValues ' ]), implode ("" , $ b ['labelValues ' ]));
600+ });
601+ }
602+
599603 $ gauges [] = $ gauge ;
600604 }
601605 return $ gauges ;
@@ -604,7 +608,7 @@ private function collectGauges(): array
604608 /**
605609 * @return mixed[]
606610 */
607- private function collectCounters (): array
611+ private function collectCounters (bool $ sortMetrics = true ): array
608612 {
609613 $ keys = $ this ->redis ->sMembers (self ::$ prefix . Counter::TYPE . self ::PROMETHEUS_METRIC_KEYS_SUFFIX );
610614 sort ($ keys );
@@ -625,9 +629,13 @@ private function collectCounters(): array
625629 'value ' => $ value ,
626630 ];
627631 }
628- usort ($ counter ['samples ' ], function ($ a , $ b ): int {
629- return strcmp (implode ("" , $ a ['labelValues ' ]), implode ("" , $ b ['labelValues ' ]));
630- });
632+
633+ if ($ sortMetrics ) {
634+ usort ($ counter ['samples ' ], function ($ a , $ b ): int {
635+ return strcmp (implode ("" , $ a ['labelValues ' ]), implode ("" , $ b ['labelValues ' ]));
636+ });
637+ }
638+
631639 $ counters [] = $ counter ;
632640 }
633641 return $ counters ;
0 commit comments