Skip to content

Commit d96121c

Browse files
author
Vsevolod
committed
refactoring
1 parent ea6aeb7 commit d96121c

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

src/Prometheus/Exception/MetricJsonException.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
class MetricJsonException extends Exception
1111
{
1212

13-
private $metricMetaData;
14-
public function __construct($message = "", $code = 0, Exception $previous = null, string $metricMetaData)
13+
private $metricName;
14+
public function __construct($message = "", $code = 0, Exception $previous = null, ?string $metricName = null)
1515
{
1616
parent::__construct($message, $code, $previous);
17-
$this->metricMetaData = $metricMetaData;
17+
$this->metricName = $metricMetaData;
1818
}
1919

20-
public function getMetricMetaData(): string
20+
public function getMetricName(): ?string
2121
{
22-
return $this->metricMetaData;
22+
return $this->metricName;
2323
}
2424
}

src/Prometheus/Storage/Redis.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Prometheus/Storage/RedisNg.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ private function metaData(array $data): array
406406

407407
/**
408408
* @return mixed[]
409+
* @throws MetricJsonException
409410
*/
410411
private function collectHistograms(): array
411412
{
@@ -431,7 +432,7 @@ private function collectHistograms(): array
431432
}
432433

433434
if (json_last_error() !== JSON_ERROR_NONE) {
434-
$this->throwMetricJsonException($key, $raw);
435+
$this->throwMetricJsonException($key);
435436
}
436437

437438
// We need set semantics.
@@ -604,10 +605,9 @@ private function collectGauges(bool $sortMetrics = true): array
604605
'labelValues' => json_decode($k, true),
605606
'value' => $value,
606607
];
607-
}
608-
609-
if (json_last_error() !== JSON_ERROR_NONE) {
610-
$this->throwMetricJsonException($key, $raw);
608+
if (json_last_error() !== JSON_ERROR_NONE) {
609+
$this->throwMetricJsonException($key, $gauge['name']);
610+
}
611611
}
612612

613613
if ($sortMetrics) {
@@ -623,6 +623,7 @@ private function collectGauges(bool $sortMetrics = true): array
623623

624624
/**
625625
* @return mixed[]
626+
* @throws MetricJsonException
626627
*/
627628
private function collectCounters(bool $sortMetrics = true): array
628629
{
@@ -641,10 +642,9 @@ private function collectCounters(bool $sortMetrics = true): array
641642
'labelValues' => json_decode($k, true),
642643
'value' => $value,
643644
];
644-
}
645-
646-
if (json_last_error() !== JSON_ERROR_NONE) {
647-
$this->throwMetricJsonException($key, $raw);
645+
if (json_last_error() !== JSON_ERROR_NONE) {
646+
$this->throwMetricJsonException($key, $counter['name']);
647+
}
648648
}
649649

650650
if ($sortMetrics) {
@@ -717,10 +717,16 @@ private function decodeLabelValues(string $values): array
717717
return $decodedValues;
718718
}
719719

720-
private function throwMetricJsonException(string $redisKey, $raw): void
720+
/**
721+
* @param string $redisKey
722+
* @param string|null $metricName
723+
* @return void
724+
* @throws MetricJsonException
725+
*/
726+
private function throwMetricJsonException(string $redisKey, ?string $metricName = null): void
721727
{
722-
$metaData = is_array($raw) && isset($raw['_meta']) ? $raw['_meta'] : 'undefined';
723-
$message = 'Json error: ' . json_last_error_msg() . ' redis key : ' . $redisKey . ' raw meta data: ' . $metaData;
724-
throw new MetricJsonException($message, 0, null, $metaData);
728+
$metricName = $metricName ?? 'unknown';
729+
$message = 'Json error: ' . json_last_error_msg() . ' redis key : ' . $redisKey . ' metric name: ' . $metricName;
730+
throw new MetricJsonException($message, 0, null, $metricName);
725731
}
726732
}

0 commit comments

Comments
 (0)