Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Apache2 module to log request times.
#include "httpd.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h"
#include <time.h>
#include <syslog.h>
/**
* Definitions.
*/
static void register_hooks(apr_pool_t *);
static int hi_handler(request_rec *);
static int hi_handler__log_transaction(request_rec *);
double get_micro_timestamp();
/**
* Static global vars.
*/
static double microtime_start;
/**
* Define the Apache2 module.
*/
module AP_MODULE_DECLARE_DATA hi_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
register_hooks
};
static void register_hooks(apr_pool_t *pool) {
ap_hook_handler(hi_handler, NULL, NULL, APR_HOOK_FIRST);
ap_hook_log_transaction(hi_handler__log_transaction, NULL, NULL, APR_HOOK_LAST);
}
static int hi_handler(request_rec *r) {
microtime_start = get_micro_timestamp();
return DECLINED;
}
static int hi_handler__log_transaction(request_rec *r) {
openlog("mod_hi", 0, 0);
syslog(LOG_NOTICE, "FILE: %s ARGS: %s TIME: %.4f", r->filename, r->args, get_micro_timestamp() - microtime_start);
return OK;
}
double get_micro_timestamp() {
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return 1000.0 * tv.tv_sec + 1e-6f * tv.tv_nsec;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment