Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch invalid JSON from Redis and throw an error #174

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Prometheus/Exception/MetricJsonException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Prometheus\Exception;

use Exception;

/**
* Exception thrown if a metric can't be found in the CollectorRegistry.
*/
class MetricJsonException extends Exception
{

private $metricName;

Check failure on line 13 in src/Prometheus/Exception/MetricJsonException.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

Property Prometheus\Exception\MetricJsonException::$metricName has no type specified.

Check failure on line 13 in src/Prometheus/Exception/MetricJsonException.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

Property Prometheus\Exception\MetricJsonException::$metricName has no type specified.

Check failure on line 13 in src/Prometheus/Exception/MetricJsonException.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.3

Property Prometheus\Exception\MetricJsonException::$metricName has no type specified.
public function __construct($message = "", $code = 0, Exception $previous = null, ?string $metricName = null)

Check failure on line 14 in src/Prometheus/Exception/MetricJsonException.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

Method Prometheus\Exception\MetricJsonException::__construct() has parameter $code with no type specified.

Check failure on line 14 in src/Prometheus/Exception/MetricJsonException.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

Method Prometheus\Exception\MetricJsonException::__construct() has parameter $message with no type specified.

Check failure on line 14 in src/Prometheus/Exception/MetricJsonException.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

Method Prometheus\Exception\MetricJsonException::__construct() has parameter $code with no type specified.

Check failure on line 14 in src/Prometheus/Exception/MetricJsonException.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

Method Prometheus\Exception\MetricJsonException::__construct() has parameter $message with no type specified.

Check failure on line 14 in src/Prometheus/Exception/MetricJsonException.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.3

Method Prometheus\Exception\MetricJsonException::__construct() has parameter $code with no type specified.

Check failure on line 14 in src/Prometheus/Exception/MetricJsonException.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.3

Method Prometheus\Exception\MetricJsonException::__construct() has parameter $message with no type specified.
{
parent::__construct($message, $code, $previous);
$this->metricName = $metricName;
}

public function getMetricName(): ?string
{
return $this->metricName;
}
}
26 changes: 26 additions & 0 deletions src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use Prometheus\Counter;
use Prometheus\Exception\MetricJsonException;
use Prometheus\Exception\StorageException;
use Prometheus\Gauge;
use Prometheus\Histogram;
Expand Down Expand Up @@ -432,6 +433,9 @@ private function collectHistograms(): array
}
$allLabelValues[] = $d['labelValues'];
}
if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwMetricJsonException($key);
}

// We need set semantics.
// This is the equivalent of array_unique but for arrays of arrays.
Expand Down Expand Up @@ -618,6 +622,9 @@ private function collectGauges(bool $sortMetrics = true): array
'labelValues' => json_decode($k, true),
'value' => $value,
];
if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwMetricJsonException($key, $gauge['name']);
}
}

if ($sortMetrics) {
Expand All @@ -633,6 +640,7 @@ private function collectGauges(bool $sortMetrics = true): array

/**
* @return mixed[]
* @throws MetricJsonException
*/
private function collectCounters(bool $sortMetrics = true): array
{
Expand All @@ -645,6 +653,7 @@ private function collectCounters(bool $sortMetrics = true): array
continue;
}
$counter = json_decode($raw['__meta'], true);

unset($raw['__meta']);
$counter['samples'] = [];
foreach ($raw as $k => $value) {
Expand All @@ -654,6 +663,10 @@ private function collectCounters(bool $sortMetrics = true): array
'labelValues' => json_decode($k, true),
'value' => $value,
];

if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwMetricJsonException($key, $counter['name']);
}
}

if ($sortMetrics) {
Expand Down Expand Up @@ -725,4 +738,17 @@ private function decodeLabelValues(string $values): array
}
return $decodedValues;
}

/**
* @param string $redisKey
* @param string|null $metricName
* @return void
* @throws MetricJsonException
*/
private function throwMetricJsonException(string $redisKey, ?string $metricName = null): void
{
$metricName = $metricName ?? 'unknown';
$message = 'Json error: ' . json_last_error_msg() . ' redis key : ' . $redisKey . ' metric name: ' . $metricName;
throw new MetricJsonException($message, 0, null, $metricName);
}
}
26 changes: 26 additions & 0 deletions src/Prometheus/Storage/RedisNg.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use Prometheus\Counter;
use Prometheus\Exception\MetricJsonException;
use Prometheus\Exception\StorageException;
use Prometheus\Gauge;
use Prometheus\Histogram;
Expand Down Expand Up @@ -405,6 +406,7 @@ private function metaData(array $data): array

/**
* @return mixed[]
* @throws MetricJsonException
*/
private function collectHistograms(): array
{
Expand All @@ -429,6 +431,10 @@ private function collectHistograms(): array
$allLabelValues[] = $d['labelValues'];
}

if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwMetricJsonException($key);
}

// We need set semantics.
// This is the equivalent of array_unique but for arrays of arrays.
$allLabelValues = array_map("unserialize", array_unique(array_map("serialize", $allLabelValues)));
Expand Down Expand Up @@ -599,6 +605,9 @@ private function collectGauges(bool $sortMetrics = true): array
'labelValues' => json_decode($k, true),
'value' => $value,
];
if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwMetricJsonException($key, $gauge['name']);
}
}

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

/**
* @return mixed[]
* @throws MetricJsonException
*/
private function collectCounters(bool $sortMetrics = true): array
{
Expand All @@ -632,6 +642,9 @@ private function collectCounters(bool $sortMetrics = true): array
'labelValues' => json_decode($k, true),
'value' => $value,
];
if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwMetricJsonException($key, $counter['name']);
}
}

if ($sortMetrics) {
Expand Down Expand Up @@ -703,4 +716,17 @@ private function decodeLabelValues(string $values): array
}
return $decodedValues;
}

/**
* @param string $redisKey
* @param string|null $metricName
* @return void
* @throws MetricJsonException
*/
private function throwMetricJsonException(string $redisKey, ?string $metricName = null): void
{
$metricName = $metricName ?? 'unknown';
$message = 'Json error: ' . json_last_error_msg() . ' redis key : ' . $redisKey . ' metric name: ' . $metricName;
throw new MetricJsonException($message, 0, null, $metricName);
}
}
Loading