-
-
Save SteveEdson/5774598 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sends statistics to the stats daemon over UDP | |
* | |
**/ | |
class StatsD { | |
/** | |
* Log timing information | |
* | |
* @param string $stats The metric to in log timing info for. | |
* @param float $time The ellapsed time (ms) to log | |
* @param float|1 $sampleRate the rate (0-1) for sampling. | |
**/ | |
public static function timing($stat, $time, $sampleRate=1) { | |
StatsD::send(array($stat => "$time|ms"), $sampleRate); | |
} | |
/** | |
* Increments one or more stats counters | |
* | |
* @param string|array $stats The metric(s) to increment. | |
* @param float|1 $sampleRate the rate (0-1) for sampling. | |
* @return boolean | |
**/ | |
public static function increment($stats, $sampleRate=1) { | |
StatsD::updateStats($stats, 1, $sampleRate); | |
} | |
/** | |
* Decrements one or more stats counters. | |
* | |
* @param string|array $stats The metric(s) to decrement. | |
* @param float|1 $sampleRate the rate (0-1) for sampling. | |
* @return boolean | |
**/ | |
public static function decrement($stats, $sampleRate=1) { | |
StatsD::updateStats($stats, -1, $sampleRate); | |
} | |
/** | |
* Updates one or more stats counters by arbitrary amounts. | |
* | |
* @param string|array $stats The metric(s) to update. Should be either a string or array of metrics. | |
* @param int|1 $delta The amount to increment/decrement each metric by. | |
* @param float|1 $sampleRate the rate (0-1) for sampling. | |
* @return boolean | |
**/ | |
public static function updateStats($stats, $delta=1, $sampleRate=1) { | |
if (!is_array($stats)) { $stats = array($stats); } | |
$data = array(); | |
foreach($stats as $stat) { | |
$data[$stat] = "$delta|c"; | |
} | |
StatsD::send($data, $sampleRate); | |
} | |
/* | |
* Squirt the metrics over UDP | |
**/ | |
public static function send($data, $sampleRate=1) { | |
// sampling | |
$sampledData = array(); | |
if ($sampleRate < 1) { | |
foreach ($data as $stat => $value) { | |
if ((mt_rand() / mt_getrandmax()) <= $sampleRate) { | |
$sampledData[$stat] = "$value|@$sampleRate"; | |
} | |
} | |
} else { | |
$sampledData = $data; | |
} | |
if (empty($sampledData)) { return; } | |
// Wrap this in a try/catch - failures in any of this should be silently ignored | |
// @TODO We're using localhost/8125 here because that's what statsd.conf is set to | |
// we should probably try to read statsd.conf or some other config file but convention | |
// is good enough for now, esp. since we catch and fail silently. Up to the dev to | |
// make sure statsd is running | |
try { | |
$fp = fsockopen("udp://localhost", 8125, $errno, $errstr); | |
if (! $fp) { return; } | |
foreach ($sampledData as $stat => $value) { | |
fwrite($fp, "$stat:$value"); | |
} | |
fclose($fp); | |
} catch (Exception $e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment