26use InvalidArgumentException;
27use Liuggio\StatsdClient\Entity\StatsdData;
28use Liuggio\StatsdClient\Entity\StatsdDataInterface;
29use Liuggio\StatsdClient\StatsdClient;
31use Wikimedia\RequestTimeout\TimeoutException;
66 array_walk( $data,
static function ( $item ) use (
$samplingRates ) {
69 if ( fnmatch( $pattern, $item->getKey(), FNM_NOESCAPE ) ) {
70 $item->setSampleRate( $item->getSampleRate() * $rate );
90 public function send( $data, $sampleRate = 1 ) {
91 if ( !is_array( $data ) ) {
97 foreach ( $data as $item ) {
98 if ( !( $item instanceof StatsdDataInterface ) ) {
99 throw new InvalidArgumentException(
100 'SamplingStatsdClient does not accept stringified messages' );
108 $data = array_map(
'strval', $data );
111 if ( $this->getReducePacket() ) {
112 $data = $this->reduceCount( $data );
118 $fp = $this->getSender()->open();
122 foreach ( $data as $message ) {
123 $written += $this->getSender()->write( $fp, $message );
125 $this->getSender()->close( $fp );
126 }
catch ( TimeoutException $e ) {
128 }
catch ( Exception $e ) {
129 $this->throwException( $e );
142 $mt_rand_max = mt_getrandmax();
143 foreach ( $data as $item ) {
144 $samplingRate = $item->getSampleRate();
145 if ( $samplingRate <= 0.0 || $samplingRate > 1.0 ) {
146 throw new LogicException(
'Sampling rate shall be within ]0, 1]' );
149 $samplingRate === 1 ||
150 ( mt_rand() / $mt_rand_max <= $samplingRate )
162 private function throwException( Exception $exception ) {
163 if ( !$this->getFailSilently() ) {
170class_alias( SamplingStatsdClient::class,
'SamplingStatsdClient' );