Skip to content

Commit

Permalink
Removed the deprecated code for metric. (#5847)
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda authored Jun 18, 2023
1 parent e8060a7 commit ba3ce0d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
7 changes: 1 addition & 6 deletions src/Adapter/Prometheus/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/Adapter/Prometheus/RedisStorageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}
Expand Down
38 changes: 32 additions & 6 deletions tests/Adapter/Prometheus/RedisStorageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
* @coversNothing
*/
#[CoversNothing]
/**
* @internal
* @coversNothing
*/
class RedisStorageFactoryTest extends TestCase
{
protected string $prePrefix;
Expand All @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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));
}
}
5 changes: 0 additions & 5 deletions tests/Adapter/Prometheus/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
* @coversNothing
*/
#[CoversNothing]
/**
* @internal
* @coversNothing
*/
class RedisTest extends TestCase
{
protected string $prePrefix;
Expand All @@ -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();
Expand Down

0 comments on commit ba3ce0d

Please sign in to comment.