mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-30 02:37:58 +08:00
Merge pull request #2018 from cwb2819259/feat_prometheus_redis_support_cluster
feat: 新增prometheus使用redis打点支持Redis集群
This commit is contained in:
commit
6bc48a5a3f
@ -1,5 +1,9 @@
|
||||
# v2.0.2 - TBD
|
||||
|
||||
## Added
|
||||
|
||||
- [#2018](https://github.com/hyperf/hyperf/pull/2018) Make prometheus use redis to store data to support cluster mode
|
||||
|
||||
## Fixed
|
||||
|
||||
- [#1898](https://github.com/hyperf/hyperf/pull/1898) Fixed crontab rule `$min-$max` parsing errors.
|
||||
|
@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
*/
|
||||
namespace Hyperf\Metric\Adapter\Prometheus;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Prometheus\Counter;
|
||||
use Prometheus\Exception\StorageException;
|
||||
@ -61,9 +62,6 @@ class Redis implements Adapter
|
||||
*/
|
||||
private $connectionInitialized = false;
|
||||
|
||||
/**
|
||||
* Redis constructor.
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
$this->options = array_merge(self::$defaultOptions, $options);
|
||||
@ -73,7 +71,6 @@ class Redis implements Adapter
|
||||
/**
|
||||
* Create an instance from an established redis connection.
|
||||
* @param \Hyperf\Redis\Redis|\Redis $redis
|
||||
* @return Redis
|
||||
*/
|
||||
public static function fromExistingConnection($redis): self
|
||||
{
|
||||
@ -129,6 +126,7 @@ class Redis implements Adapter
|
||||
public function updateHistogram(array $data): void
|
||||
{
|
||||
$this->openConnection();
|
||||
$redisTag = $this->getRedisTag(Histogram::TYPE);
|
||||
$bucketToIncrease = '+Inf';
|
||||
foreach ($data['buckets'] as $bucket) {
|
||||
if ($data['value'] <= $bucket) {
|
||||
@ -150,8 +148,8 @@ end
|
||||
LUA
|
||||
,
|
||||
[
|
||||
$this->toMetricKey($data),
|
||||
self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
|
||||
$this->toMetricKey($data) . $redisTag,
|
||||
self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX . $redisTag,
|
||||
json_encode(['b' => 'sum', 'labelValues' => $data['labelValues']]),
|
||||
json_encode(['b' => $bucketToIncrease, 'labelValues' => $data['labelValues']]),
|
||||
$data['value'],
|
||||
@ -167,6 +165,7 @@ LUA
|
||||
public function updateGauge(array $data): void
|
||||
{
|
||||
$this->openConnection();
|
||||
$redisTag = $this->getRedisTag(Gauge::TYPE);
|
||||
$metaData = $data;
|
||||
unset($metaData['value'], $metaData['labelValues'], $metaData['command']);
|
||||
|
||||
@ -187,8 +186,8 @@ end
|
||||
LUA
|
||||
,
|
||||
[
|
||||
$this->toMetricKey($data),
|
||||
self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
|
||||
$this->toMetricKey($data) . $redisTag,
|
||||
self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX . $redisTag,
|
||||
$this->getRedisCommand($data['command']),
|
||||
json_encode($data['labelValues']),
|
||||
$data['value'],
|
||||
@ -204,6 +203,7 @@ LUA
|
||||
public function updateCounter(array $data): void
|
||||
{
|
||||
$this->openConnection();
|
||||
$redisTag = $this->getRedisTag(Counter::TYPE);
|
||||
$metaData = $data;
|
||||
unset($metaData['value'], $metaData['labelValues'], $metaData['command']);
|
||||
|
||||
@ -218,8 +218,8 @@ return result
|
||||
LUA
|
||||
,
|
||||
[
|
||||
$this->toMetricKey($data),
|
||||
self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX,
|
||||
$this->toMetricKey($data) . $redisTag,
|
||||
self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX . $redisTag,
|
||||
$this->getRedisCommand($data['command']),
|
||||
$data['value'],
|
||||
json_encode($data['labelValues']),
|
||||
@ -268,7 +268,7 @@ LUA
|
||||
|
||||
private function collectHistograms(): array
|
||||
{
|
||||
$keys = $this->redis->sMembers(self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
|
||||
$keys = $this->redis->sMembers($this->getMetricGatherKey(Histogram::TYPE));
|
||||
sort($keys);
|
||||
$histograms = [];
|
||||
foreach ($keys as $key) {
|
||||
@ -336,7 +336,7 @@ LUA
|
||||
|
||||
private function collectGauges(): array
|
||||
{
|
||||
$keys = $this->redis->sMembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
|
||||
$keys = $this->redis->sMembers($this->getMetricGatherKey(Gauge::TYPE));
|
||||
sort($keys);
|
||||
$gauges = [];
|
||||
foreach ($keys as $key) {
|
||||
@ -362,7 +362,7 @@ LUA
|
||||
|
||||
private function collectCounters(): array
|
||||
{
|
||||
$keys = $this->redis->sMembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
|
||||
$keys = $this->redis->sMembers($this->getMetricGatherKey(Counter::TYPE));
|
||||
sort($keys);
|
||||
$counters = [];
|
||||
foreach ($keys as $key) {
|
||||
@ -404,4 +404,37 @@ LUA
|
||||
{
|
||||
return implode(':', [self::$prefix, $data['type'], $data['name']]);
|
||||
}
|
||||
|
||||
protected function getRedisTag(string $metricType): string
|
||||
{
|
||||
switch($metricType) {
|
||||
case Counter::TYPE:
|
||||
return "{counter}";
|
||||
case Histogram::TYPE:
|
||||
return "{histogram}";
|
||||
case Gauge::TYPE:
|
||||
return "{gauge}";
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the indicator collection key
|
||||
*
|
||||
* @throws \Exception Exception thrown when the incoming metric type does not exist
|
||||
*/
|
||||
private function getMetricGatherKey($metricType): string
|
||||
{
|
||||
switch($metricType) {
|
||||
case Counter::TYPE:
|
||||
return self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX . $this->getRedisTag(Counter::TYPE);
|
||||
case Histogram::TYPE:
|
||||
return self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX . $this->getRedisTag(Histogram::TYPE);
|
||||
case Gauge::TYPE:
|
||||
return self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX . $this->getRedisTag(Gauge::TYPE);
|
||||
default:
|
||||
throw new Exception('Unknown metric type');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user