12use InvalidArgumentException;
13use Liuggio\StatsdClient\Entity\StatsdData;
14use Liuggio\StatsdClient\Entity\StatsdDataInterface;
15use Liuggio\StatsdClient\StatsdClient;
17use Wikimedia\RequestTimeout\TimeoutException;
52 array_walk( $data,
static function ( $item ) use (
$samplingRates ) {
55 if ( fnmatch( $pattern, $item->getKey(), FNM_NOESCAPE ) ) {
56 $item->setSampleRate( $item->getSampleRate() * $rate );
76 public function send( $data, $sampleRate = 1 ) {
77 if ( !is_array( $data ) ) {
83 foreach ( $data as $item ) {
84 if ( !( $item instanceof StatsdDataInterface ) ) {
85 throw new InvalidArgumentException(
86 'SamplingStatsdClient does not accept stringified messages' );
94 $data = array_map(
'strval', $data );
97 if ( $this->getReducePacket() ) {
98 $data = $this->reduceCount( $data );
104 $fp = $this->getSender()->open();
108 foreach ( $data as $message ) {
109 $written += $this->getSender()->write( $fp, $message );
111 $this->getSender()->close( $fp );
112 }
catch ( TimeoutException $e ) {
114 }
catch ( Exception $e ) {
115 $this->throwException( $e );
128 $mt_rand_max = mt_getrandmax();
129 foreach ( $data as $item ) {
130 $samplingRate = $item->getSampleRate();
131 if ( $samplingRate <= 0.0 || $samplingRate > 1.0 ) {
132 throw new LogicException(
'Sampling rate shall be within ]0, 1]' );
135 $samplingRate === 1 ||
136 ( mt_rand() / $mt_rand_max <= $samplingRate )
148 private function throwException( Exception $exception ) {
149 if ( !$this->getFailSilently() ) {
156class_alias( SamplingStatsdClient::class,
'SamplingStatsdClient' );