MediaWiki  1.27.2
SamplingStatsdClient.php
Go to the documentation of this file.
1 <?php
26 
32 class SamplingStatsdClient extends StatsdClient {
39  public function appendSampleRate( $data, $sampleRate = 1 ) {
40  if ( $sampleRate < 1 ) {
41  array_walk( $data, function( $item ) use ( $sampleRate ) {
43  if ( $item->getSampleRate() === 1 ) {
44  $item->setSampleRate( $sampleRate );
45  }
46  } );
47  }
48 
49  return $data;
50  }
51 
52  /*
53  * Send the metrics over UDP
54  * Sample the metrics according to their sample rate and send the remaining ones.
55  *
56  * @param StatsdDataInterface|StatsdDataInterface[] $data message(s) to sent
57  * strings are not allowed here as sampleData requires a StatsdDataInterface
58  * @param int $sampleRate
59  *
60  * @return integer the data sent in bytes
61  */
62  public function send( $data, $sampleRate = 1 ) {
63  if ( !is_array( $data ) ) {
64  $data = [ $data ];
65  }
66  if ( !$data ) {
67  return;
68  }
69  foreach ( $data as $item ) {
70  if ( !( $item instanceof StatsdDataInterface ) ) {
71  throw new InvalidArgumentException(
72  'SamplingStatsdClient does not accept stringified messages' );
73  }
74  }
75 
76  // add sampling
77  if ( $sampleRate < 1 ) {
78  $data = $this->appendSampleRate( $data, $sampleRate );
79  }
80  $data = $this->sampleData( $data );
81 
82  $data = array_map( 'strval', $data );
83 
84  // reduce number of packets
85  if ( $this->getReducePacket() ) {
86  $data = $this->reduceCount( $data );
87  }
88 
89  // failures in any of this should be silently ignored if ..
90  $written = 0;
91  try {
92  $fp = $this->getSender()->open();
93  if ( !$fp ) {
94  return;
95  }
96  foreach ( $data as $message ) {
97  $written += $this->getSender()->write( $fp, $message );
98  }
99  $this->getSender()->close( $fp );
100  } catch ( Exception $e ) {
101  $this->throwException( $e );
102  }
103 
104  return $written;
105  }
106 
113  protected function sampleData( $data ) {
114  $newData = [];
115  $mt_rand_max = mt_getrandmax();
116  foreach ( $data as $item ) {
117  $samplingRate = $item->getSampleRate();
118  if ( $samplingRate <= 0.0 || $samplingRate > 1.0 ) {
119  throw new LogicException( 'Sampling rate shall be within ]0, 1]' );
120  }
121  if (
122  $samplingRate === 1 ||
123  ( mt_rand() / $mt_rand_max <= $samplingRate )
124  ) {
125  $newData[] = $item;
126  }
127  }
128  return $newData;
129  }
130 
134  protected function throwException( Exception $exception ) {
135  if ( !$this->getFailSilently() ) {
136  throw $exception;
137  }
138  }
139 }
A statsd client that applies the sampling rate to the data items before sending them.
sampleData($data)
Throw away some of the data according to the sample rate.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:1932
appendSampleRate($data, $sampleRate=1)
Sets sampling rate for all items in $data.
throwException(Exception $exception)
send($data, $sampleRate=1)
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
Definition: distributors.txt:9
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:35