MediaWiki  REL1_31
BufferingStatsdDataFactory.php
Go to the documentation of this file.
1 <?php
23 use Liuggio\StatsdClient\Entity\StatsdData;
24 use Liuggio\StatsdClient\Entity\StatsdDataInterface;
25 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
26 
35 class 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  return $this->buffer;
100  }
101 
102  public function hasData() {
103  return !empty( $this->buffer );
104  }
105 
106  public function getData() {
107  return $this->buffer;
108  }
109 
110  public function clearData() {
111  $this->buffer = [];
112  }
113 
114  public function getDataCount() {
115  return count( $this->buffer );
116  }
117 
118  public function setEnabled( $enabled ) {
119  $this->enabled = $enabled;
120  }
121 }
BufferingStatsdDataFactory\__construct
__construct( $prefix)
Definition: BufferingStatsdDataFactory.php:47
BufferingStatsdDataFactory\hasData
hasData()
Check whether this data factory has any buffered data.
Definition: BufferingStatsdDataFactory.php:102
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
BufferingStatsdDataFactory\getDataCount
getDataCount()
Return the number of buffered statsd data entries.
Definition: BufferingStatsdDataFactory.php:114
BufferingStatsdDataFactory\$enabled
bool $enabled
Collection enabled?
Definition: BufferingStatsdDataFactory.php:41
BufferingStatsdDataFactory\clearData
clearData()
Clear all buffered data from the factory.
Definition: BufferingStatsdDataFactory.php:110
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
BufferingStatsdDataFactory\produceStatsdData
produceStatsdData( $key, $value=1, $metric=StatsdDataInterface::STATSD_METRIC_COUNT)
Definition: BufferingStatsdDataFactory.php:70
BufferingStatsdDataFactory
A factory for application metric data.
Definition: BufferingStatsdDataFactory.php:35
BufferingStatsdDataFactory\$buffer
$buffer
Definition: BufferingStatsdDataFactory.php:36
BufferingStatsdDataFactory\$prefix
string $prefix
Definition: BufferingStatsdDataFactory.php:45
BufferingStatsdDataFactory\getData
getData()
Return the buffered data from the factory.
Definition: BufferingStatsdDataFactory.php:106
$value
$value
Definition: styleTest.css.php:45
BufferingStatsdDataFactory\setEnabled
setEnabled( $enabled)
Set collection enable status.
Definition: BufferingStatsdDataFactory.php:118
IBufferingStatsdDataFactory
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Definition: IBufferingStatsdDataFactory.php:10
BufferingStatsdDataFactory\getBuffer
getBuffer()
Definition: BufferingStatsdDataFactory.php:98
BufferingStatsdDataFactory\normalizeMetricKey
static normalizeMetricKey( $key)
Normalize a metric key for StatsD.
Definition: BufferingStatsdDataFactory.php:63