From 814d26f984609a081711213e3cb435087e7b2c2e Mon Sep 17 00:00:00 2001 From: Adam Greig Date: Fri, 13 Jul 2012 00:41:27 +0100 Subject: [PATCH] instrument some php --- predict/ajax.php | 10 +++++++ predict/includes/functions.inc.php | 1 + predict/includes/statsd.php | 47 ++++++++++++++++++++++++++++++ predict/index.php | 4 +++ 4 files changed, 62 insertions(+) create mode 100644 predict/includes/statsd.php diff --git a/predict/ajax.php b/predict/ajax.php index bd2b1a1..78f3f4c 100644 --- a/predict/ajax.php +++ b/predict/ajax.php @@ -2,6 +2,8 @@ require_once("includes/functions.inc.php"); require_once("includes/config.inc.php"); +$stats = new StatsD(); + $action = $_GET['action']; $software_available = array("gfs", "gfs_hd"); @@ -19,6 +21,7 @@ case "getCSV": } $returned = json_encode($data); echo $returned; + $stats->counting('habhub.predictor.php.get_csv'); break; case "JSONexists": @@ -58,6 +61,7 @@ case "getModelByUUID": $pred_model = array(); if ( !file_exists(PREDS_PATH . $uuid . "/" . SCENARIO_FILE ) ) { $pred_model['valid'] = false; + $stats->counting('habhub.predictor.php.couldnt_get_by_uuid'); } else { // populate the array, JSON encode it and return $pred_model = parse_ini_file(PREDS_PATH . $uuid . "/" . SCENARIO_FILE); @@ -67,6 +71,7 @@ case "getModelByUUID": $pred_model['valid'] = false; } $pred_model['uuid'] = $uuid; + $stats->counting('habhub.predictor.php.got_by_uuid'); } echo json_encode($pred_model); break; @@ -83,6 +88,7 @@ case "submitForm": $json_return['error'] = "Server couldn't make a model from the form data"; echo json_encode($json_return); + $stats->counter('habhub.predictor.php.form_error'); break; } @@ -91,6 +97,7 @@ case "submitForm": if ( !$verify_dump['valid'] ) { $json_return['error'] = $verify_dump['msg']; echo json_encode($json_return); + $stats->counter('habhub.predictor.php.invalid_model') break; } @@ -98,6 +105,7 @@ case "submitForm": if ( !$pred_model['uuid'] = makesha1hash($pred_model) ) { $json_return['error'] = "Couldn't make the SHA1 hash"; echo json_encode($json_return); + $stats->counter('habhub.predictor.php.unhashable'); break; } @@ -106,10 +114,12 @@ case "submitForm": $json_return['valid'] = "true"; $json_return['uuid'] = $pred_model['uuid']; $json_return['timestamp'] = $pred_model['timestamp']; + $stats->counting('habhub.predictor.php.prediction_run'); } else { $json_return['error'] = "The form submit function was called without any data"; + $stats->counting('habhub.predictor.php.no_form_data'); } echo json_encode($json_return); diff --git a/predict/includes/functions.inc.php b/predict/includes/functions.inc.php index 1073af0..9a7b409 100644 --- a/predict/includes/functions.inc.php +++ b/predict/includes/functions.inc.php @@ -6,6 +6,7 @@ */ require_once("config.inc.php"); +require_once("statsd.php"); // Given a POST array, create a scenario model function createModel($post_array) { diff --git a/predict/includes/statsd.php b/predict/includes/statsd.php new file mode 100644 index 0000000..ac65594 --- /dev/null +++ b/predict/includes/statsd.php @@ -0,0 +1,47 @@ + + * https://raw.github.com/seejohnrun/php-statsd/master/libraries/statsd.php + */ +class StatsD { + + private $host, $port; + + // Instantiate a new client + public function __construct($host = 'localhost', $port = 8125) { + $this->host = $host; + $this->port = $port; + } + + // Record timing + public function timing($key, $time, $rate = 1) { + $this->send("$key:$time|ms", $rate); + } + + // Time something + public function time_this($key, $callback, $rate = 1) { + $begin = microtime(true); + $callback(); + $time = floor((microtime(true) - $begin) * 1000); + // And record + $this->timing($key, $time, $rate); + } + + // Record counting + public function counting($key, $amount = 1, $rate = 1) { + $this->send("$key:$amount|c", $rate); + } + + // Send + private function send($value, $rate) { + $fp = fsockopen('udp://' . $this->host, $this->port, $errno, $errstr); + // Will show warning if not opened, and return false + if ($fp) { + fwrite($fp, "$value|@$rate"); + fclose($fp); + } + } + +} diff --git a/predict/index.php b/predict/index.php index 5ff5bc8..50967af 100644 --- a/predict/index.php +++ b/predict/index.php @@ -14,6 +14,10 @@ require_once("includes/config.inc.php"); require_once("includes/functions.inc.php"); + +$stats = new StatsD(); +$stats->counting('hits'); + // Get the time for pre-populating the form $time = time() + 3600; ?>