MediaWiki REL1_32
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
35class BufferingStatsdDataFactory extends StatsdDataFactory implements IBufferingStatsdDataFactory {
36 protected $buffer = [];
41 protected $enabled = true;
45 private $prefix;
46
47 public function __construct( $prefix ) {
48 parent::__construct();
49 $this->prefix = $prefix;
50 }
51
63 private static function normalizeMetricKey( $key ) {
64 $key = preg_replace( '/[:.]+/', '.', $key );
65 $key = preg_replace( '/[^a-z0-9.]+/i', '_', $key );
66 $key = trim( $key, '_.' );
67 return str_replace( [ '._', '_.' ], '.', $key );
68 }
69
70 public function produceStatsdData(
71 $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
72 ) {
73 $entity = $this->produceStatsdDataEntity();
74 if ( !$this->enabled ) {
75 return $entity;
76 }
77 if ( $key !== null ) {
78 $key = self::normalizeMetricKey( "{$this->prefix}.{$key}" );
79 $entity->setKey( $key );
80 }
81 if ( $value !== null ) {
82 $entity->setValue( $value );
83 }
84 if ( $metric !== null ) {
85 $entity->setMetric( $metric );
86 }
87 // Don't bother buffering a counter update with a delta of zero.
88 if ( !( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$value ) ) {
89 $this->buffer[] = $entity;
90 }
91 return $entity;
92 }
93
98 public function getBuffer() {
99 wfDeprecated( __METHOD__, '1.30' );
100 return $this->buffer;
101 }
102
103 public function hasData() {
104 return !empty( $this->buffer );
105 }
106
111 public function getData() {
112 return $this->buffer;
113 }
114
115 public function clearData() {
116 $this->buffer = [];
117 }
118
119 public function getDataCount() {
120 return count( $this->buffer );
121 }
122
123 public function setEnabled( $enabled ) {
124 $this->enabled = $enabled;
125 }
126}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
A factory for application metric data.
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.
static normalizeMetricKey( $key)
Normalize a metric key for StatsD.
setEnabled( $enabled)
Set collection enable status.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.