Skip to content

Commit

Permalink
feat: make Redis Adapter can be extended
Browse files Browse the repository at this point in the history
Signed-off-by: yangchenzhuo <[email protected]>
  • Loading branch information
yangchenzhuo committed May 4, 2023
1 parent 82e42b5 commit a19100d
Showing 1 changed file with 21 additions and 21 deletions.
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
{
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

0 comments on commit a19100d

Please sign in to comment.