Skip to content

Instantly share code, notes, and snippets.

@abecciu
Created November 2, 2010 19:27
Show Gist options
  • Save abecciu/660145 to your computer and use it in GitHub Desktop.
Save abecciu/660145 to your computer and use it in GitHub Desktop.
nginx patch for newrelic queue wait time updated for nginx 0.8
--- src/http/modules/ngx_http_proxy_module.c.orig 2010-08-03 09:59:14.000000000 -0300
+++ src/http/modules/ngx_http_proxy_module.c 2010-11-02 14:58:16.000000000 -0300
@@ -73,6 +73,8 @@
ngx_uint_t headers_hash_max_size;
ngx_uint_t headers_hash_bucket_size;
++ ngx_flag_t queue_wait_hdrs;
} ngx_http_proxy_loc_conf_t;
@@ -136,6 +138,7 @@
ngx_http_proxy_loc_conf_t *plcf);
#endif
static void ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v);
+static ngx_str_t *queue_wait_headers(const ngx_http_request_t *r);
static ngx_conf_post_t ngx_http_proxy_lowat_post =
@@ -443,6 +446,13 @@
#endif
+ { ngx_string("proxy_queue_wait_headers"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+ ngx_conf_set_flag_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_proxy_loc_conf_t, queue_wait_hdrs),
+ NULL },
+
ngx_null_command
};
@@ -846,6 +856,7 @@
ngx_http_script_engine_t e, le;
ngx_http_proxy_loc_conf_t *plcf;
ngx_http_script_len_code_pt lcode;
+ ngx_str_t *qw_headers = NULL;
u = r->upstream;
@@ -958,6 +969,12 @@
}
}
+ if (plcf->queue_wait_hdrs == 1) {
+ if ( (qw_headers = queue_wait_headers(r)) == NULL) {
+ return NGX_ERROR;
+ }
+ len += qw_headers->len;
+ }
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
@@ -1086,6 +1103,14 @@
}
}
+ if (plcf->queue_wait_hdrs == 1) {
+ b->last = ngx_copy(b->last, qw_headers->data, qw_headers->len);
+ /* this is not necessary since the memory pool is destroyed when
+ * the request ends.
+ * ngx_pfree(r->pool, qw_headers->data);
+ * ngx_pfree(r->pool, qw_headers);
+ */
+ }
/* add "\r\n" at the header end */
*b->last++ = CR; *b->last++ = LF;
@@ -1707,6 +1732,8 @@
conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
+ conf->queue_wait_hdrs = NGX_CONF_UNSET;
+
return conf;
}
@@ -1986,6 +2013,8 @@
}
}
+ ngx_conf_merge_value(conf->queue_wait_hdrs, prev->queue_wait_hdrs, 1);
+
#if (NGX_HTTP_SSL)
if (conf->upstream.ssl == NULL) {
conf->upstream.ssl = prev->upstream.ssl;
@@ -2780,3 +2809,38 @@
v->uri = u->uri;
}
+
+
+static ngx_str_t *
+queue_wait_headers(const ngx_http_request_t *r)
+{
+ struct timeval tv;
+ uint64_t now_usec, req_start_usec, diff;
+ u_char *p;
+ size_t len;
+ ngx_str_t h1 = ngx_string("X-Request-Start: t=");
+ ngx_str_t h2 = ngx_string("X-Request-Read: t=");
+ ngx_str_t *res;
+
+ ngx_gettimeofday(&tv);
+ now_usec = (((uint64_t)tv.tv_sec * 1000 * 1000) + (uint64_t)tv.tv_usec);
+ req_start_usec = (((uint64_t)r->start_sec * 1000 * 1000) +
+ ((uint64_t)r->start_msec * 1000));
+ diff = now_usec - req_start_usec;
+
+ len = h1.len + h2.len + (2*NGX_INT64_LEN) + (2*sizeof("\r\n"));
+ if ( (p = ngx_pnalloc(r->pool, len)) == NULL) {
+ return NULL;
+ }
+ ngx_memzero(p, len);
+ len = ngx_sprintf(p, "%V%L\r\n%V%L\r\n", &h1, now_usec, &h2, diff) - p;
+
+ if ( (res = (ngx_str_t *)ngx_pnalloc(r->pool, sizeof(ngx_str_t))) == NULL) {
+ return NULL;
+ }
+
+ ngx_memzero(res, sizeof(ngx_str_t));
+ res->len = len;
+ res->data = p;
+ return res;
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment