From: Ori Livneh Date: Mon, 13 Jul 2015 18:26:27 +0000 (-0700) Subject: Move StatsD key normalization from ProfilerOutputStats to BufferingStatsdDataFactory X-Git-Tag: 1.31.0-rc.0~10786 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_aide%28?a=commitdiff_plain;h=0a25d776adb79d8c7d2cee78c4edea79a65bf18d;p=lhc%2Fweb%2Fwiklou.git Move StatsD key normalization from ProfilerOutputStats to BufferingStatsdDataFactory I'm not sure why I stuck `normalizeMetricKey' in ProfilerOutputStats, because the transformation it applies are suitable for converting any arbitrary string into a StatsD-safe metric key. This patch moves the method to BufferingStatsdDataFactory, which ensures it applies to all metrics logged within MediaWiki, and not just the Profiler. Supercedes If0237cdd0d. Change-Id: I496ed748000d28f5399fee6e3cc271a1f68bd058 --- diff --git a/includes/libs/BufferingStatsdDataFactory.php b/includes/libs/BufferingStatsdDataFactory.php index 0caf90b39c..192b119faf 100644 --- a/includes/libs/BufferingStatsdDataFactory.php +++ b/includes/libs/BufferingStatsdDataFactory.php @@ -39,11 +39,28 @@ class BufferingStatsdDataFactory extends StatsdDataFactory { $this->prefix = $prefix; } + /** + * Normalize a metric key for StatsD + * + * Replace occurences of '::' with dots and any other non-alphabetic + * characters with underscores. Combine runs of dots or underscores. + * Then trim leading or trailing dots or underscores. + * + * @param string $key + * @since 1.26 + */ + private static function normalizeMetricKey( $key ) { + $key = preg_replace( '/[:.]+/', '.', $key ); + $key = preg_replace( '/[^a-z.]+/i', '_', $key ); + $key = trim( $key, '_.' ); + return str_replace( array( '._', '_.' ), '.', $key ); + } + public function produceStatsdData( $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT ) { $entity = $this->produceStatsdDataEntity(); if ( $key !== null ) { - $prefixedKey = ltrim( $this->prefix . '.' . $key, '.' ); - $entity->setKey( $prefixedKey ); + $key = self::normalizeMetricKey( "{$this->prefix}.{$key}" ); + $entity->setKey( $key ); } if ( $value !== null ) { $entity->setValue( $value ); diff --git a/includes/profiler/output/ProfilerOutputStats.php b/includes/profiler/output/ProfilerOutputStats.php index d816a01fe1..52aa54acca 100644 --- a/includes/profiler/output/ProfilerOutputStats.php +++ b/includes/profiler/output/ProfilerOutputStats.php @@ -31,39 +31,17 @@ */ class ProfilerOutputStats extends ProfilerOutput { - /** - * Normalize a metric key for StatsD - * - * Replace occurences of '::' with dots and any other non-alphabetic - * characters with underscores. Combine runs of dots or underscores. - * Then trim leading or trailing dots or underscores. - * - * @param string $key - * @since 1.26 - */ - private static function normalizeMetricKey( $key ) { - $key = preg_replace( '/[:.]+/', '.', $key ); - $key = preg_replace( '/[^a-z.]+/i', '_', $key ); - $key = trim( $key, '_.' ); - return str_replace( array( '._', '_.' ), '.', $key ); - } - /** * Flush profiling data to the current profiling context's stats buffer. * * @param array $stats */ public function log( array $stats ) { - if ( isset( $this->params['prefix'] ) ) { - $prefix = self::normalizeMetricKey( $this->params['prefix'] ); - } else { - $prefix = ''; - } - + $prefix = isset( $this->params['prefix'] ) ? $this->params['prefix'] : ''; $contextStats = $this->collector->getContext()->getStats(); foreach ( $stats as $stat ) { - $key = self::normalizeMetricKey( "{$prefix}.{$stat['name']}" ); + $key = "{$prefix}.{$stat['name']}"; // Convert fractional seconds to whole milliseconds $cpu = round( $stat['cpu'] * 1000 );