@@ -19,12 +19,20 @@ static ngx_str_t remote_port = ngx_string("remote_port");
19
19
static ngx_str_t realip_remote_addr = ngx_string ("realip_remote_addr" );
20
20
static ngx_str_t realip_remote_port = ngx_string ("realip_remote_port" );
21
21
22
-
22
+ static ngx_int_t ngx_http_apisix_init ( ngx_conf_t * cf );
23
23
static void * ngx_http_apisix_create_main_conf (ngx_conf_t * cf );
24
24
static void * ngx_http_apisix_create_loc_conf (ngx_conf_t * cf );
25
25
static char * ngx_http_apisix_merge_loc_conf (ngx_conf_t * cf , void * parent ,
26
26
void * child );
27
27
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
+ }
28
36
29
37
static ngx_command_t ngx_http_apisix_cmds [] = {
30
38
{ ngx_string ("apisix_delay_client_max_body_check" ),
@@ -33,14 +41,20 @@ static ngx_command_t ngx_http_apisix_cmds[] = {
33
41
NGX_HTTP_LOC_CONF_OFFSET ,
34
42
offsetof(ngx_http_apisix_loc_conf_t , delay_client_max_body_check ),
35
43
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
+ },
37
52
ngx_null_command
38
53
};
39
54
40
-
41
55
static ngx_http_module_t ngx_http_apisix_module_ctx = {
42
56
NULL , /* preconfiguration */
43
- NULL , /* postconfiguration */
57
+ ngx_http_apisix_init , /* postconfiguration */
44
58
45
59
ngx_http_apisix_create_main_conf , /* create main configuration */
46
60
NULL , /* init main configuration */
@@ -88,7 +102,6 @@ ngx_http_apisix_create_main_conf(ngx_conf_t *cf)
88
102
return acf ;
89
103
}
90
104
91
-
92
105
static void *
93
106
ngx_http_apisix_create_loc_conf (ngx_conf_t * cf )
94
107
{
@@ -100,7 +113,7 @@ ngx_http_apisix_create_loc_conf(ngx_conf_t *cf)
100
113
}
101
114
102
115
conf -> delay_client_max_body_check = NGX_CONF_UNSET ;
103
-
116
+ conf -> apisix_request_id_var_index = NGX_CONF_UNSET ;
104
117
return conf ;
105
118
}
106
119
@@ -111,6 +124,7 @@ ngx_http_apisix_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
111
124
ngx_http_apisix_loc_conf_t * prev = parent ;
112
125
ngx_http_apisix_loc_conf_t * conf = child ;
113
126
127
+ ngx_conf_merge_value (conf -> apisix_request_id_var_index , prev -> apisix_request_id_var_index , NGX_CONF_UNSET );
114
128
ngx_conf_merge_value (conf -> delay_client_max_body_check ,
115
129
prev -> delay_client_max_body_check , 0 );
116
130
@@ -825,3 +839,117 @@ ngx_http_apisix_is_ntls_enabled(ngx_http_conf_ctx_t *conf_ctx)
825
839
acf = ngx_http_get_module_main_conf (conf_ctx , ngx_http_apisix_module );
826
840
return acf -> enable_ntls ;
827
841
}
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