diff --git a/src/Filter/Sample.php b/src/Filter/Sample.php new file mode 100644 index 00000000..ae0c8882 --- /dev/null +++ b/src/Filter/Sample.php @@ -0,0 +1,57 @@ +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; + } +} diff --git a/test/Filter/SampleTest.php b/test/Filter/SampleTest.php new file mode 100644 index 00000000..56972288 --- /dev/null +++ b/test/Filter/SampleTest.php @@ -0,0 +1,62 @@ +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); + } +}