Skip to content

Commit

Permalink
was added a custom json exception for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Vsevolod committed Jan 27, 2025
1 parent cbbf444 commit ea6aeb7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
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 $metricMetaData;

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::$metricMetaData 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::$metricMetaData 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::$metricMetaData has no type specified.
public function __construct($message = "", $code = 0, Exception $previous = null, string $metricMetaData)

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

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

Deprecated in PHP 8.0: Required parameter $metricMetaData follows optional parameter $code.

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.3

Deprecated in PHP 8.0: Required parameter $metricMetaData follows optional parameter $code.

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.

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

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

Deprecated in PHP 8.0: Required parameter $metricMetaData follows optional parameter $code.

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.
{
parent::__construct($message, $code, $previous);
$this->metricMetaData = $metricMetaData;
}

public function getMetricMetaData(): string
{
return $this->metricMetaData;
}
}
11 changes: 6 additions & 5 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 @@ -433,7 +434,7 @@ private function collectHistograms(): array
$allLabelValues[] = $d['labelValues'];
}
if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwStorageExceptionOnJsonError($key, $raw);
$this->throwMetricJsonException($key, $raw);
}

// We need set semantics.
Expand Down Expand Up @@ -624,7 +625,7 @@ private function collectGauges(bool $sortMetrics = true): array
}

if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwStorageExceptionOnJsonError($key, $raw);
$this->throwMetricJsonException($key, $raw);
}
if ($sortMetrics) {
usort($gauge['samples'], function ($a, $b): int {
Expand Down Expand Up @@ -663,7 +664,7 @@ private function collectCounters(bool $sortMetrics = true): array
}

if (json_last_error() !== JSON_ERROR_NONE) {
$this->throwStorageExceptionOnJsonError($key, $raw);
$this->throwMetricJsonException($key, $raw);
}
if ($sortMetrics) {
usort($counter['samples'], function ($a, $b): int {
Expand Down Expand Up @@ -735,10 +736,10 @@ private function decodeLabelValues(string $values): array
return $decodedValues;
}

private function throwStorageExceptionOnJsonError(string $redisKey, $raw): void
private function throwMetricJsonException(string $redisKey, $raw): void

Check failure on line 739 in src/Prometheus/Storage/Redis.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

Method Prometheus\Storage\Redis::throwMetricJsonException() has parameter $raw with no type specified.

Check failure on line 739 in src/Prometheus/Storage/Redis.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.3

Method Prometheus\Storage\Redis::throwMetricJsonException() has parameter $raw with no type specified.

Check failure on line 739 in src/Prometheus/Storage/Redis.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

Method Prometheus\Storage\Redis::throwMetricJsonException() has parameter $raw with no type specified.
{
$metaData = is_array($raw) && isset($raw['_meta']) ? $raw['_meta'] : 'undefined';
$message = 'Json error: ' . json_last_error_msg() . ' redis key : ' . $redisKey . ' raw meta data: ' . $metaData;
throw new StorageException($message, 0);
throw new MetricJsonException($message, 0, null, $metaData);
}
}
11 changes: 6 additions & 5 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 @@ -430,7 +431,7 @@ private function collectHistograms(): array
}

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

// We need set semantics.
Expand Down Expand Up @@ -606,7 +607,7 @@ private function collectGauges(bool $sortMetrics = true): array
}

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

if ($sortMetrics) {
Expand Down Expand Up @@ -643,7 +644,7 @@ private function collectCounters(bool $sortMetrics = true): array
}

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

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

private function throwStorageExceptionOnJsonError(string $redisKey, $raw): void
private function throwMetricJsonException(string $redisKey, $raw): void

Check failure on line 720 in src/Prometheus/Storage/RedisNg.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.1

Method Prometheus\Storage\RedisNg::throwMetricJsonException() has parameter $raw with no type specified.

Check failure on line 720 in src/Prometheus/Storage/RedisNg.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.3

Method Prometheus\Storage\RedisNg::throwMetricJsonException() has parameter $raw with no type specified.

Check failure on line 720 in src/Prometheus/Storage/RedisNg.php

View workflow job for this annotation

GitHub Actions / PHPStan on PHP 8.2

Method Prometheus\Storage\RedisNg::throwMetricJsonException() has parameter $raw with no type specified.
{
$metaData = is_array($raw) && isset($raw['_meta']) ? $raw['_meta'] : 'undefined';
$message = 'Json error: ' . json_last_error_msg() . ' redis key : ' . $redisKey . ' raw meta data: ' . $metaData;
throw new StorageException($message, 0);
throw new MetricJsonException($message, 0, null, $metaData);
}
}

0 comments on commit ea6aeb7

Please sign in to comment.