MediaWiki master
BufferingStatsdDataFactory.php
Go to the documentation of this file.
1<?php
10
11use Liuggio\StatsdClient\Entity\StatsdData;
12use Liuggio\StatsdClient\Entity\StatsdDataInterface;
13use Liuggio\StatsdClient\Factory\StatsdDataFactory;
14
36class BufferingStatsdDataFactory extends StatsdDataFactory implements IBufferingStatsdDataFactory {
38 protected $buffer = [];
40 protected $enabled = true;
42 private $prefix;
43
44 public function __construct( string $prefix ) {
45 parent::__construct();
46 $this->prefix = $prefix;
47 }
48
49 //
50 // Methods for StatsdDataFactoryInterface
51 //
52
58 public function timing( $key, $time ) {
59 if ( !$this->enabled ) {
60 return;
61 }
62 $this->buffer[] = [ $key, $time, StatsdDataInterface::STATSD_METRIC_TIMING ];
63 }
64
70 public function gauge( $key, $value ) {
71 if ( !$this->enabled ) {
72 return;
73 }
74 $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_GAUGE ];
75 }
76
82 public function set( $key, $value ) {
83 if ( !$this->enabled ) {
84 return [];
85 }
86 $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_SET ];
87 return [];
88 }
89
94 public function increment( $key ) {
95 if ( !$this->enabled ) {
96 return [];
97 }
98 $this->buffer[] = [ $key, 1, StatsdDataInterface::STATSD_METRIC_COUNT ];
99 return [];
100 }
101
106 public function decrement( $key ) {
107 if ( !$this->enabled ) {
108 return;
109 }
110 $this->buffer[] = [ $key, -1, StatsdDataInterface::STATSD_METRIC_COUNT ];
111 }
112
118 public function updateCount( $key, $delta ) {
119 if ( !$this->enabled ) {
120 return;
121 }
122 $this->buffer[] = [ $key, $delta, StatsdDataInterface::STATSD_METRIC_COUNT ];
123 }
124
141 private static function normalizeMetricKey( $key ) {
142 $key = strtr( $key, [ '::' => '.' ] );
143 $key = preg_replace( '/[^a-zA-Z0-9.]+/', '_', $key );
144 $key = trim( $key, '_.' );
145 return strtr( $key, [ '..' => '.' ] );
146 }
147
149 public function produceStatsdData(
150 $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
151 ) {
152 $entity = $this->produceStatsdDataEntity();
153 if ( $key !== null ) {
154 $key = self::normalizeMetricKey( "{$this->prefix}.{$key}" );
155 $entity->setKey( $key );
156 }
157 if ( $value !== null ) {
158 $entity->setValue( $value );
159 }
160 if ( $metric !== null ) {
161 $entity->setMetric( $metric );
162 }
163 return $entity;
164 }
165
166 //
167 // Methods for IBufferingStatsdDataFactory
168 //
169
171 public function hasData() {
172 return (bool)$this->buffer;
173 }
174
179 public function getData() {
180 $data = [];
181 foreach ( $this->buffer as [ $key, $val, $metric ] ) {
182 // Optimization: Don't bother transmitting a counter update with a delta of zero
183 if ( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$val ) {
184 continue;
185 }
186
187 // Optimisation: Avoid produceStatsdData cost during web requests (T288702).
188 // Instead, we do it here in getData() right before the data is transmitted.
189 $data[] = $this->produceStatsdData( $key, $val, $metric );
190 }
191
192 return $data;
193 }
194
196 public function clearData() {
197 $this->buffer = [];
198 }
199
201 public function getDataCount() {
202 return count( $this->buffer );
203 }
204
206 public function setEnabled( $enabled ) {
207 $this->enabled = $enabled;
208 }
209}
210
212class_alias( BufferingStatsdDataFactory::class, 'BufferingStatsdDataFactory' );
MediaWiki's adaption of StatsdDataFactory that provides buffering and metric prefixing.
getDataCount()
Return the number of buffered statsd data entries.int 1.31
setEnabled( $enabled)
Set collection enable status.void
hasData()
Check whether this data factory has any buffered data.bool
produceStatsdData( $key, $value=1, $metric=StatsdDataInterface::STATSD_METRIC_COUNT)
clearData()
Clear all buffered data from the factory.1.31
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.