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

Add Redis adapter #37

Open
wants to merge 12 commits into
base: 3.x
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Currently supported backends:
* Null (Dummy that does nothing)
* StatsD
* Zabbix
* Redis

## Installation

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"suggest": {
"corley/influxdb-sdk": "For InfluxDB integration",
"okitsu/zabbix-sender": "For zabbix integration",
"kriswallsmith/buzz": "For Librato integration"
"kriswallsmith/buzz": "For Librato integration",
"colinmollenhour/credis": "For Redis integration"
},
"require": {
"psr/log": "~1.0"
Expand All @@ -30,7 +31,8 @@
"okitsu/zabbix-sender": "*@dev",
"symfony/config": "~2.3",
"symfony/dependency-injection": "~2.3",
"symfony/http-kernel": "~2.3"
"symfony/http-kernel": "~2.3",
"colinmollenhour/credis": "^1.5"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions example/redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require_once __DIR__.'/../vendor/autoload.php';

$credis = new Credis_Client();

$metrics = \Beberlei\Metrics\Factory::create('credis', array(
'credis_client' => $credis,
));

while (true) {
$metrics->increment('foo.bar');
$metrics->decrement('foo.baz');
$metrics->measure('foo', rand(1, 10));
usleep(10000);
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ private function createCollector($type, $config)
$definition->replaceArgument(0, $sender);
$definition->replaceArgument(1, $config['prefix']);

return $definition;
case 'credis':
$credis_client = new Definition('Credis_Client');
$credis_client ->addArgument($config['host'] ?: 'localhost');
$credis_client ->addArgument($config['port'] ?: 6379);

$definition->replaceArgument(0, $credis_client);

return $definition;
default:
throw new \InvalidArgumentException(sprintf('The type "%s" is not supported', $type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('type')->isRequired()->end()
->scalarNode('connection')->defaultNull()->end()
->scalarNode('credis_client')->defaultNull()->end()
->scalarNode('file')->defaultNull()->end()
->scalarNode('host')->defaultNull()->end()
->scalarNode('password')->defaultNull()->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<argument /> <!-- sender, set by the extension -->
<argument /> <!-- host, set by the extension -->
</service>
<service id="beberlei_metrics.collector_proto.credis" class="Beberlei\Metrics\Collector\CRedis" abstract="true">
<argument /> <!-- credis_client, set by the extension -->
</service>
</services>

</container>
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,32 @@ public function testWithZabbix()
$this->assertInstanceOf('Net\Zabbix\Sender', $sender);
}

public function testWithCRedis()
{
$container = $this->createContainer(array(
'default' => 'simple',
'collectors' => array(
'simple' => array(
'type' => 'credis',
'credis_client' => 'credis'
),
'full' => array(
'type' => 'credis',
'credis_client' => 'credis',
'host' => 'redis.localhost',
'port' => 1234
)
),
));

$collector = $container->get('beberlei_metrics.collector.simple');
$this->assertInstanceOf('Beberlei\Metrics\Collector\CRedis', $collector);

$collector = $container->get('beberlei_metrics.collector.full');
$this->assertInstanceOf('Beberlei\Metrics\Collector\CRedis', $collector);
}


private function createContainer($configs)
{
$container = new ContainerBuilder();
Expand Down
66 changes: 66 additions & 0 deletions src/Beberlei/Metrics/Collector/CRedis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Beberlei Metrics.
*
* LICENSE
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/
namespace Beberlei\Metrics\Collector;

use Credis_Client;

class CRedis implements Collector
{
/** @var \Credis_Client */
private $credis_client;

public function __construct(Credis_Client $credis_client)
{
$this->credis_client = $credis_client;
}

/**
* {@inheritdoc}
*/
public function increment($variable)
{
$this->credis_client->incr($variable);
Copy link

Choose a reason for hiding this comment

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

https://redis.io/commands/incr/

Note: this is a string operation because Redis does not have a dedicated integer type. The string stored at the key is interpreted as a base-10 64 bit signed integer to execute the operation.

Once you reach the limit of 2 pow 63 + 1, you will get an error

https://stackoverflow.com/questions/36861472/what-happens-when-int64-maxvalue-is-exceded-with-redis-incr

}

Copy link

Choose a reason for hiding this comment

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

@inheritdoc should be added on methods

/**
* {@inheritdoc}
*/
public function decrement($variable)
{
$this->credis_client->decr($variable);
}

/**
* {@inheritdoc}
*/
public function timing($variable, $time)
{
$this->credis_client->set($variable, $time);
}

/**
* {@inheritdoc}
*/
public function measure($variable, $value)
{
$this->credis_client->set($variable, $value);
}

/**
* {@inheritdoc}
*/
public function flush()
{
// No Need to Implement flush() method for now.
}
}
9 changes: 8 additions & 1 deletion src/Beberlei/Metrics/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function create($type, array $options = array())
if (!isset($options['host']) && isset($options['port'])) {
throw new MetricsException('You should specified a host if you specified a port.');
}

$prefix = isset($options['prefix']) ? $options['prefix'] : '';

return new Collector\StatsD($options['host'], $options['port'], $prefix);
Expand Down Expand Up @@ -135,6 +135,13 @@ public static function create($type, array $options = array())

return new Collector\InfluxDB($options['client']);

case 'credis':
if (!isset($options['credis_client'])) {
throw new MetricsException('Missing \'credis_client\' key for CRedis collector.');
}

return new Collector\CRedis($options['credis_client']);

case 'null':
return new Collector\NullCollector();

Expand Down
2 changes: 2 additions & 0 deletions src/Beberlei/Metrics/Tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getCreateValidMetricTests()
array('Beberlei\Metrics\Collector\Logger', 'logger', array('logger' => new NullLogger())),
array('Beberlei\Metrics\Collector\NullCollector', 'null'),
array('Beberlei\Metrics\Collector\InfluxDB', 'influxdb', array('client' => $this->getMockBuilder('\\InfluxDB\\Client')->disableOriginalConstructor()->getMock())),
array('Beberlei\Metrics\Collector\CRedis', 'credis', array('credis_client' => $this->getMockBuilder('Credis_Client')->disableOriginalConstructor()->getMock()))
);
}

Expand Down Expand Up @@ -53,6 +54,7 @@ public function getCreateThrowExceptionIfOptionsAreInvalidTests()
array('connection is required for Doctrine DBAL collector.', 'doctrine_dbal'),
array('Missing \'logger\' key with logger service.', 'logger'),
array('Missing \'client\' key for InfluxDB collector.', 'influxdb'),
array('Missing \'credis_client\' key for CRedis collector.', 'credis'),
);
}

Expand Down