Skip to content

Commit a88e857

Browse files
Var: $request_id variable.
This variable contains a string that is formed using random data and can be used as a unique request identifier. This closes #714 issue on GitHub.
1 parent 6ae7142 commit a88e857

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

docs/changes.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ NGINX Unit updated to 1.32.0.
2626
</para>
2727
</change>
2828

29+
<change type="feature">
30+
<para>
31+
$request_id variable contains a string that is formed using random data and
32+
can be used as a unique request identifier.
33+
</para>
34+
</change>
35+
2936
</changes>
3037

3138

src/nxt_http_variables.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ static u_char *nxt_http_log_date(u_char *buf, nxt_realtime_t *now,
2828
struct tm *tm, size_t size, const char *format);
2929
static nxt_int_t nxt_http_var_request_line(nxt_task_t *task, nxt_str_t *str,
3030
void *ctx, void *data);
31+
static nxt_int_t nxt_http_var_request_id(nxt_task_t *task, nxt_str_t *str,
32+
void *ctx, void *data);
3133
static nxt_int_t nxt_http_var_status(nxt_task_t *task, nxt_str_t *str,
3234
void *ctx, void *data);
3335
static nxt_int_t nxt_http_var_body_bytes_sent(nxt_task_t *task, nxt_str_t *str,
@@ -89,6 +91,10 @@ static nxt_var_decl_t nxt_http_vars[] = {
8991
.name = nxt_string("request_line"),
9092
.handler = nxt_http_var_request_line,
9193
.cacheable = 1,
94+
}, {
95+
.name = nxt_string("request_id"),
96+
.handler = nxt_http_var_request_id,
97+
.cacheable = 1,
9298
}, {
9399
.name = nxt_string("status"),
94100
.handler = nxt_http_var_status,
@@ -395,6 +401,32 @@ nxt_http_var_request_line(nxt_task_t *task, nxt_str_t *str, void *ctx,
395401
}
396402

397403

404+
static nxt_int_t
405+
nxt_http_var_request_id(nxt_task_t *task, nxt_str_t *str, void *ctx,
406+
void *data)
407+
{
408+
nxt_random_t *rand;
409+
nxt_http_request_t *r;
410+
411+
r = ctx;
412+
413+
str->start = nxt_mp_nget(r->mem_pool, 32);
414+
if (nxt_slow_path(str->start == NULL)) {
415+
return NXT_ERROR;
416+
}
417+
418+
str->length = 32;
419+
420+
rand = &task->thread->random;
421+
422+
(void) nxt_sprintf(str->start, str->start + 32, "%08xD%08xD%08xD%08xD",
423+
nxt_random(rand), nxt_random(rand),
424+
nxt_random(rand), nxt_random(rand));
425+
426+
return NXT_OK;
427+
}
428+
429+
398430
static nxt_int_t
399431
nxt_http_var_body_bytes_sent(nxt_task_t *task, nxt_str_t *str, void *ctx,
400432
void *data)

test/test_variables.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,26 @@ def test_variables_request_line(search_in_file, wait_for_record):
211211
assert wait_for_record(reg, 'access.log') is not None
212212

213213

214+
def test_variables_request_id(search_in_file, wait_for_record, findall):
215+
set_format('$uri $request_id $request_id')
216+
217+
assert search_in_file(r'/request_id', 'access.log') is None
218+
assert client.get(url='/request_id_1')['status'] == 200
219+
assert client.get(url='/request_id_2')['status'] == 200
220+
assert wait_for_record(r'/request_id_2', 'access.log') is not None
221+
222+
id1 = findall(
223+
r'^\/request_id_1 ([0-9a-f]{32}) ([0-9a-f]{32})$', 'access.log'
224+
)[0]
225+
id2 = findall(
226+
r'^\/request_id_2 ([0-9a-f]{32}) ([0-9a-f]{32})$', 'access.log'
227+
)[0]
228+
229+
assert id1[0] == id1[1], 'same ids first'
230+
assert id2[0] == id2[1], 'same ids second'
231+
assert id1[0] != id2[0], 'first id != second id'
232+
233+
214234
def test_variables_status(search_in_file, wait_for_record):
215235
set_format('$status')
216236

0 commit comments

Comments
 (0)