Skip to content

Commit

Permalink
Enable usage of metrics with same name but different labels
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Rota <rota.giuseppe@gmail.com>
  • Loading branch information
grota committed Jul 6, 2021
1 parent bca39eb commit a82fe69
Show file tree
Hide file tree
Showing 14 changed files with 403 additions and 246 deletions.
46 changes: 25 additions & 21 deletions src/Prometheus/CollectorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public function getMetricFamilySamples(): array
* @return Gauge
* @throws MetricsRegistrationException
*/
public function registerGauge(string $namespace, string $name, string $help, $labels = []): Gauge
public function registerGauge(string $namespace, string $name, string $help, array $labels = []): Gauge
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
$metricIdentifier = self::metricIdentifier($namespace, $name, $labels);
if (isset($this->gauges[$metricIdentifier])) {
throw new MetricsRegistrationException("Metric already registered");
}
Expand All @@ -99,13 +99,14 @@ public function registerGauge(string $namespace, string $name, string $help, $la
/**
* @param string $namespace
* @param string $name
* @param string[] $labels
*
* @return Gauge
* @throws MetricNotFoundException
*/
public function getGauge(string $namespace, string $name): Gauge
public function getGauge(string $namespace, string $name, array $labels = []): Gauge
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
$metricIdentifier = self::metricIdentifier($namespace, $name, $labels);
if (!isset($this->gauges[$metricIdentifier])) {
throw new MetricNotFoundException("Metric not found:" . $metricIdentifier);
}
Expand All @@ -121,10 +122,10 @@ public function getGauge(string $namespace, string $name): Gauge
* @return Gauge
* @throws MetricsRegistrationException
*/
public function getOrRegisterGauge(string $namespace, string $name, string $help, $labels = []): Gauge
public function getOrRegisterGauge(string $namespace, string $name, string $help, array $labels = []): Gauge
{
try {
$gauge = $this->getGauge($namespace, $name);
$gauge = $this->getGauge($namespace, $name, $labels);
} catch (MetricNotFoundException $e) {
$gauge = $this->registerGauge($namespace, $name, $help, $labels);
}
Expand All @@ -140,9 +141,9 @@ public function getOrRegisterGauge(string $namespace, string $name, string $help
* @return Counter
* @throws MetricsRegistrationException
*/
public function registerCounter(string $namespace, string $name, string $help, $labels = []): Counter
public function registerCounter(string $namespace, string $name, string $help, array $labels = []): Counter
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
$metricIdentifier = self::metricIdentifier($namespace, $name, $labels);
if (isset($this->counters[$metricIdentifier])) {
throw new MetricsRegistrationException("Metric already registered");
}
Expand All @@ -153,23 +154,24 @@ public function registerCounter(string $namespace, string $name, string $help, $
$help,
$labels
);
return $this->counters[self::metricIdentifier($namespace, $name)];
return $this->counters[$metricIdentifier];
}

/**
* @param string $namespace
* @param string $name
* @param string[] $labels
*
* @return Counter
* @throws MetricNotFoundException
*/
public function getCounter(string $namespace, string $name): Counter
public function getCounter(string $namespace, string $name, array $labels = []): Counter
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
$metricIdentifier = self::metricIdentifier($namespace, $name, $labels);
if (!isset($this->counters[$metricIdentifier])) {
throw new MetricNotFoundException("Metric not found:" . $metricIdentifier);
}
return $this->counters[self::metricIdentifier($namespace, $name)];
return $this->counters[$metricIdentifier];
}

/**
Expand All @@ -181,10 +183,10 @@ public function getCounter(string $namespace, string $name): Counter
* @return Counter
* @throws MetricsRegistrationException
*/
public function getOrRegisterCounter(string $namespace, string $name, string $help, $labels = []): Counter
public function getOrRegisterCounter(string $namespace, string $name, string $help, array $labels = []): Counter
{
try {
$counter = $this->getCounter($namespace, $name);
$counter = $this->getCounter($namespace, $name, $labels);
} catch (MetricNotFoundException $e) {
$counter = $this->registerCounter($namespace, $name, $help, $labels);
}
Expand All @@ -208,7 +210,7 @@ public function registerHistogram(
array $labels = [],
array $buckets = null
): Histogram {
$metricIdentifier = self::metricIdentifier($namespace, $name);
$metricIdentifier = self::metricIdentifier($namespace, $name, $labels);
if (isset($this->histograms[$metricIdentifier])) {
throw new MetricsRegistrationException("Metric already registered");
}
Expand All @@ -226,17 +228,18 @@ public function registerHistogram(
/**
* @param string $namespace
* @param string $name
* @param string[] $labels
*
* @return Histogram
* @throws MetricNotFoundException
*/
public function getHistogram(string $namespace, string $name): Histogram
public function getHistogram(string $namespace, string $name, array $labels = []): Histogram
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
$metricIdentifier = self::metricIdentifier($namespace, $name, $labels);
if (!isset($this->histograms[$metricIdentifier])) {
throw new MetricNotFoundException("Metric not found:" . $metricIdentifier);
}
return $this->histograms[self::metricIdentifier($namespace, $name)];
return $this->histograms[$metricIdentifier];
}

/**
Expand All @@ -257,7 +260,7 @@ public function getOrRegisterHistogram(
array $buckets = null
): Histogram {
try {
$histogram = $this->getHistogram($namespace, $name);
$histogram = $this->getHistogram($namespace, $name, $labels);
} catch (MetricNotFoundException $e) {
$histogram = $this->registerHistogram($namespace, $name, $help, $labels, $buckets);
}
Expand All @@ -267,12 +270,13 @@ public function getOrRegisterHistogram(
/**
* @param string $namespace
* @param string $name
* @param string[] $labels
*
* @return string
*/
private static function metricIdentifier(string $namespace, string $name): string
private static function metricIdentifier(string $namespace, string $name, array $labels = []): string
{
return $namespace . ":" . $name;
return $namespace . ":" . $name . ':' . implode('|', $labels);
}

private function registerDefaultMetrics(): void
Expand Down
17 changes: 0 additions & 17 deletions src/Prometheus/MetricFamilySamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function __construct(array $data)
$this->name = $data['name'];
$this->type = $data['type'];
$this->help = $data['help'];
$this->labelNames = $data['labelNames'];
foreach ($data['samples'] as $sampleData) {
$this->samples[] = new Sample($sampleData);
}
Expand Down Expand Up @@ -76,20 +75,4 @@ public function getSamples(): array
{
return $this->samples;
}

/**
* @return string[]
*/
public function getLabelNames(): array
{
return $this->labelNames;
}

/**
* @return bool
*/
public function hasLabelNames(): bool
{
return $this->labelNames !== [];
}
}
10 changes: 4 additions & 6 deletions src/Prometheus/RenderTextFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public function render(array $metrics): string
*/
private function renderSample(MetricFamilySamples $metric, Sample $sample): string
{
$labelNames = $metric->getLabelNames();
if ($metric->hasLabelNames() || $sample->hasLabelNames()) {
$escapedLabels = $this->escapeAllLabels($labelNames, $sample);
if ($sample->hasLabelNames()) {
$escapedLabels = $this->escapeAllLabels($sample);
return $sample->getName() . '{' . implode(',', $escapedLabels) . '} ' . $sample->getValue();
}
return $sample->getName() . ' ' . $sample->getValue();
Expand All @@ -54,16 +53,15 @@ private function escapeLabelValue(string $v): string
}

/**
* @param string[] $labelNames
* @param Sample $sample
*
* @return string[]
*/
private function escapeAllLabels(array $labelNames, Sample $sample): array
private function escapeAllLabels(Sample $sample): array
{
$escapedLabels = [];

$labels = array_combine(array_merge($labelNames, $sample->getLabelNames()), $sample->getLabelValues());
$labels = array_combine($sample->getLabelNames(), $sample->getLabelValues());

if ($labels === false) {
return [];
Expand Down

0 comments on commit a82fe69

Please sign in to comment.