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

feat: make Redis Adapter can be extended #121

Open
wants to merge 1 commit 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
42 changes: 21 additions & 21 deletions src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Redis implements Adapter
/**
* @var mixed[]
*/
private static $defaultOptions = [
protected static $defaultOptions = [
'host' => '127.0.0.1',
'port' => 6379,
'timeout' => 0.1,
Expand All @@ -33,22 +33,22 @@ class Redis implements Adapter
/**
* @var string
*/
private static $prefix = 'PROMETHEUS_';
protected static $prefix = 'PROMETHEUS_';

/**
* @var mixed[]
*/
private $options = [];
protected $options = [];

/**
* @var \Redis
*/
private $redis;
protected $redis;

/**
* @var boolean
*/
private $connectionInitialized = false;
protected $connectionInitialized = false;

/**
* Redis constructor.
Expand All @@ -63,9 +63,9 @@ public function __construct(array $options = [])
/**
* @param \Redis $redis
* @return self
* @throws StorageException
* @throws StorageException|\RedisException
*/
public static function fromExistingConnection(\Redis $redis): self
public static function fromExistingConnection($redis): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the explicit type here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, in Hyperf program,Redis instance is not \Redis,but functions are same.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By looking at the documentation it states hyperf returns a proxy object to \Redis: https://hyperf.wiki/3.1/#/en/redis?id=usage

Looks like the implementation, is still typeable by using \Hyperf\Redis\Redis. Would you mind adding that type side-by-side \Redis|\Hyperf\Redis\Redis?

Union types where added in php 8.0, this library still supports 7.2. @LKaemmerling do you mind dropping support for outdated and unmaintained php versions, too? I'd like to create a PR right away.

{
if ($redis->isConnected() === false) {
throw new StorageException('Connection to Redis server not established');
Expand Down Expand Up @@ -143,7 +143,7 @@ public function wipeStorage(): void
*
* @return string
*/
private function metaKey(array $data): string
protected function metaKey(array $data): string
{
return implode(':', [
$data['name'],
Expand All @@ -156,7 +156,7 @@ private function metaKey(array $data): string
*
* @return string
*/
private function valueKey(array $data): string
protected function valueKey(array $data): string
{
return implode(':', [
$data['name'],
Expand Down Expand Up @@ -187,7 +187,7 @@ function (array $metric): MetricFamilySamples {
/**
* @throws StorageException
*/
private function ensureOpenConnection(): void
protected function ensureOpenConnection(): void
{
if ($this->connectionInitialized === true) {
return;
Expand All @@ -211,7 +211,7 @@ private function ensureOpenConnection(): void
/**
* @throws StorageException
*/
private function connectToServer(): void
protected function connectToServer(): void
{
try {
$connection_successful = false;
Expand Down Expand Up @@ -379,7 +379,7 @@ public function updateCounter(array $data): void
* @param mixed[] $data
* @return mixed[]
*/
private function metaData(array $data): array
protected function metaData(array $data): array
{
$metricsMetaData = $data;
unset($metricsMetaData['value'], $metricsMetaData['command'], $metricsMetaData['labelValues']);
Expand All @@ -389,7 +389,7 @@ private function metaData(array $data): array
/**
* @return mixed[]
*/
private function collectHistograms(): array
protected function collectHistograms(): array
{
$keys = $this->redis->sMembers(self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
Expand Down Expand Up @@ -471,7 +471,7 @@ private function collectHistograms(): array
*
* @return string
*/
private function removePrefixFromKey(string $key): string
protected function removePrefixFromKey(string $key): string
{
// @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
if ($this->redis->getOption(\Redis::OPT_PREFIX) === null) {
Expand All @@ -484,7 +484,7 @@ private function removePrefixFromKey(string $key): string
/**
* @return mixed[]
*/
private function collectSummaries(): array
protected function collectSummaries(): array
{
$math = new Math();
$summaryKey = self::$prefix . Summary::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX;
Expand Down Expand Up @@ -572,7 +572,7 @@ private function collectSummaries(): array
/**
* @return mixed[]
*/
private function collectGauges(bool $sortMetrics = true): array
protected function collectGauges(bool $sortMetrics = true): array
{
$keys = $this->redis->sMembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
Expand Down Expand Up @@ -608,7 +608,7 @@ private function collectGauges(bool $sortMetrics = true): array
/**
* @return mixed[]
*/
private function collectCounters(bool $sortMetrics = true): array
protected function collectCounters(bool $sortMetrics = true): array
{
$keys = $this->redis->sMembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
sort($keys);
Expand Down Expand Up @@ -645,7 +645,7 @@ private function collectCounters(bool $sortMetrics = true): array
* @param int $cmd
* @return string
*/
private function getRedisCommand(int $cmd): string
protected function getRedisCommand(int $cmd): string
{
switch ($cmd) {
case Adapter::COMMAND_INCREMENT_INTEGER:
Expand All @@ -663,7 +663,7 @@ private function getRedisCommand(int $cmd): string
* @param mixed[] $data
* @return string
*/
private function toMetricKey(array $data): string
protected function toMetricKey(array $data): string
{
return implode(':', [self::$prefix, $data['type'], $data['name']]);
}
Expand All @@ -673,7 +673,7 @@ private function toMetricKey(array $data): string
* @return string
* @throws RuntimeException
*/
private function encodeLabelValues(array $values): string
protected function encodeLabelValues(array $values): string
{
$json = json_encode($values);
if (false === $json) {
Expand All @@ -687,7 +687,7 @@ private function encodeLabelValues(array $values): string
* @return mixed[]
* @throws RuntimeException
*/
private function decodeLabelValues(string $values): array
protected function decodeLabelValues(string $values): array
{
$json = base64_decode($values, true);
if (false === $json) {
Expand Down