Skip to content

Commit a1f7c3a

Browse files
committed
final
1 parent 41582ef commit a1f7c3a

File tree

3 files changed

+132
-133
lines changed

3 files changed

+132
-133
lines changed

config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
ngx_module_type=HTTP
22
ngx_module_name=ngx_http_apisix_module
33
ngx_module_srcs="$ngx_addon_dir/src/ngx_http_apisix_module.c"
4-
ngx_module_srcs="$ngx_addon_dir/src/ngx_http_apisix_log_handler.c"
54
ngx_module_deps=$ngx_addon_dir/src/ngx_http_apisix_module.h
65
ngx_module_incs="$ngx_addon_dir/src"
76

src/ngx_http_apisix_log_handler.c

Lines changed: 0 additions & 130 deletions
This file was deleted.

src/ngx_http_apisix_module.c

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2+
#include <ngx_config.h>
23
#include <ngx_core.h>
34
#include <ngx_http.h>
4-
#include <ngx_http_realip_module.c>
5+
#include <ngx_http_realip_module.h>
56
#include "ngx_http_apisix_module.h"
6-
#include "ngx_http_apisix_log_handler.c"
77

88
#define NGX_HTTP_APISIX_SSL_ENC 1
99
#define NGX_HTTP_APISIX_SSL_SIGN 2
@@ -51,6 +51,7 @@ static ngx_command_t ngx_http_apisix_cmds[] = {
5151
},
5252
ngx_null_command
5353
};
54+
5455
static ngx_http_module_t ngx_http_apisix_module_ctx = {
5556
NULL, /* preconfiguration */
5657
ngx_http_apisix_init, /* postconfiguration */
@@ -838,3 +839,132 @@ ngx_http_apisix_is_ntls_enabled(ngx_http_conf_ctx_t *conf_ctx)
838839
acf = ngx_http_get_module_main_conf(conf_ctx, ngx_http_apisix_module);
839840
return acf->enable_ntls;
840841
}
842+
843+
/*******************Log handler***************** */
844+
/*
845+
* This function contains the logic to append the Request ID to
846+
* the error log line when being called.
847+
* Get the location configuration from helper function. Find indexed variable with the loc_conf->request_id_var_index. and add that to buffer.
848+
*/
849+
static u_char*
850+
ngx_http_apisix_error_log_handler(ngx_http_request_t *r, u_char *buf, size_t len)
851+
{
852+
ngx_http_variable_value_t *request_id_var;
853+
ngx_http_apisix_loc_conf_t *loc_conf;
854+
855+
loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_apisix_module);
856+
if (loc_conf->request_id_var_index == NGX_CONF_UNSET) {
857+
return buf;
858+
}
859+
860+
request_id_var = ngx_http_get_indexed_variable(r, loc_conf->request_id_var_index);
861+
if (request_id_var == NULL || request_id_var->not_found) {
862+
return buf;
863+
}
864+
buf = ngx_snprintf(buf, len, ", request_id: \"%v\"", request_id_var);
865+
return buf;
866+
}
867+
868+
869+
/*
870+
* This function replaces the original HTTP error
871+
* log handler (r->log_handler). It executes the original logic
872+
* and then our error log handler: ngx_http_apisix_error_log_handler
873+
* This function returns the final message.
874+
*/
875+
static u_char*
876+
ngx_http_apisix_combined_error_log_handler(ngx_http_request_t *r, ngx_http_request_t *sr, u_char *buf, size_t len)
877+
{
878+
u_char *p;
879+
ngx_http_apisix_ctx_t *ctx;
880+
881+
ctx = ngx_http_apisix_get_module_ctx(r);
882+
if (ctx == NULL || ctx->orig_log_handler == NULL) {
883+
return buf;
884+
}
885+
886+
//Get the original log message
887+
p = ctx->orig_log_handler(r, sr, buf, len);
888+
//p - buf calculates the number of bytes written by the original log handler into the buffer.
889+
//len -= (p - buf) reduces the remaining buffer length by the amount already used.
890+
len -= p-buf;
891+
892+
//Apisix log handler
893+
buf = ngx_http_apisix_error_log_handler(r, buf, len);
894+
return buf;
895+
}
896+
897+
898+
//It replaces the r->log_handler which is the log handler of the request with the combined log handler.
899+
// Creates the apisix context we need from the request to act on it.
900+
static ngx_int_t
901+
ngx_http_apisix_replace_error_log_handler(ngx_http_request_t *r)
902+
{
903+
ngx_http_apisix_ctx_t *ctx;
904+
905+
ctx = ngx_http_apisix_get_module_ctx(r);
906+
if (ctx == NULL) {
907+
return NGX_OK;
908+
}
909+
910+
if (r->log_handler == NULL){
911+
return NGX_DECLINED;
912+
}
913+
914+
/*
915+
* Store the original log handler in ctx->orig_log_handler, replace
916+
* it with the combined log handler, which will execute the original
917+
* handler's logic in addition to our own.
918+
*/
919+
ctx->orig_log_handler = r->log_handler;
920+
r->log_handler = ngx_http_apisix_combined_error_log_handler;
921+
922+
return NGX_DECLINED;
923+
}
924+
925+
//This function is part of postconfiguration passed to module context and will override the post_read_phase with custom log handler.
926+
// It extracts the pointer to log handler from the post read phase handlers and then override that with new function address.
927+
char *
928+
ngx_http_apisix_error_log_init(ngx_conf_t *cf)
929+
{
930+
ngx_http_handler_pt *h;
931+
ngx_http_core_main_conf_t *cmcf;
932+
933+
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
934+
h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
935+
if (h == NULL) {
936+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
937+
"failed setting error log handler");
938+
return NGX_CONF_ERROR;
939+
}
940+
941+
*h = ngx_http_apisix_replace_error_log_handler;
942+
943+
return NGX_CONF_OK;
944+
}
945+
946+
// This function does the translation of the configuration file to the internal representation.
947+
// So this will just set the value in loc_conf that it gets by reference.
948+
char *
949+
ngx_http_apisix_error_log_request_id(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
950+
{
951+
ngx_str_t *value;
952+
ngx_http_apisix_loc_conf_t *loc_conf = conf;
953+
954+
value = cf->args->elts;
955+
if (value[1].data[0] != '$') {
956+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid variable name \"%V\"", &value[1]);
957+
return NGX_CONF_ERROR;
958+
}
959+
960+
value[1].len--;
961+
value[1].data++;
962+
963+
loc_conf->request_id_var_index = ngx_http_get_variable_index(cf, &value[1]);
964+
if (loc_conf->request_id_var_index == NGX_ERROR) {
965+
return NGX_CONF_ERROR;
966+
}
967+
968+
return NGX_CONF_OK;
969+
}
970+

0 commit comments

Comments
 (0)