MediaWiki master
BufferingStatsdDataFactory.php
Go to the documentation of this file.
1<?php
23use Liuggio\StatsdClient\Entity\StatsdData;
24use Liuggio\StatsdClient\Entity\StatsdDataInterface;
25use Liuggio\StatsdClient\Factory\StatsdDataFactory;
26
47class 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 return [];
99 }
100
105 public function increment( $key ) {
106 if ( !$this->enabled ) {
107 return [];
108 }
109 $this->buffer[] = [ $key, 1, StatsdDataInterface::STATSD_METRIC_COUNT ];
110 return [];
111 }
112
117 public function decrement( $key ) {
118 if ( !$this->enabled ) {
119 return;
120 }
121 $this->buffer[] = [ $key, -1, StatsdDataInterface::STATSD_METRIC_COUNT ];
122 }
123
129 public function updateCount( $key, $delta ) {
130 if ( !$this->enabled ) {
131 return;
132 }
133 $this->buffer[] = [ $key, $delta, StatsdDataInterface::STATSD_METRIC_COUNT ];
134 }
135
152 private static function normalizeMetricKey( $key ) {
153 $key = strtr( $key, [ '::' => '.' ] );
154 $key = preg_replace( '/[^a-zA-Z0-9.]+/', '_', $key );
155 $key = trim( $key, '_.' );
156 return strtr( $key, [ '..' => '.' ] );
157 }
158
159 public function produceStatsdData(
160 $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
161 ) {
162 $entity = $this->produceStatsdDataEntity();
163 if ( $key !== null ) {
164 $key = self::normalizeMetricKey( "{$this->prefix}.{$key}" );
165 $entity->setKey( $key );
166 }
167 if ( $value !== null ) {
168 $entity->setValue( $value );
169 }
170 if ( $metric !== null ) {
171 $entity->setMetric( $metric );
172 }
173 return $entity;
174 }
175
176 //
177 // Methods for IBufferingStatsdDataFactory
178 //
179
180 public function hasData() {
181 return (bool)$this->buffer;
182 }
183
188 public function getData() {
189 $data = [];
190 foreach ( $this->buffer as [ $key, $val, $metric ] ) {
191 // Optimization: Don't bother transmitting a counter update with a delta of zero
192 if ( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$val ) {
193 continue;
194 }
195
196 // Optimisation: Avoid produceStatsdData cost during web requests (T288702).
197 // Instead, we do it here in getData() right before the data is transmitted.
198 $data[] = $this->produceStatsdData( $key, $val, $metric );
199 }
200
201 return $data;
202 }
203
204 public function clearData() {
205 $this->buffer = [];
206 }
207
208 public function getDataCount() {
209 return count( $this->buffer );
210 }
211
212 public function setEnabled( $enabled ) {
213 $this->enabled = $enabled;
214 }
215}
MediaWiki's adaption of StatsdDataFactory that provides buffering and metric prefixing.
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.
getDataCount()
Return the number of buffered statsd data entries.
setEnabled( $enabled)
Set collection enable status.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.