[PATCH] add $unixtime to http variables

Roberto De Ioris roberto at unbit.it
Mon Sep 20 21:32:44 MSD 2010


This is a patch to add $unixtime to the http variables.

It simply contains the output of time(NULL)

It could be useful to measure the time between the send
of a request to an upstream module and the time the application
server process it.

Example:

uwsgi_param UWSGI_TIME $unixtime;

Then in your app:

if int(time.time()) - int(env['UWSGI_TIME']):
    print "*** SYSTEM IS RESPONDING SLOWLY ***"

I do not know if it is better to name it $time instead of $unixtime...


--- src/http/ngx_http_variables.c	2010-06-23 17:31:33.000000000 +0200
+++ ../nginx-ok/src/http/ngx_http_variables.c	2010-09-20
19:24:57.000000000 +0200
@@ -93,6 +93,9 @@
 static ngx_int_t ngx_http_variable_pid(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);

+static ngx_int_t ngx_http_variable_unixtime(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
+
 /*
  * TODO:
  *     Apache CGI: AUTH_TYPE, PATH_INFO (null), PATH_TRANSLATED
@@ -254,6 +257,9 @@
     { ngx_string("pid"), NULL, ngx_http_variable_pid,
       0, 0, 0 },

+    { ngx_string("unixtime"), NULL, ngx_http_variable_unixtime,
+      0, 0, 0 },
+
     { ngx_null_string, NULL, NULL, 0, 0, 0 }
 };

@@ -1640,6 +1646,26 @@


 static ngx_int_t
+ngx_http_variable_unixtime(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data)
+{
+    u_char  *p;
+
+    p = ngx_pnalloc(r->pool, NGX_INT64_LEN);
+    if (p == NULL) {
+        return NGX_ERROR;
+    }
+
+    v->len = ngx_sprintf(p, "%T", time(NULL)) - p;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->data = p;
+
+    return NGX_OK;
+}
+
+static ngx_int_t
 ngx_http_variable_pid(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data)
 {


-- 
Roberto De Ioris
http://unbit.it



More information about the nginx mailing list