Skip to content

Commit

Permalink
ngx_http_lua_kong_request_id.c
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Oct 10, 2023
1 parent 8296b70 commit 99ac3d2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ngx_module_srcs=" \
$ngx_addon_dir/src/ngx_http_lua_kong_module.c \
$ngx_addon_dir/src/ngx_http_lua_kong_log.c \
$ngx_addon_dir/src/ngx_http_lua_kong_log_handler.c \
$ngx_addon_dir/src/ngx_http_lua_kong_request_id.c \
$ngx_addon_dir/src/ssl/ngx_lua_kong_ssl.c \
"

Expand Down
50 changes: 50 additions & 0 deletions src/ngx_http_lua_kong_request_id.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2019-2023 Kong Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


#include "ngx_http_lua_kong_common.h"


#define KONG_REQUEST_ID_MAX_LEN 32
#define KONG_REQUEST_ID_FORMAT "%08xD%08xD%08xD%08xD"


static ngx_int_t
ngx_http_lua_kong_variable_request_id(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
u_char *id;

id = ngx_pnalloc(r->pool, KONG_REQUEST_ID_MAX_LEN);
if (id == NULL) {
return NGX_ERROR;
}

v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;

v->len = KONG_REQUEST_ID_MAX_LEN;
v->data = id;

ngx_sprintf(id, KONG_REQUEST_ID_FORMAT,
(uint32_t) ngx_random(), (uint32_t) ngx_random(),
(uint32_t) ngx_random(), (uint32_t) ngx_random());

return NGX_OK;
}


0 comments on commit 99ac3d2

Please sign in to comment.