You are here

statsd.module in StatsD 7.2

Same filename and directory in other branches
  1. 6 statsd.module
  2. 7 statsd.module

File

statsd.module
View source
<?php

/**
 * Implementation of hook_menu()
 *
 */
function statsd_menu() {
  $items['admin/config/development/statsd'] = array(
    'title' => 'StatsD',
    'description' => 'Settings for statsd logging. StatsD is a Node JS daemon that aggregates statistics for visibility in Graphite. Best suited for compiling arbitrary statistics on various developer implemented metrics, this module can also be used to send all system events (via watchdog calls) to Graphite.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'statsd_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'statsd.admin.inc',
  );
  return $items;
}

// Cannot rely on autoloader because of early watchdogs.
if (!class_exists('StatsD', FALSE)) {
  require_once dirname(__FILE__) . '/includes/statsd.inc';
}

/**
 * Implementation of hook_boot()
 *
 */
function statsd_boot() {
  if (variable_get('statsd_performance_events', TRUE)) {
    drupal_static('statsd_timer', microtime(TRUE));
  }
}

/**
 * Implementation of hook_user_login()
 *
 */
function statsd_user_login(&$edit, $account) {
  if (!variable_get('statsd_user_events', TRUE)) {
    return;
  }
  statsd_call('user_events.successful_login');
}

/**
 * Send failed login attempt
 *
 */
function statsd_user_login_failed($account) {
  if (!variable_get('statsd_user_events', TRUE)) {
    return;
  }
  statsd_call('user_events.failed_login');
}

/**
 * Implementation of hook_exit()
 *
 */
function statsd_exit($destination = NULL) {
  if (variable_get('statsd_user_events', TRUE)) {
    $active_sessions = db_query("SELECT count(*) as num FROM {sessions} WHERE timestamp > UNIX_TIMESTAMP() - 3600")
      ->fetchField();
    statsd_call('user_events.active_sessions', 'gauge', $active_sessions);
    statsd_call('user_events.page_view');
  }
  if (variable_get('statsd_performance_events', TRUE)) {
    $memory = round(memory_get_peak_usage() / 1024 / 1024, 2);
    statsd_call('performance_events.peak_memory', 'gauge', $memory);
    $start =& drupal_static('statsd_timer');

    // hook_boot() may not be called in certain contexts.
    if ($start > 0) {
      $end = microtime(TRUE);
      $time = round(($end - $start) * 1000, 0);
      statsd_call('performance_events.execution_time', 'timing', $time);
    }
  }
}

/**
 * Implementation of hook_watchdog()
 *
 */
function statsd_watchdog($entry) {
  if (strstr($entry['message'], 'Login attempt failed for')) {
    statsd_user_login_failed($entry['user']);
  }
  $enabled = variable_get('statsd_watchdog_events', TRUE);
  $level = variable_get('statsd_watchdog_level', WATCHDOG_WARNING);
  if (!$enabled || $level < $entry['severity']) {
    return;
  }
  $levels = watchdog_severity_levels();
  $data = array(
    sprintf('watchdog.type.%s', $entry['type']),
    sprintf('watchdog.severity.%s', $levels[$entry['severity']]),
  );
  statsd_call($data);
}

/* End of hooks, API functions follow.
---------------------------------------------------------------------- */

/**
 * The generic statsd wrapper. Used for convenience.
 *
 * @param $name
 *   Name of the value you want to track.
 * @param $type
 *   The type of action you want to take with the value.
 * @param $value
 *   The numeric value you wish to pass to statsd.
 *
 */
function statsd_call($name, $type = 'increment', $value = NULL) {

  // In certain situations, hook_watchdog() is called early in the bootstrap
  // process before autoloaded classes are available.
  module_load_include('inc', 'statsd', 'includes/statsd');
  $statsd = statsd_instance();
  switch ($type) {
    case 'increment':
      $statsd
        ->updateStats($name, isset($value) ? $value : 1);
      break;
    case 'decrement':
      $statsd
        ->updateStats($name, isset($value) ? $value : -1);
      break;
    case 'gauge':
      $statsd
        ->gauge($name, $value);
      break;
    case 'timing':
      $statsd
        ->timing($name, $value);
      break;
    default:
      watchdog('statsd', 'Unknown method called for statsd: %type', array(
        '%type' => $type,
      ), WATCHDOG_WARNING);
      break;
  }
}

/**
* Static instance function for StatsD.
*
* @param $parameters
*    Set of default parameters that can be used to make an unique statsd object
* @return StatsD
*   Return the same instance on each call, unless drupal_static() is reset.
*/
function statsd_instance($parameters = array()) {

  // since we could have different sets of parameters across the whole site
  // we want to make sure we have a different static object per set of unique
  // parameters.
  $parameters_md5 = md5(serialize($parameters));
  $static_name = 'statsd_' . $parameters_md5;
  $statsd =& drupal_static($static_name);
  if (!isset($statsd)) {
    $parameter_defaults = StatsD::getDefaultProperties();
    $parameters = array_merge($parameters_defaults, $parameters);
    $statsd = new StatsD($parameters);
  }
  return $statsd;
}

Functions

Namesort descending Description
statsd_boot Implementation of hook_boot()
statsd_call The generic statsd wrapper. Used for convenience.
statsd_exit Implementation of hook_exit()
statsd_instance Static instance function for StatsD.
statsd_menu Implementation of hook_menu()
statsd_user_login Implementation of hook_user_login()
statsd_user_login_failed Send failed login attempt
statsd_watchdog Implementation of hook_watchdog()