@@ -434,7 +434,7 @@ private function collectHistograms(): array
434434 $ allLabelValues [] = $ d ['labelValues ' ];
435435 }
436436 if (json_last_error () !== JSON_ERROR_NONE ) {
437- $ this ->throwMetricJsonException ($ key, $ raw );
437+ $ this ->throwMetricJsonException ($ key );
438438 }
439439
440440 // We need set semantics.
@@ -622,11 +622,11 @@ private function collectGauges(bool $sortMetrics = true): array
622622 'labelValues ' => json_decode ($ k , true ),
623623 'value ' => $ value ,
624624 ];
625+ if (json_last_error () !== JSON_ERROR_NONE ) {
626+ $ this ->throwMetricJsonException ($ key , $ gauge ['name ' ]);
627+ }
625628 }
626629
627- if (json_last_error () !== JSON_ERROR_NONE ) {
628- $ this ->throwMetricJsonException ($ key , $ raw );
629- }
630630 if ($ sortMetrics ) {
631631 usort ($ gauge ['samples ' ], function ($ a , $ b ): int {
632632 return strcmp (implode ("" , $ a ['labelValues ' ]), implode ("" , $ b ['labelValues ' ]));
@@ -640,6 +640,7 @@ private function collectGauges(bool $sortMetrics = true): array
640640
641641 /**
642642 * @return mixed[]
643+ * @throws MetricJsonException
643644 */
644645 private function collectCounters (bool $ sortMetrics = true ): array
645646 {
@@ -652,6 +653,7 @@ private function collectCounters(bool $sortMetrics = true): array
652653 continue ;
653654 }
654655 $ counter = json_decode ($ raw ['__meta ' ], true );
656+
655657 unset($ raw ['__meta ' ]);
656658 $ counter ['samples ' ] = [];
657659 foreach ($ raw as $ k => $ value ) {
@@ -661,11 +663,12 @@ private function collectCounters(bool $sortMetrics = true): array
661663 'labelValues ' => json_decode ($ k , true ),
662664 'value ' => $ value ,
663665 ];
664- }
665666
666- if (json_last_error () !== JSON_ERROR_NONE ) {
667- $ this ->throwMetricJsonException ($ key , $ raw );
667+ if (json_last_error () !== JSON_ERROR_NONE ) {
668+ $ this ->throwMetricJsonException ($ key , $ counter ['name ' ]);
669+ }
668670 }
671+
669672 if ($ sortMetrics ) {
670673 usort ($ counter ['samples ' ], function ($ a , $ b ): int {
671674 return strcmp (implode ("" , $ a ['labelValues ' ]), implode ("" , $ b ['labelValues ' ]));
@@ -736,10 +739,16 @@ private function decodeLabelValues(string $values): array
736739 return $ decodedValues ;
737740 }
738741
739- private function throwMetricJsonException (string $ redisKey , $ raw ): void
742+ /**
743+ * @param string $redisKey
744+ * @param string|null $metricName
745+ * @return void
746+ * @throws MetricJsonException
747+ */
748+ private function throwMetricJsonException (string $ redisKey , ?string $ metricName = null ): void
740749 {
741- $ metaData = is_array ( $ raw ) && isset ( $ raw [ ' _meta ' ]) ? $ raw [ ' _meta ' ] : ' undefined ' ;
742- $ message = 'Json error: ' . json_last_error_msg () . ' redis key : ' . $ redisKey . ' raw meta data : ' . $ metaData ;
743- throw new MetricJsonException ($ message , 0 , null , $ metaData );
750+ $ metricName = $ metricName ?? ' unknown ' ;
751+ $ message = 'Json error: ' . json_last_error_msg () . ' redis key : ' . $ redisKey . ' metric name : ' . $ metricName ;
752+ throw new MetricJsonException ($ message , 0 , null , $ metricName );
744753 }
745754}
0 commit comments