Created
January 24, 2022 21:01
-
-
Save mundry/a7c77c59da6c68604d18e747331b1b0d to your computer and use it in GitHub Desktop.
Patch to add `$time_iso8601_ms` to nginx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 697b506f5b23269f2ffadf29f5f93adcede094b1 Mon Sep 17 00:00:00 2001 | |
From: mundry <[email protected]> | |
Date: Fri, 28 Oct 2016 20:36:30 +0200 | |
Subject: [PATCH 1/1] Introduce access_log variable time_iso8601_ms. | |
A new variable $time_iso8601_ms has been introduced to the http and | |
stream log modules to log the request time in ISO 8601 format with | |
millisecond precision. | |
--- | |
src/core/ngx_times.c | 16 +++++++++++++++- | |
src/core/ngx_times.h | 1 + | |
src/http/modules/ngx_http_log_module.c | 11 +++++++++++ | |
src/http/ngx_http_variables.c | 29 +++++++++++++++++++++++++++++ | |
src/stream/ngx_stream_variables.c | 29 +++++++++++++++++++++++++++++ | |
5 files changed, 85 insertions(+), 1 deletion(-) | |
diff --git a/src/core/ngx_times.c b/src/core/ngx_times.c | |
index 843314a..559837e 100644 | |
--- a/src/core/ngx_times.c | |
+++ b/src/core/ngx_times.c | |
@@ -29,6 +29,7 @@ volatile ngx_str_t ngx_cached_err_log_time; | |
volatile ngx_str_t ngx_cached_http_time; | |
volatile ngx_str_t ngx_cached_http_log_time; | |
volatile ngx_str_t ngx_cached_http_log_iso8601; | |
+volatile ngx_str_t ngx_cached_http_log_iso8601_ms; | |
volatile ngx_str_t ngx_cached_syslog_time; | |
#if !(NGX_WIN32) | |
@@ -51,6 +52,8 @@ static u_char cached_http_log_time[NGX_TIME_SLOTS] | |
[sizeof("28/Sep/1970:12:00:00 +0600")]; | |
static u_char cached_http_log_iso8601[NGX_TIME_SLOTS] | |
[sizeof("1970-09-28T12:00:00+06:00")]; | |
+static u_char cached_http_log_iso8601_ms[NGX_TIME_SLOTS] | |
+ [sizeof("1970-09-28T12:00:00.000+06:00")]; | |
static u_char cached_syslog_time[NGX_TIME_SLOTS] | |
[sizeof("Sep 28 12:00:00")]; | |
@@ -66,6 +69,7 @@ ngx_time_init(void) | |
ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1; | |
ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1; | |
ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1; | |
+ ngx_cached_http_log_iso8601_ms.len = sizeof("1970-09-28T12:00:00.000+06:00") - 1; | |
ngx_cached_syslog_time.len = sizeof("Sep 28 12:00:00") - 1; | |
ngx_cached_time = &cached_time[0]; | |
@@ -77,7 +81,7 @@ ngx_time_init(void) | |
void | |
ngx_time_update(void) | |
{ | |
- u_char *p0, *p1, *p2, *p3, *p4; | |
+ u_char *p0, *p1, *p2, *p3, *p4, *p5; | |
ngx_tm_t tm, gmt; | |
time_t sec; | |
ngx_uint_t msec; | |
@@ -176,6 +180,15 @@ ngx_time_update(void) | |
months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday, | |
tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec); | |
+ p5 = &cached_http_log_iso8601_ms[slot][0]; | |
+ | |
+ (void) ngx_sprintf(p5, "%4d-%02d-%02dT%02d:%02d:%02d.%03d%c%02i:%02i", | |
+ tm.ngx_tm_year, tm.ngx_tm_mon, | |
+ tm.ngx_tm_mday, tm.ngx_tm_hour, | |
+ tm.ngx_tm_min, tm.ngx_tm_sec, msec, | |
+ tp->gmtoff < 0 ? '-' : '+', | |
+ ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60)); | |
+ | |
ngx_memory_barrier(); | |
ngx_cached_time = tp; | |
@@ -183,6 +196,7 @@ ngx_time_update(void) | |
ngx_cached_err_log_time.data = p1; | |
ngx_cached_http_log_time.data = p2; | |
ngx_cached_http_log_iso8601.data = p3; | |
+ ngx_cached_http_log_iso8601_ms.data = p5; | |
ngx_cached_syslog_time.data = p4; | |
ngx_unlock(&ngx_time_lock); | |
diff --git a/src/core/ngx_times.h b/src/core/ngx_times.h | |
index 94aedcd..108dd66 100644 | |
--- a/src/core/ngx_times.h | |
+++ b/src/core/ngx_times.h | |
@@ -40,6 +40,7 @@ extern volatile ngx_str_t ngx_cached_err_log_time; | |
extern volatile ngx_str_t ngx_cached_http_time; | |
extern volatile ngx_str_t ngx_cached_http_log_time; | |
extern volatile ngx_str_t ngx_cached_http_log_iso8601; | |
+extern volatile ngx_str_t ngx_cached_http_log_iso8601_ms; | |
extern volatile ngx_str_t ngx_cached_syslog_time; | |
/* | |
diff --git a/src/http/modules/ngx_http_log_module.c b/src/http/modules/ngx_http_log_module.c | |
index c42fb08..07605d0 100644 | |
--- a/src/http/modules/ngx_http_log_module.c | |
+++ b/src/http/modules/ngx_http_log_module.c | |
@@ -112,6 +112,8 @@ static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf, | |
ngx_http_log_op_t *op); | |
static u_char *ngx_http_log_iso8601(ngx_http_request_t *r, u_char *buf, | |
ngx_http_log_op_t *op); | |
+static u_char *ngx_http_log_iso8601_ms(ngx_http_request_t *r, u_char *buf, | |
+ ngx_http_log_op_t *op); | |
static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, | |
ngx_http_log_op_t *op); | |
static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf, | |
@@ -223,6 +225,8 @@ static ngx_http_log_var_t ngx_http_log_vars[] = { | |
ngx_http_log_time }, | |
{ ngx_string("time_iso8601"), sizeof("1970-09-28T12:00:00+06:00") - 1, | |
ngx_http_log_iso8601 }, | |
+ { ngx_string("time_iso8601_ms"), sizeof("1970-09-28T12:00:00.000+06:00") - 1, | |
+ ngx_http_log_iso8601_ms }, | |
{ ngx_string("msec"), NGX_TIME_T_LEN + 4, ngx_http_log_msec }, | |
{ ngx_string("request_time"), NGX_TIME_T_LEN + 4, | |
ngx_http_log_request_time }, | |
@@ -819,6 +823,13 @@ ngx_http_log_iso8601(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op) | |
} | |
static u_char * | |
+ngx_http_log_iso8601_ms(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op) | |
+{ | |
+ return ngx_cpymem(buf, ngx_cached_http_log_iso8601_ms.data, | |
+ ngx_cached_http_log_iso8601_ms.len); | |
+} | |
+ | |
+static u_char * | |
ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op) | |
{ | |
ngx_time_t *tp; | |
diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c | |
index 7e65b2e..0f036a6 100644 | |
--- a/src/http/ngx_http_variables.c | |
+++ b/src/http/ngx_http_variables.c | |
@@ -135,6 +135,8 @@ static ngx_int_t ngx_http_variable_msec(ngx_http_request_t *r, | |
ngx_http_variable_value_t *v, uintptr_t data); | |
static ngx_int_t ngx_http_variable_time_iso8601(ngx_http_request_t *r, | |
ngx_http_variable_value_t *v, uintptr_t data); | |
+static ngx_int_t ngx_http_variable_time_iso8601_ms(ngx_http_request_t *r, | |
+ ngx_http_variable_value_t *v, uintptr_t data); | |
static ngx_int_t ngx_http_variable_time_local(ngx_http_request_t *r, | |
ngx_http_variable_value_t *v, uintptr_t data); | |
@@ -339,6 +341,9 @@ static ngx_http_variable_t ngx_http_core_variables[] = { | |
{ ngx_string("time_iso8601"), NULL, ngx_http_variable_time_iso8601, | |
0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, | |
+ { ngx_string("time_iso8601_ms"), NULL, ngx_http_variable_time_iso8601_ms, | |
+ 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, | |
+ | |
{ ngx_string("time_local"), NULL, ngx_http_variable_time_local, | |
0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, | |
@@ -2239,6 +2244,30 @@ ngx_http_variable_time_iso8601(ngx_http_request_t *r, | |
static ngx_int_t | |
+ngx_http_variable_time_iso8601_ms(ngx_http_request_t *r, | |
+ ngx_http_variable_value_t *v, uintptr_t data) | |
+{ | |
+ u_char *p; | |
+ | |
+ p = ngx_pnalloc(r->pool, ngx_cached_http_log_iso8601_ms.len); | |
+ if (p == NULL) { | |
+ return NGX_ERROR; | |
+ } | |
+ | |
+ ngx_memcpy(p, ngx_cached_http_log_iso8601_ms.data, | |
+ ngx_cached_http_log_iso8601_ms.len); | |
+ | |
+ v->len = ngx_cached_http_log_iso8601_ms.len; | |
+ v->valid = 1; | |
+ v->no_cacheable = 0; | |
+ v->not_found = 0; | |
+ v->data = p; | |
+ | |
+ return NGX_OK; | |
+} | |
+ | |
+ | |
+static ngx_int_t | |
ngx_http_variable_time_local(ngx_http_request_t *r, | |
ngx_http_variable_value_t *v, uintptr_t data) | |
{ | |
diff --git a/src/stream/ngx_stream_variables.c b/src/stream/ngx_stream_variables.c | |
index aa5361d..652415d 100644 | |
--- a/src/stream/ngx_stream_variables.c | |
+++ b/src/stream/ngx_stream_variables.c | |
@@ -44,6 +44,8 @@ static ngx_int_t ngx_stream_variable_msec(ngx_stream_session_t *s, | |
ngx_stream_variable_value_t *v, uintptr_t data); | |
static ngx_int_t ngx_stream_variable_time_iso8601(ngx_stream_session_t *s, | |
ngx_stream_variable_value_t *v, uintptr_t data); | |
+static ngx_int_t ngx_stream_variable_time_iso8601_ms(ngx_stream_session_t *s, | |
+ ngx_stream_variable_value_t *v, uintptr_t data); | |
static ngx_int_t ngx_stream_variable_time_local(ngx_stream_session_t *s, | |
ngx_stream_variable_value_t *v, uintptr_t data); | |
static ngx_int_t ngx_stream_variable_protocol(ngx_stream_session_t *s, | |
@@ -103,6 +105,9 @@ static ngx_stream_variable_t ngx_stream_core_variables[] = { | |
{ ngx_string("time_iso8601"), NULL, ngx_stream_variable_time_iso8601, | |
0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, | |
+ { ngx_string("time_iso8601_ms"), NULL, ngx_stream_variable_time_iso8601_ms, | |
+ 0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, | |
+ | |
{ ngx_string("time_local"), NULL, ngx_stream_variable_time_local, | |
0, NGX_STREAM_VAR_NOCACHEABLE, 0 }, | |
@@ -721,6 +726,30 @@ ngx_stream_variable_time_iso8601(ngx_stream_session_t *s, | |
static ngx_int_t | |
+ngx_stream_variable_time_iso8601_ms(ngx_stream_session_t *s, | |
+ ngx_stream_variable_value_t *v, uintptr_t data) | |
+{ | |
+ u_char *p; | |
+ | |
+ p = ngx_pnalloc(s->connection->pool, ngx_cached_http_log_iso8601_ms.len); | |
+ if (p == NULL) { | |
+ return NGX_ERROR; | |
+ } | |
+ | |
+ ngx_memcpy(p, ngx_cached_http_log_iso8601_ms.data, | |
+ ngx_cached_http_log_iso8601_ms.len); | |
+ | |
+ v->len = ngx_cached_http_log_iso8601_ms.len; | |
+ v->valid = 1; | |
+ v->no_cacheable = 0; | |
+ v->not_found = 0; | |
+ v->data = p; | |
+ | |
+ return NGX_OK; | |
+} | |
+ | |
+ | |
+static ngx_int_t | |
ngx_stream_variable_time_local(ngx_stream_session_t *s, | |
ngx_stream_variable_value_t *v, uintptr_t data) | |
{ | |
-- | |
2.10.1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment