Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 4 additions & 16 deletions src/Prometheus/Storage/AbstractRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,8 @@ public function updateGauge(array $data): void
$this->redis->eval(
<<<'LUA'
local result = redis.call(ARGV[1], KEYS[1], ARGV[2], ARGV[3])

if ARGV[1] == 'hSet' then
if result == 1 then
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
else
if result == ARGV[3] then
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
end
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
LUA
,
[
Expand Down Expand Up @@ -256,10 +246,8 @@ public function updateCounter(array $data): void
$this->redis->eval(
<<<'LUA'
local result = redis.call(ARGV[1], KEYS[1], ARGV[3], ARGV[2])
local added = redis.call('sAdd', KEYS[2], KEYS[1])
if added == 1 then
redis.call('hMSet', KEYS[1], '__meta', ARGV[4])
end
redis.call('sAdd', KEYS[2], KEYS[1])
redis.call('hMSet', KEYS[1], '__meta', ARGV[4])
return result
LUA
,
Expand Down
20 changes: 4 additions & 16 deletions src/Prometheus/Storage/RedisNg.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,8 @@ public function updateGauge(array $data): void
$this->redis->eval(
<<<LUA
local result = redis.call(ARGV[1], KEYS[1], ARGV[2], ARGV[3])

if ARGV[1] == 'hSet' then
if result == 1 then
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
else
if result == ARGV[3] then
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
end
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
LUA
,
[
Expand All @@ -373,10 +363,8 @@ public function updateCounter(array $data): void
$this->redis->eval(
<<<LUA
local result = redis.call(ARGV[1], KEYS[1], ARGV[3], ARGV[2])
local added = redis.call('sAdd', KEYS[2], KEYS[1])
if added == 1 then
redis.call('hMSet', KEYS[1], '__meta', ARGV[4])
end
redis.call('sAdd', KEYS[2], KEYS[1])
redis.call('hMSet', KEYS[1], '__meta', ARGV[4])
return result
LUA
,
Expand Down
26 changes: 26 additions & 0 deletions tests/Test/Prometheus/AbstractCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Prometheus\CollectorRegistry;
use Prometheus\Counter;
use Prometheus\MetricFamilySamples;
use Prometheus\Sample;
Expand Down Expand Up @@ -223,4 +224,29 @@ public function labelValuesDataProvider(): array
}
return $cases;
}

/**
* @test
*/
public function itShouldUpdateMetadataWhenLabelSchemaChanges(): void
{
$registry = new CollectorRegistry($this->adapter, false);

$counterV1 = $registry->getOrRegisterCounter('test', 'some_metric', 'help', ['foo']);
$counterV1->inc(['val1']);

$metrics = $this->adapter->collect();
self::assertCount(1, $metrics);
self::assertSame(['foo'], $metrics[0]->getLabelNames());

$this->adapter->wipeStorage();
$registry = new CollectorRegistry($this->adapter, false);

$counterV2 = $registry->getOrRegisterCounter('test', 'some_metric', 'help', ['foo', 'bar']);
$counterV2->inc(['val1', 'val2']);

$metrics = $this->adapter->collect();
self::assertCount(1, $metrics);
self::assertSame(['foo', 'bar'], $metrics[0]->getLabelNames());
}
}
26 changes: 26 additions & 0 deletions tests/Test/Prometheus/AbstractGaugeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Prometheus\CollectorRegistry;
use Prometheus\Gauge;
use Prometheus\MetricFamilySamples;
use Prometheus\Sample;
Expand Down Expand Up @@ -357,4 +358,29 @@ public function labelValuesDataProvider(): array
}
return $cases;
}

/**
* @test
*/
public function itShouldUpdateMetadataWhenLabelSchemaChanges(): void
{
$registry = new CollectorRegistry($this->adapter, false);

$gaugeV1 = $registry->getOrRegisterGauge('test', 'some_metric', 'help', ['foo']);
$gaugeV1->set(42, ['val1']);

$metrics = $this->adapter->collect();
self::assertCount(1, $metrics);
self::assertSame(['foo'], $metrics[0]->getLabelNames());

$this->adapter->wipeStorage();
$registry = new CollectorRegistry($this->adapter, false);

$gaugeV2 = $registry->getOrRegisterGauge('test', 'some_metric', 'help', ['foo', 'bar']);
$gaugeV2->set(99, ['val1', 'val2']);

$metrics = $this->adapter->collect();
self::assertCount(1, $metrics);
self::assertSame(['foo', 'bar'], $metrics[0]->getLabelNames());
}
}
23 changes: 23 additions & 0 deletions tests/Test/Prometheus/Redis/CounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Test\Prometheus\Redis;

use Prometheus\CollectorRegistry;
use Prometheus\Storage\Redis;
use Test\Prometheus\AbstractCounterTest;

Expand All @@ -18,4 +19,26 @@ public function configureAdapter(): void
$this->adapter = new Redis(['host' => REDIS_HOST]);
$this->adapter->wipeStorage();
}

/**
* @test
*/
public function itShouldUpdateMetadataOnSubsequentWritesWithoutWipe(): void
{
$registry = new CollectorRegistry($this->adapter, false);

$counterV1 = $registry->getOrRegisterCounter('test', 'some_metric', 'help', ['foo']);
$counterV1->inc(['val1']);

$metrics = $this->adapter->collect();
self::assertSame(['foo'], $metrics[0]->getLabelNames());

$registry = new CollectorRegistry($this->adapter, false);

$counterV2 = $registry->getOrRegisterCounter('test', 'some_metric', 'help', ['foo', 'bar']);
$counterV2->inc(['val1', 'val2']);

$metrics = $this->adapter->collect();
self::assertSame(['foo', 'bar'], $metrics[0]->getLabelNames());
}
}
23 changes: 23 additions & 0 deletions tests/Test/Prometheus/Redis/GaugeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Test\Prometheus\Redis;

use Prometheus\CollectorRegistry;
use Prometheus\Storage\Redis;
use Test\Prometheus\AbstractGaugeTest;

Expand All @@ -18,4 +19,26 @@ public function configureAdapter(): void
$this->adapter = new Redis(['host' => REDIS_HOST]);
$this->adapter->wipeStorage();
}

/**
* @test
*/
public function itShouldUpdateMetadataOnSubsequentWritesWithoutWipe(): void
{
$registry = new CollectorRegistry($this->adapter, false);

$gaugeV1 = $registry->getOrRegisterGauge('test', 'some_metric', 'help', ['foo']);
$gaugeV1->set(42, ['val1']);

$metrics = $this->adapter->collect();
self::assertSame(['foo'], $metrics[0]->getLabelNames());

$registry = new CollectorRegistry($this->adapter, false);

$gaugeV2 = $registry->getOrRegisterGauge('test', 'some_metric', 'help', ['foo', 'bar']);
$gaugeV2->set(99, ['val1', 'val2']);

$metrics = $this->adapter->collect();
self::assertSame(['foo', 'bar'], $metrics[0]->getLabelNames());
}
}
23 changes: 23 additions & 0 deletions tests/Test/Prometheus/RedisNg/CounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Test\Prometheus\RedisNg;

use Prometheus\CollectorRegistry;
use Prometheus\Storage\RedisNg;
use Test\Prometheus\AbstractCounterTest;

Expand All @@ -18,4 +19,26 @@ public function configureAdapter(): void
$this->adapter = new RedisNg(['host' => REDIS_HOST]);
$this->adapter->wipeStorage();
}

/**
* @test
*/
public function itShouldUpdateMetadataOnSubsequentWritesWithoutWipe(): void
{
$registry = new CollectorRegistry($this->adapter, false);

$counterV1 = $registry->getOrRegisterCounter('test', 'some_metric', 'help', ['foo']);
$counterV1->inc(['val1']);

$metrics = $this->adapter->collect();
self::assertSame(['foo'], $metrics[0]->getLabelNames());

$registry = new CollectorRegistry($this->adapter, false);

$counterV2 = $registry->getOrRegisterCounter('test', 'some_metric', 'help', ['foo', 'bar']);
$counterV2->inc(['val1', 'val2']);

$metrics = $this->adapter->collect();
self::assertSame(['foo', 'bar'], $metrics[0]->getLabelNames());
}
}
24 changes: 23 additions & 1 deletion tests/Test/Prometheus/RedisNg/GaugeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Test\Prometheus\RedisNg;

use Prometheus\Storage\Redis;
use Prometheus\CollectorRegistry;
use Prometheus\Storage\RedisNg;
use Test\Prometheus\AbstractGaugeTest;

Expand All @@ -19,4 +19,26 @@ public function configureAdapter(): void
$this->adapter = new RedisNg(['host' => REDIS_HOST]);
$this->adapter->wipeStorage();
}

/**
* @test
*/
public function itShouldUpdateMetadataOnSubsequentWritesWithoutWipe(): void
{
$registry = new CollectorRegistry($this->adapter, false);

$gaugeV1 = $registry->getOrRegisterGauge('test', 'some_metric', 'help', ['foo']);
$gaugeV1->set(42, ['val1']);

$metrics = $this->adapter->collect();
self::assertSame(['foo'], $metrics[0]->getLabelNames());

$registry = new CollectorRegistry($this->adapter, false);

$gaugeV2 = $registry->getOrRegisterGauge('test', 'some_metric', 'help', ['foo', 'bar']);
$gaugeV2->set(99, ['val1', 'val2']);

$metrics = $this->adapter->collect();
self::assertSame(['foo', 'bar'], $metrics[0]->getLabelNames());
}
}
8 changes: 3 additions & 5 deletions tests/Test/Prometheus/RenderTextFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public function testValueErrorThrownWithInvalidSamples(): void
$registry->registerCounter($namespace, $counter, 'counter-help-text', ['label1', 'label2'])
->inc(['bob', 'alice']);

// Reload the registry with an updated counter config
$registry = new CollectorRegistry($storage, false);
$registry->registerCounter($namespace, $counter, 'counter-help-text', ['label1', 'label2', 'label3'])
->inc(['bob', 'alice', 'eve']);
Expand All @@ -116,18 +115,17 @@ public function testOutputWithInvalidSamplesSkipped(): void
$registry->registerCounter($namespace, $counter, 'counter-help-text', ['label1', 'label2'])
->inc(['bob', 'alice']);

// Reload the registry with an updated counter config
$registry = new CollectorRegistry($storage, false);
$registry->registerCounter($namespace, $counter, 'counter-help-text', ['label1', 'label2', 'label3'])
->inc(['bob', 'alice', 'eve']);

$expectedOutput = '
# HELP foo_bar counter-help-text
# TYPE foo_bar counter
foo_bar{label1="bob",label2="alice"} 1
# Error: array_combine(): Argument #1 ($keys) and argument #2 ($values) must have the same number of elements
# Labels: ["label1","label2"]
# Values: ["bob","alice","eve"]
# Labels: ["label1","label2","label3"]
# Values: ["bob","alice"]
foo_bar{label1="bob",label2="alice",label3="eve"} 1
';

$renderer = new RenderTextFormat();
Expand Down
Loading