Skip to content

Instantly share code, notes, and snippets.

@lawrencepit
Created December 8, 2012 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawrencepit/4238742 to your computer and use it in GitHub Desktop.
Save lawrencepit/4238742 to your computer and use it in GitHub Desktop.
nginx variables $start_msec and $msec
From 2598ef9407d47eadbb4aaed329f3206082067fa8 Mon Sep 17 00:00:00 2001
From: Lawrence Pit <lawrence.pit@gmail.com>
Date: Sat, 1 Dec 2012 17:03:22 +1100
Subject: [PATCH] Feature: $start_msec and $msec http variables, useful for
setting in proxy headers before sending request to backend
servers so processing and queue times can be measured.
---
src/http/ngx_http_variables.c | 55 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+)
diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
index b0949c7..3fbbd2c 100644
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -108,6 +108,10 @@ static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r,
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_msec(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_msec(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
/*
* TODO:
@@ -285,6 +289,12 @@ static ngx_http_variable_t ngx_http_core_variables[] = {
{ ngx_string("pid"), NULL, ngx_http_variable_pid,
0, 0, 0 },
+ { ngx_string("start_msec"), NULL, ngx_http_variable_start_msec,
+ 0, 0, 0 },
+
+ { ngx_string("msec"), NULL, ngx_http_variable_msec,
+ 0, 0, 0 },
+
#if (NGX_HAVE_TCP_INFO)
{ ngx_string("tcpinfo_rtt"), NULL, ngx_http_variable_tcpinfo,
0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
@@ -1915,6 +1925,51 @@ ngx_http_variable_pid(ngx_http_request_t *r,
}
+static ngx_int_t
+ngx_http_variable_start_msec(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.%03M", r->start_sec, r->start_msec) - 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_msec(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ u_char *p;
+ ngx_time_t *tp;
+
+ p = ngx_pnalloc(r->pool, NGX_TIME_T_LEN + 4);
+ if (p == NULL) {
+ return NGX_ERROR;
+ }
+
+ tp = ngx_timeofday();
+
+ v->len = ngx_sprintf(p, "%T.%03M", tp->sec, tp->msec) - 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)
{
--
1.7.11.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment