From ba3ce0de8c2adccde3a6cdcf7dc431e1726a40e8 Mon Sep 17 00:00:00 2001 From: yansongda Date: Sun, 18 Jun 2023 20:57:40 +0800 Subject: [PATCH] Removed the deprecated code for metric. (#5847) --- src/Adapter/Prometheus/Redis.php | 7 +--- .../Prometheus/RedisStorageFactory.php | 3 +- .../Prometheus/RedisStorageFactoryTest.php | 38 ++++++++++++++++--- tests/Adapter/Prometheus/RedisTest.php | 5 --- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Adapter/Prometheus/Redis.php b/src/Adapter/Prometheus/Redis.php index debe4b7..0552e48 100644 --- a/src/Adapter/Prometheus/Redis.php +++ b/src/Adapter/Prometheus/Redis.php @@ -339,12 +339,7 @@ protected function collectSamples(string $metricType): array protected function toMetricKey(array $data): string { - // TODO: This is a hack, we should remove it since v3.1. - if (! str_ends_with(self::$prefix, ':')) { - $prefix = self::$prefix . ':'; - } - - return ($prefix ?? self::$prefix) . implode(':', [$data['type'] ?? '', $data['name'] ?? '']) . $this->getRedisTag($data['type'] ?? ''); + return self::$prefix . implode(':', [$data['type'] ?? '', $data['name'] ?? '']) . $this->getRedisTag($data['type'] ?? ''); } protected function getMetricGatherKey(string $metricType): string diff --git a/src/Adapter/Prometheus/RedisStorageFactory.php b/src/Adapter/Prometheus/RedisStorageFactory.php index ddc1188..c1bc640 100644 --- a/src/Adapter/Prometheus/RedisStorageFactory.php +++ b/src/Adapter/Prometheus/RedisStorageFactory.php @@ -29,8 +29,7 @@ public function __invoke(ContainerInterface $container): Redis $redisFactory = $container->get(RedisFactory::class); Redis::setPrefix($config->get('metric.metric.prometheus.redis_prefix', $config->get('app_name', 'skeleton'))); - // TODO: since 3.1, default value will be changed to ':metric_keys' - Redis::setMetricGatherKeySuffix($config->get('metric.metric.prometheus.redis_gather_key_suffix', '_METRIC_KEYS')); + Redis::setMetricGatherKeySuffix($config->get('metric.metric.prometheus.redis_gather_key_suffix', ':metric_keys')); return new Redis($redisFactory->get($config->get('metric.metric.prometheus.redis_config', 'default'))); } diff --git a/tests/Adapter/Prometheus/RedisStorageFactoryTest.php b/tests/Adapter/Prometheus/RedisStorageFactoryTest.php index b3d8718..6f4c1c6 100644 --- a/tests/Adapter/Prometheus/RedisStorageFactoryTest.php +++ b/tests/Adapter/Prometheus/RedisStorageFactoryTest.php @@ -28,10 +28,6 @@ * @coversNothing */ #[CoversNothing] -/** - * @internal - * @coversNothing - */ class RedisStorageFactoryTest extends TestCase { protected string $prePrefix; @@ -43,7 +39,6 @@ protected function setUp(): void parent::setUp(); $prefixProperty = new ReflectionProperty(Redis::class, 'prefix'); - $metricGatherKeySuffix = new ReflectionProperty(Redis::class, 'metricGatherKeySuffix'); $this->prePrefix = $prefixProperty->getDefaultValue(); @@ -78,7 +73,7 @@ public function testEmptyMetricRedisConfig() self::assertInstanceOf(Redis::class, $redis); self::assertEquals('skeleton', $prefixProperty->getValue($redis)); - self::assertEquals('_METRIC_KEYS', $metricGatherKeySuffixProperty->getValue($redis)); + self::assertEquals(':metric_keys', $metricGatherKeySuffixProperty->getValue($redis)); } public function testNewConfig() @@ -111,4 +106,35 @@ public function testNewConfig() self::assertEquals('prometheus:', $prefixProperty->getValue($redis)); self::assertEquals(':metric_keys', $metricGatherKeySuffixProperty->getValue($redis)); } + + public function testCustomConfig() + { + $redisFactory = Mockery::mock(RedisFactory::class); + $redisFactory->shouldReceive('get')->with('custom')->andReturn(Mockery::mock(RedisProxy::class)); + + $container = Mockery::mock(ContainerInterface::class); + $container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([ + 'metric' => [ + 'metric' => [ + 'prometheus' => [ + 'redis_config' => 'custom', + 'redis_prefix' => 'custom:', + 'redis_gather_key_suffix' => ':custom', + ], + ], + ], + ])); + $container->shouldReceive('get')->with(RedisFactory::class)->andReturn($redisFactory); + + $factory = new RedisStorageFactory(); + $redis = $factory($container); + + $prefixProperty = new ReflectionProperty(Redis::class, 'prefix'); + + $metricGatherKeySuffixProperty = new ReflectionProperty(Redis::class, 'metricGatherKeySuffix'); + + self::assertInstanceOf(Redis::class, $redis); + self::assertEquals('custom:', $prefixProperty->getValue($redis)); + self::assertEquals(':custom', $metricGatherKeySuffixProperty->getValue($redis)); + } } diff --git a/tests/Adapter/Prometheus/RedisTest.php b/tests/Adapter/Prometheus/RedisTest.php index 76ead11..8c59d75 100644 --- a/tests/Adapter/Prometheus/RedisTest.php +++ b/tests/Adapter/Prometheus/RedisTest.php @@ -25,10 +25,6 @@ * @coversNothing */ #[CoversNothing] -/** - * @internal - * @coversNothing - */ class RedisTest extends TestCase { protected string $prePrefix; @@ -40,7 +36,6 @@ protected function setUp(): void parent::setUp(); $prefixProperty = new ReflectionProperty(Redis::class, 'prefix'); - $metricGatherKeySuffix = new ReflectionProperty(Redis::class, 'metricGatherKeySuffix'); $this->prePrefix = $prefixProperty->getDefaultValue();