66
77use InvalidArgumentException ;
88use Prometheus \Storage \Adapter ;
9+ use Prometheus \Storage \APC ;
910
1011abstract 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 . "' " );
0 commit comments