6
6
7
7
use InvalidArgumentException ;
8
8
use Prometheus \Storage \Adapter ;
9
+ use Prometheus \Storage \APC ;
9
10
10
11
abstract class Collector
11
12
{
12
13
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_]*$/ ' ;
13
21
14
22
/**
15
23
* @var Adapter
@@ -42,11 +50,12 @@ public function __construct(Adapter $storageAdapter, string $namespace, string $
42
50
{
43
51
$ this ->storageAdapter = $ storageAdapter ;
44
52
$ 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 );
46
55
$ this ->name = $ metricName ;
47
56
$ this ->help = $ help ;
48
57
foreach ($ labels as $ label ) {
49
- self ::assertValidLabel ($ label );
58
+ self ::assertValidLabel ($ label, $ regexp );
50
59
}
51
60
$ this ->labels = $ labels ;
52
61
}
@@ -101,7 +110,7 @@ protected function assertLabelsAreDefinedCorrectly(array $labels): void
101
110
/**
102
111
* @param string $metricName
103
112
*/
104
- public static function assertValidMetricName (string $ metricName ): void
113
+ public static function assertValidMetricName (string $ metricName, string $ regExp ): void
105
114
{
106
115
if (preg_match (self ::RE_METRIC_LABEL_NAME , $ metricName ) !== 1 ) {
107
116
throw new InvalidArgumentException ("Invalid metric name: ' " . $ metricName . "' " );
@@ -111,7 +120,7 @@ public static function assertValidMetricName(string $metricName): void
111
120
/**
112
121
* @param string $label
113
122
*/
114
- public static function assertValidLabel (string $ label ): void
123
+ public static function assertValidLabel (string $ label, string $ regExp ): void
115
124
{
116
125
if (preg_match (self ::RE_METRIC_LABEL_NAME , $ label ) !== 1 ) {
117
126
throw new InvalidArgumentException ("Invalid label name: ' " . $ label . "' " );
0 commit comments