Skip to content

Instantly share code, notes, and snippets.

@mtourne
Created December 3, 2012 20:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtourne/4197759 to your computer and use it in GitHub Desktop.
Save mtourne/4197759 to your computer and use it in GitHub Desktop.
Expose a $request_time in msec to Nginx core
/* inspired from ngx_http_log_module.c:ngx_http_log_request_time() */
static ngx_int_t
ngx_http_request_time(ngx_http_request_t *r,
ngx_http_variable_value_t *v,
uintptr_t data) {
ngx_time_t *tp;
ngx_msec_int_t ms;
tp = ngx_timeofday();
ms = (ngx_msec_int_t)
((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
ms = ngx_max(ms, 0);
v->data = ngx_pnalloc(r->pool, NGX_TIME_T_LEN + 4);
if (v->data == NULL) {
return NGX_ERROR;
}
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->len = ngx_snprintf(v->data, NGX_TIME_T_LEN + 4,
"%T.%03M", ms / 1000, ms % 1000) - v->data;
return NGX_OK;
}
@fmm1977
Copy link

fmm1977 commented Sep 24, 2020

Hi, I was wondering how to install this in Nginx. Thanks!

@mtourne
Copy link
Author

mtourne commented Oct 20, 2020

you would probably need to make this part of a module, but if I were you I would explore if this data isn't already accessible via the Lua api found in OpenResty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment