]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/libs/stats/BufferingStatsdDataFactory.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / libs / stats / BufferingStatsdDataFactory.php
1 <?php
2 /**
3  * Copyright 2015
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @file
21  */
22
23 use Liuggio\StatsdClient\Entity\StatsdData;
24 use Liuggio\StatsdClient\Entity\StatsdDataInterface;
25 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
26
27 /**
28  * A factory for application metric data.
29  *
30  * This class prepends a context-specific prefix to each metric key and keeps
31  * a reference to each constructed metric in an internal array buffer.
32  *
33  * @since 1.25
34  */
35 class BufferingStatsdDataFactory extends StatsdDataFactory implements IBufferingStatsdDataFactory {
36         protected $buffer = [];
37         /**
38          * Collection enabled?
39          * @var bool
40          */
41         protected $enabled = true;
42         /**
43          * @var string
44          */
45         private $prefix;
46
47         public function __construct( $prefix ) {
48                 parent::__construct();
49                 $this->prefix = $prefix;
50         }
51
52         /**
53          * Normalize a metric key for StatsD
54          *
55          * Replace occurences of '::' with dots and any other non-alphanumeric
56          * characters with underscores. Combine runs of dots or underscores.
57          * Then trim leading or trailing dots or underscores.
58          *
59          * @param string $key
60          * @since 1.26
61          * @return string
62          */
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
94         /**
95          * @deprecated Use getData()
96          * @return StatsdData[]
97          */
98         public function getBuffer() {
99                 return $this->buffer;
100         }
101
102         /**
103          * Check whether this data factory has any data.
104          * @return bool
105          */
106         public function hasData() {
107                 return !empty( $this->buffer );
108         }
109
110         /**
111          * Return data from the factory.
112          * @return StatsdData[]
113          */
114         public function getData() {
115                 return $this->buffer;
116         }
117
118         /**
119          * Set collection enable status.
120          * @param bool $enabled Will collection be enabled?
121          * @return void
122          */
123         public function setEnabled( $enabled ) {
124                 $this->enabled = $enabled;
125         }
126 }