Skip to content

Commit a19100d

Browse files
author
yangchenzhuo
committed
feat: make Redis Adapter can be extended
Signed-off-by: yangchenzhuo <[email protected]>
1 parent 82e42b5 commit a19100d

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/Prometheus/Storage/Redis.php

+21-21
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Redis implements Adapter
2121
/**
2222
* @var mixed[]
2323
*/
24-
private static $defaultOptions = [
24+
protected static $defaultOptions = [
2525
'host' => '127.0.0.1',
2626
'port' => 6379,
2727
'timeout' => 0.1,
@@ -33,22 +33,22 @@ class Redis implements Adapter
3333
/**
3434
* @var string
3535
*/
36-
private static $prefix = 'PROMETHEUS_';
36+
protected static $prefix = 'PROMETHEUS_';
3737

3838
/**
3939
* @var mixed[]
4040
*/
41-
private $options = [];
41+
protected $options = [];
4242

4343
/**
4444
* @var \Redis
4545
*/
46-
private $redis;
46+
protected $redis;
4747

4848
/**
4949
* @var boolean
5050
*/
51-
private $connectionInitialized = false;
51+
protected $connectionInitialized = false;
5252

5353
/**
5454
* Redis constructor.
@@ -63,9 +63,9 @@ public function __construct(array $options = [])
6363
/**
6464
* @param \Redis $redis
6565
* @return self
66-
* @throws StorageException
66+
* @throws StorageException|\RedisException
6767
*/
68-
public static function fromExistingConnection(\Redis $redis): self
68+
public static function fromExistingConnection($redis): self
6969
{
7070
if ($redis->isConnected() === false) {
7171
throw new StorageException('Connection to Redis server not established');
@@ -143,7 +143,7 @@ public function wipeStorage(): void
143143
*
144144
* @return string
145145
*/
146-
private function metaKey(array $data): string
146+
protected function metaKey(array $data): string
147147
{
148148
return implode(':', [
149149
$data['name'],
@@ -156,7 +156,7 @@ private function metaKey(array $data): string
156156
*
157157
* @return string
158158
*/
159-
private function valueKey(array $data): string
159+
protected function valueKey(array $data): string
160160
{
161161
return implode(':', [
162162
$data['name'],
@@ -187,7 +187,7 @@ function (array $metric): MetricFamilySamples {
187187
/**
188188
* @throws StorageException
189189
*/
190-
private function ensureOpenConnection(): void
190+
protected function ensureOpenConnection(): void
191191
{
192192
if ($this->connectionInitialized === true) {
193193
return;
@@ -211,7 +211,7 @@ private function ensureOpenConnection(): void
211211
/**
212212
* @throws StorageException
213213
*/
214-
private function connectToServer(): void
214+
protected function connectToServer(): void
215215
{
216216
try {
217217
$connection_successful = false;
@@ -379,7 +379,7 @@ public function updateCounter(array $data): void
379379
* @param mixed[] $data
380380
* @return mixed[]
381381
*/
382-
private function metaData(array $data): array
382+
protected function metaData(array $data): array
383383
{
384384
$metricsMetaData = $data;
385385
unset($metricsMetaData['value'], $metricsMetaData['command'], $metricsMetaData['labelValues']);
@@ -389,7 +389,7 @@ private function metaData(array $data): array
389389
/**
390390
* @return mixed[]
391391
*/
392-
private function collectHistograms(): array
392+
protected function collectHistograms(): array
393393
{
394394
$keys = $this->redis->sMembers(self::$prefix . Histogram::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
395395
sort($keys);
@@ -471,7 +471,7 @@ private function collectHistograms(): array
471471
*
472472
* @return string
473473
*/
474-
private function removePrefixFromKey(string $key): string
474+
protected function removePrefixFromKey(string $key): string
475475
{
476476
// @phpstan-ignore-next-line false positive, phpstan thinks getOptions returns int
477477
if ($this->redis->getOption(\Redis::OPT_PREFIX) === null) {
@@ -484,7 +484,7 @@ private function removePrefixFromKey(string $key): string
484484
/**
485485
* @return mixed[]
486486
*/
487-
private function collectSummaries(): array
487+
protected function collectSummaries(): array
488488
{
489489
$math = new Math();
490490
$summaryKey = self::$prefix . Summary::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX;
@@ -572,7 +572,7 @@ private function collectSummaries(): array
572572
/**
573573
* @return mixed[]
574574
*/
575-
private function collectGauges(bool $sortMetrics = true): array
575+
protected function collectGauges(bool $sortMetrics = true): array
576576
{
577577
$keys = $this->redis->sMembers(self::$prefix . Gauge::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
578578
sort($keys);
@@ -608,7 +608,7 @@ private function collectGauges(bool $sortMetrics = true): array
608608
/**
609609
* @return mixed[]
610610
*/
611-
private function collectCounters(bool $sortMetrics = true): array
611+
protected function collectCounters(bool $sortMetrics = true): array
612612
{
613613
$keys = $this->redis->sMembers(self::$prefix . Counter::TYPE . self::PROMETHEUS_METRIC_KEYS_SUFFIX);
614614
sort($keys);
@@ -645,7 +645,7 @@ private function collectCounters(bool $sortMetrics = true): array
645645
* @param int $cmd
646646
* @return string
647647
*/
648-
private function getRedisCommand(int $cmd): string
648+
protected function getRedisCommand(int $cmd): string
649649
{
650650
switch ($cmd) {
651651
case Adapter::COMMAND_INCREMENT_INTEGER:
@@ -663,7 +663,7 @@ private function getRedisCommand(int $cmd): string
663663
* @param mixed[] $data
664664
* @return string
665665
*/
666-
private function toMetricKey(array $data): string
666+
protected function toMetricKey(array $data): string
667667
{
668668
return implode(':', [self::$prefix, $data['type'], $data['name']]);
669669
}
@@ -673,7 +673,7 @@ private function toMetricKey(array $data): string
673673
* @return string
674674
* @throws RuntimeException
675675
*/
676-
private function encodeLabelValues(array $values): string
676+
protected function encodeLabelValues(array $values): string
677677
{
678678
$json = json_encode($values);
679679
if (false === $json) {
@@ -687,7 +687,7 @@ private function encodeLabelValues(array $values): string
687687
* @return mixed[]
688688
* @throws RuntimeException
689689
*/
690-
private function decodeLabelValues(string $values): array
690+
protected function decodeLabelValues(string $values): array
691691
{
692692
$json = base64_decode($values, true);
693693
if (false === $json) {

0 commit comments

Comments
 (0)