Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validare names for apcu #59

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/Prometheus/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 . "'");
Expand All @@ -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 . "'");
Expand Down