From ed9b1fd311be802f7aaa008e89fcef4609c8f728 Mon Sep 17 00:00:00 2001 From: "v.sidorin" Date: Mon, 17 May 2021 19:26:59 +0300 Subject: [PATCH 1/2] validate names for apcu Signed-off-by: v.sidorin --- src/Prometheus/Collector.php | 17 +++++++++++++---- src/Prometheus/Histogram.php | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Prometheus/Collector.php b/src/Prometheus/Collector.php index cb17656d..fd2770e3 100644 --- a/src/Prometheus/Collector.php +++ b/src/Prometheus/Collector.php @@ -6,10 +6,18 @@ use InvalidArgumentException; use Prometheus\Storage\Adapter; +use Prometheus\Storage\APC; abstract class Collector { const RE_METRIC_LABEL_NAME = '/^[a-zA-Z_:][a-zA-Z0-9_:]*$/'; + /** + * Colons cannot be used in names - all meta-information will be invalid + * @see src/Prometheus/Storage/APC.php:169 + * @see src/Prometheus/Storage/APC.php:178 + * @see src/Prometheus/Storage/APC.php:194 + */ + const RE_METRIC_LABEL_NAME_APCU = '/^[a-zA-Z_][a-zA-Z0-9_]*$/'; /** * @var Adapter @@ -42,11 +50,12 @@ public function __construct(Adapter $storageAdapter, string $namespace, string $ { $this->storageAdapter = $storageAdapter; $metricName = ($namespace !== '' ? $namespace . '_' : '') . $name; - self::assertValidMetricName($metricName); + $regexp = ($this->storageAdapter instanceof APC) ? self::RE_METRIC_LABEL_NAME_APCU : self::RE_METRIC_LABEL_NAME; + self::assertValidMetricName($metricName, $regexp); $this->name = $metricName; $this->help = $help; foreach ($labels as $label) { - self::assertValidLabel($label); + self::assertValidLabel($label, $regexp); } $this->labels = $labels; } @@ -101,7 +110,7 @@ protected function assertLabelsAreDefinedCorrectly(array $labels): void /** * @param string $metricName */ - public static function assertValidMetricName(string $metricName): void + public static function assertValidMetricName(string $metricName, string $regExp): void { if (preg_match(self::RE_METRIC_LABEL_NAME, $metricName) !== 1) { throw new InvalidArgumentException("Invalid metric name: '" . $metricName . "'"); @@ -111,7 +120,7 @@ public static function assertValidMetricName(string $metricName): void /** * @param string $label */ - public static function assertValidLabel(string $label): void + public static function assertValidLabel(string $label, string $regExp): void { if (preg_match(self::RE_METRIC_LABEL_NAME, $label) !== 1) { throw new InvalidArgumentException("Invalid label name: '" . $label . "'"); diff --git a/src/Prometheus/Histogram.php b/src/Prometheus/Histogram.php index 0e8fc926..5203d7f4 100644 --- a/src/Prometheus/Histogram.php +++ b/src/Prometheus/Histogram.php @@ -6,6 +6,7 @@ use InvalidArgumentException; use Prometheus\Storage\Adapter; +use Prometheus\Storage\APC; class Histogram extends Collector { From 5231c1895f1c66631a58405cfd3ffd6abb07367d Mon Sep 17 00:00:00 2001 From: "v.sidorin" Date: Mon, 17 May 2021 19:30:33 +0300 Subject: [PATCH 2/2] validate names for apcu Signed-off-by: v.sidorin --- src/Prometheus/Histogram.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Prometheus/Histogram.php b/src/Prometheus/Histogram.php index 5203d7f4..0e8fc926 100644 --- a/src/Prometheus/Histogram.php +++ b/src/Prometheus/Histogram.php @@ -6,7 +6,6 @@ use InvalidArgumentException; use Prometheus\Storage\Adapter; -use Prometheus\Storage\APC; class Histogram extends Collector {