Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/4813' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jul 19, 2013
2 parents d19a95c + 1f39946 commit 8250024
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Filter/Sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Log\Filter;

use Zend\Log\Exception;
use Zend\Log\Filter\FilterInterface;

class Sample implements FilterInterface
{
/**
* Sample rate [0-1].
*
* @var float
*/
protected $sampleRate;

/**
* Filters logging by sample rate.
*
* Sample rate must be a float number between 0 and 1 included.
* If 0.5, only half of the values will be logged.
* If 0.1 only 1 among 10 values will be logged.
*
* @param float|int $samplerate Sample rate [0-1].
* @return Priority
* @throws Exception\InvalidArgumentException
*/
public function __construct($sampleRate = 1)
{
if (! is_numeric($sampleRate)) {
throw new Exception\InvalidArgumentException(sprintf(
'Sample rate must be numeric, received "%s"',
gettype($sampleRate)
));
}

$this->sampleRate = (float) $sampleRate;
}

/**
* Returns TRUE to accept the message, FALSE to block it.
*
* @param array $event event data
* @return bool Accepted ?
*/
public function filter(array $event)
{
return (mt_rand() / mt_getrandmax()) <= $this->sampleRate;
}
}
62 changes: 62 additions & 0 deletions test/Filter/SampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Log
*/

namespace ZendTest\Log\Filter;

use Zend\Log\Filter\Sample;

/**
* @category Zend
* @package Zend_Log
* @subpackage UnitTests
* @group Zend_Log
*/
class SampleTest extends \PHPUnit_Framework_TestCase
{
public function testConstructorThrowsOnInvalidSampleRate()
{
$this->setExpectedException('Zend\Log\Exception\InvalidArgumentException', 'must be numeric');
new Sample('bar');
}

public function testSampleLimit0()
{
// Should log nothing.
$filter = new Sample(0);

// Since sampling is a random process, let's test several times.
$ret = false;
for ($i = 0; $i < 100; $i ++) {
if ($filter->filter(array())) {
break;
$ret = true;
}
}

$this->assertFalse($ret);
}

public function testSampleLimit1()
{
// Should log all events.
$filter = new Sample(1);

// Since sampling is a random process, let's test several times.
$ret = true;
for ($i = 0; $i < 100; $i ++) {
if (! $filter->filter(array())) {
break;
$ret = false;
}
}

$this->assertTrue($ret);
}
}

0 comments on commit 8250024

Please sign in to comment.