|
1 | 1 |
|
| 2 | +#include <ngx_config.h> |
2 | 3 | #include <ngx_core.h> |
3 | 4 | #include <ngx_http.h> |
4 | | -#include <ngx_http_realip_module.c> |
| 5 | +#include <ngx_http_realip_module.h> |
5 | 6 | #include "ngx_http_apisix_module.h" |
6 | | -#include "ngx_http_apisix_log_handler.c" |
7 | 7 |
|
8 | 8 | #define NGX_HTTP_APISIX_SSL_ENC 1 |
9 | 9 | #define NGX_HTTP_APISIX_SSL_SIGN 2 |
@@ -51,6 +51,7 @@ static ngx_command_t ngx_http_apisix_cmds[] = { |
51 | 51 | }, |
52 | 52 | ngx_null_command |
53 | 53 | }; |
| 54 | + |
54 | 55 | static ngx_http_module_t ngx_http_apisix_module_ctx = { |
55 | 56 | NULL, /* preconfiguration */ |
56 | 57 | ngx_http_apisix_init, /* postconfiguration */ |
@@ -838,3 +839,132 @@ ngx_http_apisix_is_ntls_enabled(ngx_http_conf_ctx_t *conf_ctx) |
838 | 839 | acf = ngx_http_get_module_main_conf(conf_ctx, ngx_http_apisix_module); |
839 | 840 | return acf->enable_ntls; |
840 | 841 | } |
| 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