MediaWiki REL1_32
SamplingStatsdClient.php
Go to the documentation of this file.
1<?php
23use Liuggio\StatsdClient\StatsdClient;
24use Liuggio\StatsdClient\Entity\StatsdData;
25use Liuggio\StatsdClient\Entity\StatsdDataInterface;
26
32class SamplingStatsdClient extends StatsdClient {
33 protected $samplingRates = [];
34
43 $this->samplingRates = $samplingRates;
44 }
45
52 public function appendSampleRate( $data, $sampleRate = 1 ) {
54 if ( !$samplingRates && $sampleRate !== 1 ) {
55 $samplingRates = [ '*' => $sampleRate ];
56 }
57 if ( $samplingRates ) {
58 array_walk( $data, function ( $item ) use ( $samplingRates ) {
60 foreach ( $samplingRates as $pattern => $rate ) {
61 if ( fnmatch( $pattern, $item->getKey(), FNM_NOESCAPE ) ) {
62 $item->setSampleRate( $item->getSampleRate() * $rate );
63 break;
64 }
65 }
66 } );
67 }
68
69 return $data;
70 }
71
82 public function send( $data, $sampleRate = 1 ) {
83 if ( !is_array( $data ) ) {
84 $data = [ $data ];
85 }
86 if ( !$data ) {
87 return;
88 }
89 foreach ( $data as $item ) {
90 if ( !( $item instanceof StatsdDataInterface ) ) {
91 throw new InvalidArgumentException(
92 'SamplingStatsdClient does not accept stringified messages' );
93 }
94 }
95
96 // add sampling
97 $data = $this->appendSampleRate( $data, $sampleRate );
98 $data = $this->sampleData( $data );
99
100 $data = array_map( 'strval', $data );
101
102 // reduce number of packets
103 if ( $this->getReducePacket() ) {
104 $data = $this->reduceCount( $data );
105 }
106
107 // failures in any of this should be silently ignored if ..
108 $written = 0;
109 try {
110 $fp = $this->getSender()->open();
111 if ( !$fp ) {
112 return;
113 }
114 foreach ( $data as $message ) {
115 $written += $this->getSender()->write( $fp, $message );
116 }
117 $this->getSender()->close( $fp );
118 } catch ( Exception $e ) {
119 $this->throwException( $e );
120 }
121
122 return $written;
123 }
124
131 protected function sampleData( $data ) {
132 $newData = [];
133 $mt_rand_max = mt_getrandmax();
134 foreach ( $data as $item ) {
135 $samplingRate = $item->getSampleRate();
136 if ( $samplingRate <= 0.0 || $samplingRate > 1.0 ) {
137 throw new LogicException( 'Sampling rate shall be within ]0, 1]' );
138 }
139 if (
140 $samplingRate === 1 ||
141 ( mt_rand() / $mt_rand_max <= $samplingRate )
142 ) {
143 $newData[] = $item;
144 }
145 }
146 return $newData;
147 }
148
152 protected function throwException( Exception $exception ) {
153 if ( !$this->getFailSilently() ) {
154 throw $exception;
155 }
156 }
157}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
A statsd client that applies the sampling rate to the data items before sending them.
setSamplingRates(array $samplingRates)
Sampling rates as an associative array of patterns and rates.
send( $data, $sampleRate=1)
Send the metrics over UDP Sample the metrics according to their sample rate and send the remaining on...
throwException(Exception $exception)
@inheritDoc
sampleData( $data)
Throw away some of the data according to the sample rate.
appendSampleRate( $data, $sampleRate=1)
Sets sampling rate for all items in $data.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
returning false will NOT prevent logging $e
Definition hooks.txt:2226
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))