MediaWiki  master
BufferingStatsdDataFactory.php
Go to the documentation of this file.
1 <?php
23 use Liuggio\StatsdClient\Entity\StatsdData;
24 use Liuggio\StatsdClient\Entity\StatsdDataInterface;
25 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
26 
47 class BufferingStatsdDataFactory extends StatsdDataFactory implements IBufferingStatsdDataFactory {
49  protected $buffer = [];
51  protected $enabled = true;
53  private $prefix;
54 
55  public function __construct( $prefix ) {
56  parent::__construct();
57  $this->prefix = $prefix;
58  }
59 
60  //
61  // Methods for StatsdDataFactoryInterface
62  //
63 
69  public function timing( $key, $time ) {
70  if ( !$this->enabled ) {
71  return;
72  }
73  $this->buffer[] = [ $key, $time, StatsdDataInterface::STATSD_METRIC_TIMING ];
74  }
75 
81  public function gauge( $key, $value ) {
82  if ( !$this->enabled ) {
83  return;
84  }
85  $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_GAUGE ];
86  }
87 
93  public function set( $key, $value ) {
94  if ( !$this->enabled ) {
95  return;
96  }
97  $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_SET ];
98  }
99 
104  public function increment( $key ) {
105  if ( !$this->enabled ) {
106  return;
107  }
108  $this->buffer[] = [ $key, 1, StatsdDataInterface::STATSD_METRIC_COUNT ];
109  }
110 
115  public function decrement( $key ) {
116  if ( !$this->enabled ) {
117  return;
118  }
119  $this->buffer[] = [ $key, -1, StatsdDataInterface::STATSD_METRIC_COUNT ];
120  }
121 
127  public function updateCount( $key, $delta ) {
128  if ( !$this->enabled ) {
129  return;
130  }
131  $this->buffer[] = [ $key, $delta, StatsdDataInterface::STATSD_METRIC_COUNT ];
132  }
133 
150  private static function normalizeMetricKey( $key ) {
151  $key = strtr( $key, [ '::' => '.' ] );
152  $key = preg_replace( '/[^a-zA-Z0-9.]+/', '_', $key );
153  $key = trim( $key, '_.' );
154  return strtr( $key, [ '..' => '.' ] );
155  }
156 
157  public function produceStatsdData(
158  $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
159  ) {
160  $entity = $this->produceStatsdDataEntity();
161  if ( $key !== null ) {
162  $key = self::normalizeMetricKey( "{$this->prefix}.{$key}" );
163  $entity->setKey( $key );
164  }
165  if ( $value !== null ) {
166  $entity->setValue( $value );
167  }
168  if ( $metric !== null ) {
169  $entity->setMetric( $metric );
170  }
171  return $entity;
172  }
173 
174  //
175  // Methods for IBufferingStatsdDataFactory
176  //
177 
178  public function hasData() {
179  return (bool)$this->buffer;
180  }
181 
186  public function getData() {
187  $data = [];
188  foreach ( $this->buffer as list( $key, $val, $metric ) ) {
189  // Optimization: Don't bother transmitting a counter update with a delta of zero
190  if ( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$val ) {
191  continue;
192  }
193 
194  // Optimisation: Avoid produceStatsdData cost during web requests (T288702).
195  // Instead, we do it here in getData() right before the data is transmitted.
196  $data[] = $this->produceStatsdData( $key, $val, $metric );
197  }
198 
199  return $data;
200  }
201 
202  public function clearData() {
203  $this->buffer = [];
204  }
205 
206  public function getDataCount() {
207  return count( $this->buffer );
208  }
209 
210  public function setEnabled( $enabled ) {
211  $this->enabled = $enabled;
212  }
213 }
BufferingStatsdDataFactory\increment
increment( $key)
Definition: BufferingStatsdDataFactory.php:104
BufferingStatsdDataFactory\__construct
__construct( $prefix)
Definition: BufferingStatsdDataFactory.php:55
BufferingStatsdDataFactory\hasData
hasData()
Check whether this data factory has any buffered data.
Definition: BufferingStatsdDataFactory.php:178
BufferingStatsdDataFactory\getDataCount
getDataCount()
Return the number of buffered statsd data entries.
Definition: BufferingStatsdDataFactory.php:206
BufferingStatsdDataFactory\$buffer
array $buffer
Definition: BufferingStatsdDataFactory.php:49
BufferingStatsdDataFactory\$enabled
bool $enabled
Definition: BufferingStatsdDataFactory.php:51
BufferingStatsdDataFactory\gauge
gauge( $key, $value)
Definition: BufferingStatsdDataFactory.php:81
BufferingStatsdDataFactory\clearData
clearData()
Clear all buffered data from the factory.
Definition: BufferingStatsdDataFactory.php:202
BufferingStatsdDataFactory\produceStatsdData
produceStatsdData( $key, $value=1, $metric=StatsdDataInterface::STATSD_METRIC_COUNT)
Definition: BufferingStatsdDataFactory.php:157
BufferingStatsdDataFactory\timing
timing( $key, $time)
Definition: BufferingStatsdDataFactory.php:69
BufferingStatsdDataFactory\updateCount
updateCount( $key, $delta)
Definition: BufferingStatsdDataFactory.php:127
BufferingStatsdDataFactory\decrement
decrement( $key)
Definition: BufferingStatsdDataFactory.php:115
BufferingStatsdDataFactory
MediaWiki adaption of StatsdDataFactory that provides buffering and metric prefixing.
Definition: BufferingStatsdDataFactory.php:47
BufferingStatsdDataFactory\$prefix
string $prefix
Definition: BufferingStatsdDataFactory.php:53
BufferingStatsdDataFactory\getData
getData()
Definition: BufferingStatsdDataFactory.php:186
BufferingStatsdDataFactory\setEnabled
setEnabled( $enabled)
Set collection enable status.
Definition: BufferingStatsdDataFactory.php:210
IBufferingStatsdDataFactory
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Definition: IBufferingStatsdDataFactory.php:13
BufferingStatsdDataFactory\normalizeMetricKey
static normalizeMetricKey( $key)
Normalize a metric key for StatsD.
Definition: BufferingStatsdDataFactory.php:150