| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Wikimedia\Stats; |
| 24 |
|
| 25 |
use Liuggio\StatsdClient\Entity\StatsdData; |
| 26 |
use Liuggio\StatsdClient\Entity\StatsdDataInterface; |
| 27 |
use Liuggio\StatsdClient\Factory\StatsdDataFactory; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class BufferingStatsdDataFactory extends StatsdDataFactory implements IBufferingStatsdDataFactory { |
| 50 |
|
| 51 |
protected $buffer = []; |
| 52 |
|
| 53 |
protected $enabled = true; |
| 54 |
|
| 55 |
private $prefix; |
| 56 |
|
| 57 |
public function __construct( $prefix ) { |
| 58 |
parent::__construct(); |
| 59 |
$this->prefix = $prefix; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function timing( $key, $time ) { |
| 72 |
if ( !$this->enabled ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
$this->buffer[] = [ $key, $time, StatsdDataInterface::STATSD_METRIC_TIMING ]; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function gauge( $key, $value ) { |
| 84 |
if ( !$this->enabled ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
$this->buffer[] = [ $key, $value, StatsdDataInterface::STATSD_METRIC_GAUGE ]; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function decrement( $key ) { |
| 120 |
if ( !$this->enabled ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
$this->buffer[] = [ $key, -1, StatsdDataInterface::STATSD_METRIC_COUNT ]; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function updateCount( $key, $delta ) { |
| 132 |
if ( !$this->enabled ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
$this->buffer[] = [ $key, $delta, StatsdDataInterface::STATSD_METRIC_COUNT ]; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
public function hasData() { |
| 183 |
return (bool)$this->buffer; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function getData() { |
| 191 |
$data = []; |
| 192 |
foreach ( $this->buffer as [ $key, $val, $metric ] ) { |
| 193 |
|
| 194 |
if ( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$val ) { |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 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 |
|
| 219 |
|
| 220 |
class_alias( BufferingStatsdDataFactory::class, 'BufferingStatsdDataFactory' ); |