Overview

Packages

  • awl
    • caldav-client-v2
    • RRule
  • davical
    • authentication
      • drivers
    • caldav
    • DAViCalSession
    • DAVTicket
    • external-bind
    • feed
    • HTTPAuthSession
    • iSchedule
    • iSchedule-POST
    • logging
    • metrics
    • Principal
    • propfind
    • PublicSession
    • Request
    • Resource
    • tzservice
  • None
  • PHP

Functions

  • print_metric
  • Overview
  • Package
  • Function
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * DAViCal - metrics page for Prometheus
  4:  *
  5:  * @package   davical
  6:  * @subpackage   metrics
  7:  * @author    Andrew McMillan <andrew@mcmillan.net.nz>
  8:  * @copyright Andrew McMillan <andrew@mcmillan.net.nz>
  9:  * @license   http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
 10:  */
 11: 
 12: header("Content-type: text/plain; version=0.4.0");
 13: 
 14: require_once('./always.php');
 15: 
 16: // If necessary, validate they are coming from an allowed address
 17: if ( isset($c->metrics_collectors) && !in_array($_SERVER['REMOTE_ADDR'], $c->metrics_collectors) ) {
 18:   echo "Nope.";
 19:   exit(0);
 20: }
 21: 
 22: // If necessary, validate that they are coming in as an authorized user
 23: if ( isset($c->metrics_require_user) ) {
 24:   require_once('HTTPAuthSession.php');
 25:   $session = new HTTPAuthSession();
 26:   if ( $session->username != $c->metrics_require_user ) {
 27:     $session->AuthFailedResponse();
 28:     echo "Nope.";
 29:     exit(0);
 30:   }
 31: }
 32: 
 33: // Validate that the metrics are actually turned on!
 34: if ( !isset($c->metrics_style) || $c->metrics_style === false ) {
 35:   echo "Metrics are not enabled.";
 36:   exit(0);
 37: }
 38: 
 39: // Helper function to ensure we get the metric format consistent
 40: function print_metric( $name, $qualifiers, $value ) {
 41:   print $name;
 42:   if ( !empty($qualifiers) ) {
 43:     print '{';
 44:     $continuation = '';
 45:     foreach( $qualifiers AS $k => $v ) {
 46:       if ( $continuation == '' ) {
 47:         $continuation = ',';
 48:       } else {
 49:         print $continuation;
 50:       }
 51:       printf( '%s="%s"', $k, $v);
 52:     }
 53:     print '}';
 54:   }
 55:   echo ' ', $value, "\n";
 56: }
 57: 
 58: 
 59: // If they want 'both' or 'all' or something then that's what they will get
 60: // If they don't want counters, they must want to use memcache!
 61: if ( $c->metrics_style != 'counters' ) {
 62:   // These are the preferred metrics, which include some internal details
 63:   // of the request processing.
 64:   include_once('AwlCache.php');
 65:   $cache = getCacheInstance();
 66: 
 67:   $index = unserialize($cache->get('metrics', 'index'));
 68:   print "# HELP caldav_request_status The DAViCal requests broken down by HTTP method and response status\n";
 69:   print "# TYPE caldav_request_status counter\n";
 70:   foreach( $index['methods'] AS $method => $ignored ) {
 71:     foreach( $index['statuses'] AS $status => $ignored ) {
 72:       $count = $cache->get('metrics', $method.':'.$status );
 73:       if ( $count !== false ) {
 74:         print_metric("caldav_request_status", array('method'=>$method, 'status'=>$status), $count);
 75:       }
 76:     }
 77:   }
 78: 
 79:   print "\n";
 80:   print "# HELP caldav_response_bytes The DAViCal response size by HTTP method\n";
 81:   print "# TYPE caldav_response_bytes counter\n";
 82:   foreach( $index['methods'] AS $method => $ignored ) {
 83:     $count = $cache->get('metrics', $method.':size');
 84:     print_metric("caldav_request_bytes", array('method'=>$method), $count);
 85:   }
 86: 
 87:   $timings = array('script', 'query', 'flush');
 88:   print "\n";
 89:   print "# HELP caldav_request_microseconds The DAViCal response time taken in general, in queries, and in flushing buffers\n";
 90:   print "# TYPE caldav_request_microseconds counter\n";
 91:   foreach( $index['methods'] AS $method => $ignored ) {
 92:     foreach( $timings AS $timing ) {
 93:       $count = $cache->get('metrics', $method.':'.$timing.'_time');
 94:       print_metric("caldav_request_microseconds", array('method'=>$method, 'timing'=>$timing), $count);
 95:     }
 96:   }
 97: }
 98: 
 99: // If they don't want memcache, they must want to use counters!
100: if ( $c->metrics_style != 'memcache' ) {
101:   // These are more basic metrics.  Just counts of requests, by type.
102:   $sql = <<<QUERY
103: SELECT
104:   (SELECT last_value FROM metrics_count_get) AS get_count,
105:   (SELECT last_value FROM metrics_count_put) AS put_count,
106:   (SELECT last_value FROM metrics_count_propfind) AS propfind_count,
107:   (SELECT last_value FROM metrics_count_proppatch) AS proppatch_count,
108:   (SELECT last_value FROM metrics_count_report) AS report_count,
109:   (SELECT last_value FROM metrics_count_head) AS head_count,
110:   (SELECT last_value FROM metrics_count_options) AS options_count,
111:   (SELECT last_value FROM metrics_count_post) AS post_count,
112:   (SELECT last_value FROM metrics_count_mkcalendar) AS mkcalendar_count,
113:   (SELECT last_value FROM metrics_count_mkcol) AS mkcol_count,
114:   (SELECT last_value FROM metrics_count_delete) AS delete_count,
115:   (SELECT last_value FROM metrics_count_move) AS move_count,
116:   (SELECT last_value FROM metrics_count_acl) AS acl_count,
117:   (SELECT last_value FROM metrics_count_lock) AS lock_count,
118:   (SELECT last_value FROM metrics_count_unlock) AS unlock_count,
119:   (SELECT last_value FROM metrics_count_mkticket) AS mkticket_count,
120:   (SELECT last_value FROM metrics_count_delticket) AS delticket_count,
121:   (SELECT last_value FROM metrics_count_bind) AS bind_count,
122:   (SELECT last_value FROM metrics_count_unknown) AS unknown_count
123: QUERY;
124: 
125:   $qry = new AwlQuery($sql);
126:   $result = $qry->Exec("metrics", __LINE__ , __FILE__);
127:   $row = (array) $qry->Fetch();
128:   print "\n";
129:   print "# HELP caldav_request_count The DAViCal requests broken down by HTTP method (get, put, propfind, etc.).\n";
130:   print "# TYPE caldav_request_count counter\n";
131:   foreach ($row as $k => $v) {
132:     print_metric("caldav_request_count", array( "method" => str_replace("_count", "", $k)), $v);
133:   }
134: }
135: 
136: print "\n";
137: print "# HELP davical_up Are the servers up.\n";
138: print "# TYPE davical_up gauge\n";
139: print_metric("davical_up", array('server'=>$c->sysabbr), 1);
140: 
141: if ( function_exists('memory_get_usage') ) {
142:   print "\n";
143:   print "# HELP davical_process_memory How much memory is this process using.\n";
144:   print "# TYPE davical_process_memory gauge\n";
145:   print_metric("davical_process_memory", array("pid" => getmypid(), 'type' => 'curr'), memory_get_usage());
146:   print_metric("davical_process_memory", array("pid" => getmypid(), 'type' => 'peak'), memory_get_peak_usage());
147: }
148: 
DAViCal API documentation generated by ApiGen 2.8.0