Merge "Add page_restrictions to readlock in lockSearchindex"
[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\Factory\StatsdDataFactory;
24
25 /**
26 * A factory for application metric data.
27 *
28 * This class prepends a context-specific prefix to each metric key and keeps
29 * a reference to each constructed metric in an internal array buffer.
30 *
31 * @since 1.25
32 */
33 class BufferingStatsdDataFactory extends StatsdDataFactory {
34 protected $buffer = array();
35
36 public function __construct( $prefix ) {
37 parent::__construct();
38 $this->prefix = $prefix;
39 }
40
41 public function produceStatsdData( $key, $value = 1, $metric = self::STATSD_METRIC_COUNT ) {
42 $this->buffer[] = $entity = $this->produceStatsdDataEntity();
43 if ( $key !== null ) {
44 $prefixedKey = ltrim( $this->prefix . '.' . $key, '.' );
45 $entity->setKey( $prefixedKey );
46 }
47 if ( $value !== null ) {
48 $entity->setValue( $value );
49 }
50 if ( $metric !== null ) {
51 $entity->setMetric( $metric );
52 }
53 return $entity;
54 }
55
56 public function getBuffer() {
57 return $this->buffer;
58 }
59 }