Skip to content

Commit ed9b1fd

Browse files
author
v.sidorin
committed
validate names for apcu
Signed-off-by: v.sidorin <[email protected]>
1 parent bca39eb commit ed9b1fd

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Prometheus/Collector.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66

77
use InvalidArgumentException;
88
use Prometheus\Storage\Adapter;
9+
use Prometheus\Storage\APC;
910

1011
abstract class Collector
1112
{
1213
const RE_METRIC_LABEL_NAME = '/^[a-zA-Z_:][a-zA-Z0-9_:]*$/';
14+
/**
15+
* Colons cannot be used in names - all meta-information will be invalid
16+
* @see src/Prometheus/Storage/APC.php:169
17+
* @see src/Prometheus/Storage/APC.php:178
18+
* @see src/Prometheus/Storage/APC.php:194
19+
*/
20+
const RE_METRIC_LABEL_NAME_APCU = '/^[a-zA-Z_][a-zA-Z0-9_]*$/';
1321

1422
/**
1523
* @var Adapter
@@ -42,11 +50,12 @@ public function __construct(Adapter $storageAdapter, string $namespace, string $
4250
{
4351
$this->storageAdapter = $storageAdapter;
4452
$metricName = ($namespace !== '' ? $namespace . '_' : '') . $name;
45-
self::assertValidMetricName($metricName);
53+
$regexp = ($this->storageAdapter instanceof APC) ? self::RE_METRIC_LABEL_NAME_APCU : self::RE_METRIC_LABEL_NAME;
54+
self::assertValidMetricName($metricName, $regexp);
4655
$this->name = $metricName;
4756
$this->help = $help;
4857
foreach ($labels as $label) {
49-
self::assertValidLabel($label);
58+
self::assertValidLabel($label, $regexp);
5059
}
5160
$this->labels = $labels;
5261
}
@@ -101,7 +110,7 @@ protected function assertLabelsAreDefinedCorrectly(array $labels): void
101110
/**
102111
* @param string $metricName
103112
*/
104-
public static function assertValidMetricName(string $metricName): void
113+
public static function assertValidMetricName(string $metricName, string $regExp): void
105114
{
106115
if (preg_match(self::RE_METRIC_LABEL_NAME, $metricName) !== 1) {
107116
throw new InvalidArgumentException("Invalid metric name: '" . $metricName . "'");
@@ -111,7 +120,7 @@ public static function assertValidMetricName(string $metricName): void
111120
/**
112121
* @param string $label
113122
*/
114-
public static function assertValidLabel(string $label): void
123+
public static function assertValidLabel(string $label, string $regExp): void
115124
{
116125
if (preg_match(self::RE_METRIC_LABEL_NAME, $label) !== 1) {
117126
throw new InvalidArgumentException("Invalid label name: '" . $label . "'");

src/Prometheus/Histogram.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use InvalidArgumentException;
88
use Prometheus\Storage\Adapter;
9+
use Prometheus\Storage\APC;
910

1011
class Histogram extends Collector
1112
{

0 commit comments

Comments
 (0)