From: Ori Livneh Date: Thu, 21 May 2015 18:07:39 +0000 (-0700) Subject: ProfilerOutputStats: allow a key prefix to be specified X-Git-Tag: 1.31.0-rc.0~11337^2 X-Git-Url: http://git.cyclocoop.org/%22.%28%24lien.?a=commitdiff_plain;h=a70b4c85a8892a62b839581dc96cb9df34ef05b8;p=lhc%2Fweb%2Fwiklou.git ProfilerOutputStats: allow a key prefix to be specified If one wants to nest all metrics emitted by the profiler under a metric namespace, one can now set the 'prefix' param. Task: T66301 Change-Id: I6c52f20e39017f4c818ca6623bb7f48683fc8abc --- diff --git a/includes/profiler/output/ProfilerOutputStats.php b/includes/profiler/output/ProfilerOutputStats.php index a6357935f5..0d7519170e 100644 --- a/includes/profiler/output/ProfilerOutputStats.php +++ b/includes/profiler/output/ProfilerOutputStats.php @@ -31,20 +31,39 @@ */ 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 = str_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 = ''; + } + $contextStats = $this->collector->getContext()->getStats(); foreach ( $stats as $stat ) { - // Sanitize the key - $key = str_replace( '::', '.', $stat['name'] ); - $key = preg_replace( '/[^a-z.]+/i', '_', $key ); - $key = trim( $key, '_.' ); - $key = str_replace( array( '._', '_.' ), '.', $key ); + $key = self::normalizeMetricKey( "{$prefix}.{$stat['name']}" ); // Convert fractional seconds to whole milliseconds $cpu = round( $stat['cpu'] * 1000 );