[PATCH] add $unixtime to http variables

Maxim Dounin mdounin at mdounin.ru
Tue Sep 21 15:25:38 MSD 2010


Hello!

On Mon, Sep 20, 2010 at 07:32:44PM +0200, Roberto De Ioris wrote:

> 
> 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 ***"

This won't work as you expect since request to upstream is created 
once and used when sending request to multiple upstream servers 
(due to proxy_next_upstream/fastcgi_next_upstream/...).  Logging 
$upstream_response_time would be better idea.

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

If you just need current system time as a variable - I would 
recommend moving $msec from log module to generic variables 
instead.  Or, probably, duplicating it - as log variable will be 
more efficient during logging than generic one.

> 
> 
> --- 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);

NGX_TIME_T_LEN should be here.

> +    if (p == NULL) {
> +        return NGX_ERROR;
> +    }
> +
> +    v->len = ngx_sprintf(p, "%T", time(NULL)) - p;

As you were already said - don't use time().  Use ngx_getimeofday() 
instead.

> +    v->valid = 1;
> +    v->no_cacheable = 0;

If you want to print current time - you may want to mark this 
variable as non-cacheable (here and in definition via 
NGX_HTTP_VAR_NOCACHEABLE flag).

> +    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)
>  {

Maxim Dounin



More information about the nginx mailing list