* @since 1.26
*/
class SamplingStatsdClient extends StatsdClient {
+ protected $samplingRates = [];
+
+ /**
+ * Sampling rates as an associative array of patterns and rates.
+ * Patterns are Unix shell patterns (e.g. 'MediaWiki.api.*').
+ * Rates are sampling probabilities (e.g. 0.1 means 1 in 10 events are sampled).
+ * @param array $samplingRates
+ * @since 1.28
+ */
+ public function setSamplingRates( array $samplingRates ) {
+ $this->samplingRates = $samplingRates;
+ }
+
/**
* Sets sampling rate for all items in $data.
* The sample rate specified in a StatsdData entity overrides the sample rate specified here.
* {@inheritDoc}
*/
public function appendSampleRate( $data, $sampleRate = 1 ) {
- if ( $sampleRate < 1 ) {
- array_walk( $data, function( $item ) use ( $sampleRate ) {
+ $samplingRates = $this->samplingRates;
+ if ( !$samplingRates && $sampleRate !== 1 ) {
+ $samplingRates = [ '*' => $sampleRate ];
+ }
+ if ( $samplingRates ) {
+ array_walk( $data, function( $item ) use ( $samplingRates ) {
/** @var $item StatsdData */
- if ( $item->getSampleRate() === 1 ) {
- $item->setSampleRate( $sampleRate );
+ foreach ( $samplingRates as $pattern => $rate ) {
+ if ( fnmatch( $pattern, $item->getKey(), FNM_NOESCAPE ) ) {
+ $item->setSampleRate( $item->getSampleRate() * $rate );
+ break;
+ }
}
} );
}
}
// add sampling
- if ( $sampleRate < 1 ) {
- $data = $this->appendSampleRate( $data, $sampleRate );
- }
+ $data = $this->appendSampleRate( $data, $sampleRate );
$data = $this->sampleData( $data );
$data = array_map( 'strval', $data );
return [
// $data, $sampleRate, $seed, $expectWrite
- [ $unsampled, 1, 0 /*0.44*/, $unsampled ],
- [ $sampled, 1, 0 /*0.44*/, null ],
- [ $sampled, 1, 4 /*0.03*/, $sampled ],
- [ $unsampled, 0.1, 4 /*0.03*/, $sampled ],
- [ $sampled, 0.5, 0 /*0.44*/, null ],
- [ $sampled, 0.5, 4 /*0.03*/, $sampled ],
+ [ $unsampled, 1, 0 /*0.44*/, true ],
+ [ $sampled, 1, 0 /*0.44*/, false ],
+ [ $sampled, 1, 4 /*0.03*/, true ],
+ [ $unsampled, 0.1, 0 /*0.44*/, false ],
+ [ $sampled, 0.5, 0 /*0.44*/, false ],
+ [ $sampled, 0.5, 4 /*0.03*/, false ],
];
}
+
+ public function testSetSamplingRates() {
+ $matching = new StatsdData();
+ $matching->setKey( 'foo.bar' );
+ $matching->setValue( 1 );
+
+ $nonMatching = new StatsdData();
+ $nonMatching->setKey( 'oof.bar' );
+ $nonMatching->setValue( 1 );
+
+ $sender = $this->getMock( 'Liuggio\StatsdClient\Sender\SenderInterface' );
+ $sender->expects( $this->any() )->method( 'open' )->will( $this->returnValue( true ) );
+ $sender->expects( $this->once() )->method( 'write' )->with( $this->anything(),
+ $this->equalTo( $nonMatching ) );
+
+ $client = new SamplingStatsdClient( $sender );
+ $client->setSamplingRates( [ 'foo.*' => 0.2 ] );
+
+ mt_srand( 0 ); // next random is 0.44
+ $client->send( $matching );
+ mt_srand( 0 );
+ $client->send( $nonMatching );
+ }
}