Skip to content

Instantly share code, notes, and snippets.

@michaeldauria
Created June 7, 2012 17:16
Show Gist options
  • Save michaeldauria/2890138 to your computer and use it in GitHub Desktop.
Save michaeldauria/2890138 to your computer and use it in GitHub Desktop.
Add a 'start_time' variable to nginx 1.2.1 to support an X-REQUEST-START header. This header is used by New Relic RPM to record queue time.
--- src/http/ngx_http_variables.c.orig 2012-06-07 12:47:34.000000000 -0400
+++ src/http/ngx_http_variables.c 2012-06-07 12:55:51.000000000 -0400
@@ -99,6 +99,8 @@
ngx_http_variable_value_t *v, uintptr_t data);
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_start_time(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
/*
* TODO:
@@ -263,6 +265,9 @@
{ ngx_string("pid"), NULL, ngx_http_variable_pid,
0, 0, 0 },
+ { ngx_string("start_time"), NULL, ngx_http_variable_start_time,
+ 0, 0, 0 },
+
#if (NGX_HAVE_TCP_INFO)
{ ngx_string("tcpinfo_rtt"), NULL, ngx_http_variable_tcpinfo,
0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
@@ -1796,6 +1801,27 @@
return NGX_OK;
}
+static ngx_int_t
+ngx_http_variable_start_time(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;
+ }
+
+ uint64_t usec = (((uint64_t)r->start_sec * 1000 * 1000) + ((uint64_t)r->start_msec * 1000));
+
+ v->len = ngx_sprintf(p, "%L", usec) - p;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = p;
+
+ return NGX_OK;
+}
void *
ngx_http_map_find(ngx_http_request_t *r, ngx_http_map_t *map, ngx_str_t *match)
@agibralter
Copy link

Unfortunate that there's no way to achieve this without a patch...

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