MediaWiki master
BufferingStatsdDataFactory.php
Go to the documentation of this file.
1<?php
24
25use Liuggio\StatsdClient\Entity\StatsdData;
26use Liuggio\StatsdClient\Entity\StatsdDataInterface;
27use Liuggio\StatsdClient\Factory\StatsdDataFactory;
28
49class BufferingStatsdDataFactory extends StatsdDataFactory implements IBufferingStatsdDataFactory {
51 protected $buffer = [];
53 protected $enabled = true;
55 private $prefix;
56
57 public function __construct( $prefix ) {
58 parent::__construct();
59 $this->prefix = $prefix;
60 }
61
62 //
63 // Methods for StatsdDataFactoryInterface
64 //
65
71 public function timing( $key, $time ) {
72 if ( !$this->enabled ) {
73 return;
74 }
75 $this->buffer[] = [ $key, $time, StatsdDataInterface::STATSD_METRIC_TIMING ];
76 }
77
83 public function gauge( $key, $value ) {
84 if ( !$this->enabled ) {
85 return;
86 }
87 $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_GAUGE ];
88 }
89
95 public function set( $key, $value ) {
96 if ( !$this->enabled ) {
97 return [];
98 }
99 $this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_SET ];
100 return [];
101 }
102
107 public function increment( $key ) {
108 if ( !$this->enabled ) {
109 return [];
110 }
111 $this->buffer[] = [ $key, 1, StatsdDataInterface::STATSD_METRIC_COUNT ];
112 return [];
113 }
114
119 public function decrement( $key ) {
120 if ( !$this->enabled ) {
121 return;
122 }
123 $this->buffer[] = [ $key, -1, StatsdDataInterface::STATSD_METRIC_COUNT ];
124 }
125
131 public function updateCount( $key, $delta ) {
132 if ( !$this->enabled ) {
133 return;
134 }
135 $this->buffer[] = [ $key, $delta, StatsdDataInterface::STATSD_METRIC_COUNT ];
136 }
137
154 private static function normalizeMetricKey( $key ) {
155 $key = strtr( $key, [ '::' => '.' ] );
156 $key = preg_replace( '/[^a-zA-Z0-9.]+/', '_', $key );
157 $key = trim( $key, '_.' );
158 return strtr( $key, [ '..' => '.' ] );
159 }
160
161 public function produceStatsdData(
162 $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
163 ) {
164 $entity = $this->produceStatsdDataEntity();
165 if ( $key !== null ) {
166 $key = self::normalizeMetricKey( "{$this->prefix}.{$key}" );
167 $entity->setKey( $key );
168 }
169 if ( $value !== null ) {
170 $entity->setValue( $value );
171 }
172 if ( $metric !== null ) {
173 $entity->setMetric( $metric );
174 }
175 return $entity;
176 }
177
178 //
179 // Methods for IBufferingStatsdDataFactory
180 //
181
182 public function hasData() {
183 return (bool)$this->buffer;
184 }
185
190 public function getData() {
191 $data = [];
192 foreach ( $this->buffer as [ $key, $val, $metric ] ) {
193 // Optimization: Don't bother transmitting a counter update with a delta of zero
194 if ( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$val ) {
195 continue;
196 }
197
198 // Optimisation: Avoid produceStatsdData cost during web requests (T288702).
199 // Instead, we do it here in getData() right before the data is transmitted.
200 $data[] = $this->produceStatsdData( $key, $val, $metric );
201 }
202
203 return $data;
204 }
205
206 public function clearData() {
207 $this->buffer = [];
208 }
209
210 public function getDataCount() {
211 return count( $this->buffer );
212 }
213
214 public function setEnabled( $enabled ) {
215 $this->enabled = $enabled;
216 }
217}
218
220class_alias( BufferingStatsdDataFactory::class, 'BufferingStatsdDataFactory' );
MediaWiki's adaption of StatsdDataFactory that provides buffering and metric prefixing.
getDataCount()
Return the number of buffered statsd data entries.
setEnabled( $enabled)
Set collection enable status.
hasData()
Check whether this data factory has any buffered data.
produceStatsdData( $key, $value=1, $metric=StatsdDataInterface::STATSD_METRIC_COUNT)
clearData()
Clear all buffered data from the factory.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.