Merge "MediaWiki.php: Redirect non-standard title urls to canonical"
[lhc/web/wiklou.git] / includes / libs / 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\StatsdDataInterface;
24 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
25
26 /**
27 * A factory for application metric data.
28 *
29 * This class prepends a context-specific prefix to each metric key and keeps
30 * a reference to each constructed metric in an internal array buffer.
31 *
32 * @since 1.25
33 */
34 class BufferingStatsdDataFactory extends StatsdDataFactory {
35 protected $buffer = array();
36
37 public function __construct( $prefix ) {
38 parent::__construct();
39 $this->prefix = $prefix;
40 }
41
42 public function produceStatsdData( $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT ) {
43 $entity = $this->produceStatsdDataEntity();
44 if ( $key !== null ) {
45 $prefixedKey = ltrim( $this->prefix . '.' . $key, '.' );
46 $entity->setKey( $prefixedKey );
47 }
48 if ( $value !== null ) {
49 $entity->setValue( $value );
50 }
51 if ( $metric !== null ) {
52 $entity->setMetric( $metric );
53 }
54 // Don't bother buffering a counter update with a delta of zero.
55 if ( !( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$value ) ) {
56 $this->buffer[] = $entity;
57 }
58 return $entity;
59 }
60
61 public function getBuffer() {
62 return $this->buffer;
63 }
64 }