Add variables to hold processing times for modsecurity phases#278
Add variables to hold processing times for modsecurity phases#278awmackowiak wants to merge 14 commits intoowasp-modsecurity:masterfrom
Conversation
Add variable for logging time and add examples how to use it to README
src/ngx_http_modsecurity_rewrite.c
Outdated
| int ret = 0; | ||
| struct timeval start_tv; | ||
|
|
||
| ngx_gettimeofday(&start_tv); |
There was a problem hiding this comment.
I suggest use ngx_timeofday like request_time or upstream_xxx_time in upstream module.
There was a problem hiding this comment.
sorry, ngx_current_msec is cached. just use ngx_gettimeofday
There was a problem hiding this comment.
I'd like to use clock_gettime with CLOCK_MONOTONIC for ns.
|
For waf performance, I'd like to implement as this (in nginx module):
For response, it's hard to implement, as there may be multi upstream (refer For modsec rules performance, I'd like to implement it in modsecurity. |
|
@awmackowiak Could you help to review #273 and #277? |
|
Have you guys tried the performance instrumentation? The perf. instrumentation seems to have all the information that you guys are trying to collect. https://github.com/SpiderLabs/ModSecurity-nginx/blob/master/ngx-modsec.stp Alternatively you can the have the function level chart - That is an interactive SVG, like this one - Also, you can compare ModSecurity execution times outside nginx, by using the command line utilities - https://github.com/SpiderLabs/ModSecurity/tree/v3/master/test All that without having to patch a single line from ModSecurity. Patching ModSecurity to support some sort of performance indicators was discussed here - owasp-modsecurity/ModSecurity#1011 |
|
@zimmerle We'd like to record waf time in log...just as request_time and upstream_response_time... |
src/ngx_http_modsecurity_module.c
Outdated
| ngx_http_modsecurity_logging_phase_time, 0, | ||
| NGX_HTTP_VAR_NOCACHEABLE, 0 }, | ||
|
|
||
| ngx_http_null_variable |
There was a problem hiding this comment.
ngx_http_null_variable is introduced in 1.13.4, expand it to support old nginx. nginx/nginx@b992f72
There was a problem hiding this comment.
Yes. I change it for old style.
src/ngx_http_modsecurity_module.c
Outdated
|
|
||
| { ngx_string("modsecurity_logging_phase_time"), NULL, | ||
| ngx_http_modsecurity_logging_phase_time, 0, | ||
| NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
There was a problem hiding this comment.
Yes you are right. I added it.
src/ngx_http_modsecurity_module.c
Outdated
| NGX_HTTP_VAR_NOCACHEABLE, 0 }, | ||
|
|
||
| { ngx_string("modsecurity_logging_phase_time"), NULL, | ||
| ngx_http_modsecurity_logging_phase_time, 0, |
There was a problem hiding this comment.
use a same function with different argument, like 0, 1, 2, ...
There was a problem hiding this comment.
Can you provide an example what are you mean for that?
I didn't find example for variables with indexed arguments.
There was a problem hiding this comment.
Refer https://github.com/nginx/nginx/blob/release-1.21.6/src/http/ngx_http_upstream.c#L391-L401
Same function ngx_http_upstream_response_time_variable for 3 variables.
| /* XXX: once more -- is body can be modified ? content-length need to be adjusted ? */ | ||
|
|
||
| old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool); | ||
|
|
Fix after review
|
@liudongmiao I made fixes as you suggests. |
|
@liudongmiao could you review it again? |
|
@ziollek These codes are ok. However, we use another solution, calc the total modsec time, named it as "msc_request_time" and "msc_engine_time".
|
|
@liudongmiao thanks for reply. Could you point out where these variables are described? Are they exposed as a nginx variables? I would like to give you some insights what need states behind this change. |
|
@ziollek Yes, we have the same problem. We export it to nginx variables, and update the nginx log format. In our production, the average msc_engine_time is about 1ms, and 95% is less than 5ms, which is acceptable by our team. |
@zimmerle Thanks for pointing out this tool, I was not aware of that. Lately, I had some spare time and I tried to profile using it, and I have some thoughts about it. First of all, it is really cumbersome using And finally, I think that there is some misunderstanding of the purpose of such changes. We do not want to provide another way of profiling |
|






Format Variables
ModSecurity-nginx provide times variables for particular phases that you can uses in nginx log_format:
The following example show how to configure custom log_format with variables above and use them with custom access.log: