Keep backend param to search API as long as there's a backend
[lhc/web/wiklou.git] / includes / StatCounter.php
1 <?php
2 /**
3 * @defgroup StatCounter StatCounter
4 *
5 * StatCounter is used to increment arbitrary keys for profiling reasons.
6 * The key/values are persisted in several possible ways (see $wgStatsMethod).
7 */
8
9 /**
10 * Aggregator for wfIncrStats() that batches updates per request.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @ingroup StatCounter
29 * @author Aaron Schulz
30 */
31
32 /**
33 * Aggregator for wfIncrStats() that batches updates per request.
34 * This avoids spamming the collector many times for the same key.
35 *
36 * @ingroup StatCounter
37 */
38 class StatCounter {
39 /** @var array */
40 protected $deltas = array(); // (key => count)
41
42 /** @var Config */
43 protected $config;
44
45 protected function __construct( Config $config ) {
46 $this->config = $config;
47 }
48
49 /**
50 * @return StatCounter
51 */
52 public static function singleton() {
53 static $instance = null;
54 if ( !$instance ) {
55 $instance = new self(
56 ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
57 );
58 }
59 return $instance;
60 }
61
62 /**
63 * Increment a key by delta $count
64 *
65 * @param string $key
66 * @param int $count
67 * @return void
68 */
69 public function incr( $key, $count = 1 ) {
70 $this->deltas[$key] = isset( $this->deltas[$key] ) ? $this->deltas[$key] : 0;
71 $this->deltas[$key] += $count;
72 if ( PHP_SAPI === 'cli' ) {
73 $this->flush();
74 }
75 }
76
77 /**
78 * Flush all pending deltas to persistent storage
79 *
80 * @return void
81 */
82 public function flush() {
83 $statsMethod = $this->config->get( 'StatsMethod' );
84 $deltas = array_filter( $this->deltas ); // remove 0 valued entries
85 if ( $statsMethod === 'udp' ) {
86 $this->sendDeltasUDP( $deltas );
87 } elseif ( $statsMethod === 'cache' ) {
88 $this->sendDeltasMemc( $deltas );
89 } else {
90 // disabled
91 }
92 $this->deltas = array();
93 }
94
95 /**
96 * @param array $deltas
97 * @return void
98 */
99 protected function sendDeltasUDP( array $deltas ) {
100 $aggregateStatsID = $this->config->get( 'AggregateStatsID' );
101 $id = strlen( $aggregateStatsID ) ? $aggregateStatsID : wfWikiID();
102
103 $lines = array();
104 foreach ( $deltas as $key => $count ) {
105 $lines[] = sprintf( $this->config->get( 'StatsFormatString' ), $id, $count, $key );
106 }
107
108 if ( count( $lines ) ) {
109 static $socket = null;
110 if ( !$socket ) {
111 $socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
112 }
113 $packet = '';
114 $packets = array();
115 foreach ( $lines as $line ) {
116 if ( ( strlen( $packet ) + strlen( $line ) ) > 1450 ) {
117 $packets[] = $packet;
118 $packet = '';
119 }
120 $packet .= $line;
121 }
122 if ( $packet != '' ) {
123 $packets[] = $packet;
124 }
125 foreach ( $packets as $packet ) {
126 wfSuppressWarnings();
127 socket_sendto(
128 $socket,
129 $packet,
130 strlen( $packet ),
131 0,
132 $this->config->get( 'UDPProfilerHost' ),
133 $this->config->get( 'UDPProfilerPort' )
134 );
135 wfRestoreWarnings();
136 }
137 }
138 }
139
140 /**
141 * @param array $deltas
142 * @return void
143 */
144 protected function sendDeltasMemc( array $deltas ) {
145 global $wgMemc;
146
147 foreach ( $deltas as $key => $count ) {
148 $ckey = wfMemcKey( 'stats', $key );
149 if ( $wgMemc->incr( $ckey, $count ) === null ) {
150 $wgMemc->add( $ckey, $count );
151 }
152 }
153 }
154 }