MediaWiki REL1_37
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
36class BufferingStatsdDataFactory extends StatsdDataFactory implements IBufferingStatsdDataFactory {
37 protected $buffer = [];
42 protected $enabled = true;
46 private $prefix;
47
48 public function __construct( $prefix ) {
49 parent::__construct();
50 $this->prefix = $prefix;
51 }
52
64 private static function normalizeMetricKey( $key ) {
65 $key = preg_replace( '/[:.]+/', '.', $key );
66 $key = preg_replace( '/[^a-z0-9.]+/i', '_', $key );
67 $key = trim( $key, '_.' );
68 return str_replace( [ '._', '_.' ], '.', $key );
69 }
70
71 public function produceStatsdData(
72 $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
73 ) {
74 $entity = $this->produceStatsdDataEntity();
75 if ( !$this->enabled ) {
76 return $entity;
77 }
78 if ( $key !== null ) {
79 $key = self::normalizeMetricKey( "{$this->prefix}.{$key}" );
80 $entity->setKey( $key );
81 }
82 if ( $value !== null ) {
83 $entity->setValue( $value );
84 }
85 if ( $metric !== null ) {
86 $entity->setMetric( $metric );
87 }
88 // Don't bother buffering a counter update with a delta of zero.
89 if ( !( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$value ) ) {
90 $this->buffer[] = $entity;
91 }
92 return $entity;
93 }
94
95 public function hasData() {
96 return !empty( $this->buffer );
97 }
98
103 public function getData() {
104 return $this->buffer;
105 }
106
107 public function clearData() {
108 $this->buffer = [];
109 }
110
111 public function getDataCount() {
112 return count( $this->buffer );
113 }
114
115 public function setEnabled( $enabled ) {
116 $this->enabled = $enabled;
117 }
118}
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.