通過Nginx,Tomcat訪問日志(access log)記錄請求耗時


一、Nginx通過$upstream_response_time $request_time統計請求和后台服務響應時間

nginx.conf使用配置方式:

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' '$connection $upstream_addr ' 'upstream_response_time $upstream_response_time request_time $request_time ';

$request_time和$upstream_response_time之間差別:

$request_time包含了用戶數據接收時間,而真正程序的響應時間應該用$upstream_response_time 所以如果用戶網絡較差,或者傳遞數據較大時,$request_time會比$upstream_response_time大很多 詳細參考:http://wuzhangshu927.blog.163.com/blog/static/114224687201310674652147/

二、Tomcat通過%D或%T統計請求響應時間

server.xml使用配置方式 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"    prefix="localhost_access_log." suffix=".txt"    pattern="%h %l %u [%{yyyy-MM-dd HH:mm:ss}t] %{X-Real_IP}i &quot;%r&quot; %s %b %D %F" />

%D - 官方解釋:Time taken to process the request, in millis,處理請求的時間,以毫秒為單位 %T - 官方解釋:Time taken to process the request, in seconds,處理請求的時間,以秒為單位 %F - 官方解釋:Time taken to commit the response, in millis,提交響應的時間,以毫秒為單位 詳細說明:http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Logging

 

三、通過awk命令輔助統計access.log

1.簡單統計nginx訪問日志access log每分鍾請求數

awk -F: '{count[$2":"$3]++} END {for (minute in count) print minute, count[minute]}' /usr/local/nginx/logs/access.log | sort > count.log

結果如下所示(count.log) 18:30 2086 18:31 2184 18:32 2176 18:33 2122 18:34 2128 18:35 2179 ...

參考:http://huoding.com/2013/01/26/215

 

2.統計請求響應時間超過10s的記錄

awk '($NF > 10){print $0}' /usr/local/tengine/logs/cut-log/access_2015-01-12.log >t10_0112.log

更多awk命令統計訪問日志參考:http://www.ibm.com/developerworks/cn/linux/l-cn-awk-httplog/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM