Completed
Branch master (939199)
by
unknown
39:35
created

includes/libs/stats/BufferingStatsdDataFactory.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 {
36
	protected $buffer = [];
37
38
	public function __construct( $prefix ) {
39
		parent::__construct();
40
		$this->prefix = $prefix;
0 ignored issues
show
The property prefix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
	}
42
43
	/**
44
	 * Normalize a metric key for StatsD
45
	 *
46
	 * Replace occurences of '::' with dots and any other non-alphanumeric
47
	 * characters with underscores. Combine runs of dots or underscores.
48
	 * Then trim leading or trailing dots or underscores.
49
	 *
50
	 * @param string $key
51
	 * @since 1.26
52
	 */
53
	private static function normalizeMetricKey( $key ) {
54
		$key = preg_replace( '/[:.]+/', '.', $key );
55
		$key = preg_replace( '/[^a-z0-9.]+/i', '_', $key );
56
		$key = trim( $key, '_.' );
57
		return str_replace( [ '._', '_.' ], '.', $key );
58
	}
59
60
	public function produceStatsdData(
61
		$key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
62
	) {
63
		$entity = $this->produceStatsdDataEntity();
64
		if ( $key !== null ) {
65
			$key = self::normalizeMetricKey( "{$this->prefix}.{$key}" );
66
			$entity->setKey( $key );
67
		}
68
		if ( $value !== null ) {
69
			$entity->setValue( $value );
70
		}
71
		if ( $metric !== null ) {
72
			$entity->setMetric( $metric );
73
		}
74
		// Don't bother buffering a counter update with a delta of zero.
75
		if ( !( $metric === StatsdDataInterface::STATSD_METRIC_COUNT && !$value ) ) {
76
			$this->buffer[] = $entity;
77
		}
78
		return $entity;
79
	}
80
81
	/**
82
	 * @return StatsdData[]
83
	 */
84
	public function getBuffer() {
85
		return $this->buffer;
86
	}
87
}
88