@@ -19,12 +19,20 @@ static ngx_str_t remote_port = ngx_string("remote_port");
1919static ngx_str_t realip_remote_addr = ngx_string ("realip_remote_addr" );
2020static ngx_str_t realip_remote_port = ngx_string ("realip_remote_port" );
2121
22-
22+ static ngx_int_t ngx_http_apisix_init ( ngx_conf_t * cf );
2323static void * ngx_http_apisix_create_main_conf (ngx_conf_t * cf );
2424static void * ngx_http_apisix_create_loc_conf (ngx_conf_t * cf );
2525static char * ngx_http_apisix_merge_loc_conf (ngx_conf_t * cf , void * parent ,
2626 void * child );
2727
28+ static ngx_int_t
29+ ngx_http_apisix_init (ngx_conf_t * cf )
30+ {
31+ if (ngx_http_apisix_error_log_init (cf ) != NGX_CONF_OK ) {
32+ return NGX_ERROR ;
33+ }
34+ return NGX_OK ;
35+ }
2836
2937static ngx_command_t ngx_http_apisix_cmds [] = {
3038 { ngx_string ("apisix_delay_client_max_body_check" ),
@@ -33,14 +41,20 @@ static ngx_command_t ngx_http_apisix_cmds[] = {
3341 NGX_HTTP_LOC_CONF_OFFSET ,
3442 offsetof(ngx_http_apisix_loc_conf_t , delay_client_max_body_check ),
3543 NULL },
36-
44+ {
45+ ngx_string ("lua_error_log_request_id" ),
46+ NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
47+ ngx_http_apisix_error_log_request_id ,
48+ NGX_HTTP_LOC_CONF_OFFSET ,
49+ offsetof(ngx_http_apisix_loc_conf_t , apisix_request_id_var_index ),
50+ NULL
51+ },
3752 ngx_null_command
3853};
3954
40-
4155static ngx_http_module_t ngx_http_apisix_module_ctx = {
4256 NULL , /* preconfiguration */
43- NULL , /* postconfiguration */
57+ ngx_http_apisix_init , /* postconfiguration */
4458
4559 ngx_http_apisix_create_main_conf , /* create main configuration */
4660 NULL , /* init main configuration */
@@ -88,7 +102,6 @@ ngx_http_apisix_create_main_conf(ngx_conf_t *cf)
88102 return acf ;
89103}
90104
91-
92105static void *
93106ngx_http_apisix_create_loc_conf (ngx_conf_t * cf )
94107{
@@ -100,7 +113,7 @@ ngx_http_apisix_create_loc_conf(ngx_conf_t *cf)
100113 }
101114
102115 conf -> delay_client_max_body_check = NGX_CONF_UNSET ;
103-
116+ conf -> apisix_request_id_var_index = NGX_CONF_UNSET ;
104117 return conf ;
105118}
106119
@@ -111,6 +124,7 @@ ngx_http_apisix_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
111124 ngx_http_apisix_loc_conf_t * prev = parent ;
112125 ngx_http_apisix_loc_conf_t * conf = child ;
113126
127+ ngx_conf_merge_value (conf -> apisix_request_id_var_index , prev -> apisix_request_id_var_index , NGX_CONF_UNSET );
114128 ngx_conf_merge_value (conf -> delay_client_max_body_check ,
115129 prev -> delay_client_max_body_check , 0 );
116130
@@ -825,3 +839,117 @@ ngx_http_apisix_is_ntls_enabled(ngx_http_conf_ctx_t *conf_ctx)
825839 acf = ngx_http_get_module_main_conf (conf_ctx , ngx_http_apisix_module );
826840 return acf -> enable_ntls ;
827841}
842+
843+ /*******************Log handler***************** */
844+ static u_char *
845+ ngx_http_apisix_error_log_handler (ngx_http_request_t * r , u_char * buf , size_t len )
846+ {
847+ ngx_http_variable_value_t * request_id_var ;
848+ ngx_http_apisix_loc_conf_t * loc_conf ;
849+
850+ loc_conf = ngx_http_get_module_loc_conf (r , ngx_http_apisix_module );
851+ if (loc_conf -> apisix_request_id_var_index == NGX_CONF_UNSET ) {
852+ return buf ;
853+ }
854+
855+ request_id_var = ngx_http_get_indexed_variable (r , loc_conf -> apisix_request_id_var_index );
856+ if (request_id_var == NULL || request_id_var -> not_found ) {
857+ return buf ;
858+ }
859+ buf = ngx_snprintf (buf , len , ", request_id: \"%v\"" , request_id_var );
860+ return buf ;
861+ }
862+
863+
864+ static u_char *
865+ ngx_http_apisix_combined_error_log_handler (ngx_http_request_t * r , ngx_http_request_t * sr , u_char * buf , size_t len )
866+ {
867+ u_char * p ;
868+ ngx_http_apisix_ctx_t * ctx ;
869+
870+ ctx = ngx_http_apisix_get_module_ctx (r );
871+ if (ctx == NULL || ctx -> orig_log_handler == NULL ) {
872+ return buf ;
873+ }
874+
875+ //Get the original log message
876+ p = ctx -> orig_log_handler (r , sr , buf , len );
877+ //p - buf calculates the number of bytes written by the original log handler into the buffer.
878+ //len -= (p - buf) reduces the remaining buffer length by the amount already used.
879+ len -= p - buf ;
880+
881+ //Apisix log handler
882+ buf = ngx_http_apisix_error_log_handler (r , buf , len );
883+ return buf ;
884+ }
885+
886+
887+ static ngx_int_t
888+ ngx_http_apisix_replace_error_log_handler (ngx_http_request_t * r )
889+ {
890+ ngx_http_apisix_ctx_t * ctx ;
891+
892+ ctx = ngx_http_apisix_get_module_ctx (r );
893+ if (ctx == NULL ) {
894+ return NGX_ERROR ;
895+ }
896+
897+ if (r -> log_handler == NULL ){
898+ return NGX_DECLINED ;
899+ }
900+
901+ /*
902+ * Store the original log handler in ctx->orig_log_handler, replace
903+ * it with the combined log handler, which will execute the original
904+ * handler's logic in addition to our own.
905+ */
906+ ctx -> orig_log_handler = r -> log_handler ;
907+ r -> log_handler = ngx_http_apisix_combined_error_log_handler ;
908+
909+ return NGX_DECLINED ;
910+ }
911+
912+
913+ char *
914+ ngx_http_apisix_error_log_init (ngx_conf_t * cf )
915+ {
916+ ngx_http_handler_pt * h ;
917+ ngx_http_core_main_conf_t * cmcf ;
918+
919+ cmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_core_module );
920+ h = ngx_array_push (& cmcf -> phases [NGX_HTTP_POST_READ_PHASE ].handlers );
921+ if (h == NULL ) {
922+ ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
923+ "failed setting error log handler" );
924+ return NGX_CONF_ERROR ;
925+ }
926+
927+ * h = ngx_http_apisix_replace_error_log_handler ;
928+
929+ return NGX_CONF_OK ;
930+ }
931+
932+
933+ char *
934+ ngx_http_apisix_error_log_request_id (ngx_conf_t * cf , ngx_command_t * cmd , void * conf )
935+ {
936+ ngx_str_t * value ;
937+ ngx_http_apisix_loc_conf_t * loc_conf = conf ;
938+
939+ value = cf -> args -> elts ;
940+ if (value [1 ].data [0 ] != '$' ) {
941+ ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "invalid variable name \"%V\"" , & value [1 ]);
942+ return NGX_CONF_ERROR ;
943+ }
944+
945+ value [1 ].len -- ;
946+ value [1 ].data ++ ;
947+
948+ loc_conf -> apisix_request_id_var_index = ngx_http_get_variable_index (cf , & value [1 ]);
949+ if (loc_conf -> apisix_request_id_var_index == NGX_ERROR ) {
950+ return NGX_CONF_ERROR ;
951+ }
952+
953+ return NGX_CONF_OK ;
954+ }
955+
0 commit comments