Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Fix errant keys in Redis scripts #111

Open
wants to merge 1 commit into
base: master
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
44 changes: 22 additions & 22 deletions src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,23 @@ public function updateHistogram(array $data)
unset($metaData['value']);
unset($metaData['labelValues']);
$this->redis->eval(<<<LUA
local increment = redis.call('hIncrByFloat', KEYS[1], KEYS[2], ARGV[1])
redis.call('hIncrBy', KEYS[1], KEYS[3], 1)
if increment == ARGV[1] then
redis.call('hSet', KEYS[1], '__meta', ARGV[2])
redis.call('sAdd', KEYS[4], KEYS[1])
local increment = redis.call('hIncrByFloat', KEYS[1], ARGV[1], ARGV[3])
redis.call('hIncrBy', KEYS[1], ARGV[2], 1)
if increment == ARGV[3] then
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
LUA
,
array(
$this->toMetricKey($data),
self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
json_encode(array('b' => 'sum', 'labelValues' => $data['labelValues'])),
json_encode(array('b' => $bucketToIncrease, 'labelValues' => $data['labelValues'])),
self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
$data['value'],
json_encode($metaData),
),
4
2
);
}

Expand All @@ -145,30 +145,30 @@ public function updateGauge(array $data)
unset($metaData['labelValues']);
unset($metaData['command']);
$this->redis->eval(<<<LUA
local result = redis.call(KEYS[2], KEYS[1], KEYS[4], ARGV[1])
local result = redis.call(ARGV[1], KEYS[1], ARGV[2], ARGV[3])

if KEYS[2] == 'hSet' then
if ARGV[1] == 'hSet' then
if result == 1 then
redis.call('hSet', KEYS[1], '__meta', ARGV[2])
redis.call('sAdd', KEYS[3], KEYS[1])
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
else
if result == ARGV[1] then
redis.call('hSet', KEYS[1], '__meta', ARGV[2])
redis.call('sAdd', KEYS[3], KEYS[1])
if result == ARGV[3] then
redis.call('hSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
end
LUA
,
array(
$this->toMetricKey($data),
$this->getRedisCommand($data['command']),
self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
$this->getRedisCommand($data['command']),
json_encode($data['labelValues']),
$data['value'],
json_encode($metaData),
),
4
2
);
}

Expand All @@ -180,23 +180,23 @@ public function updateCounter(array $data)
unset($metaData['labelValues']);
unset($metaData['command']);
$result = $this->redis->eval(<<<LUA
local result = redis.call(KEYS[2], KEYS[1], KEYS[4], ARGV[1])
if result == tonumber(ARGV[1]) then
redis.call('hMSet', KEYS[1], '__meta', ARGV[2])
redis.call('sAdd', KEYS[3], KEYS[1])
local result = redis.call(ARGV[1], KEYS[1], ARGV[2], ARGV[3])
if result == tonumber(ARGV[3]) then
redis.call('hMSet', KEYS[1], '__meta', ARGV[4])
redis.call('sAdd', KEYS[2], KEYS[1])
end
return result
LUA
,
array(
$this->toMetricKey($data),
$this->getRedisCommand($data['command']),
self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
$this->getRedisCommand($data['command']),
json_encode($data['labelValues']),
$data['value'],
json_encode($metaData),
),
4
2
);
return $result;
}
Expand Down