Move up devunt's name to Developers
[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 /**
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 {
36 protected $buffer = array();
37
38 public function __construct( $prefix ) {
39 parent::__construct();
40 $this->prefix = $prefix;
41 }
42
43 public function produceStatsdData( $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT ) {
44 $entity = $this->produceStatsdDataEntity();
45 if ( $key !== null ) {
46 $prefixedKey = ltrim( $this->prefix . '.' . $key, '.' );
47 $entity->setKey( $prefixedKey );
48 }
49 if ( $value !== null ) {
50 $entity->setValue( $value );
51 }
52 if ( $metric !== null ) {
53 $entity->setMetric( $metric );
54 }
55 // Don't bother buffering a counter update with a delta of zero.
56 if ( !( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$value ) ) {
57 $this->buffer[] = $entity;
58 }
59 return $entity;
60 }
61
62 public function getBuffer() {
63 return $this->buffer;
64 }
65 }