通过Nginx,Tomcat访问日志(access log)记录请求耗时
一、Nginx通過(guò)$upstream_response_time $request_time統(tǒng)計(jì)請(qǐng)求和后臺(tái)服務(wù)響應(yīng)時(shí)間
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包含了用戶(hù)數(shù)據(jù)接收時(shí)間,而真正程序的響應(yīng)時(shí)間應(yīng)該用$upstream_response_time
所以如果用戶(hù)網(wǎng)絡(luò)較差,或者傳遞數(shù)據(jù)較大時(shí),$request_time會(huì)比$upstream_response_time大很多
詳細(xì)參考:http://wuzhangshu927.blog.163.com/blog/static/114224687201310674652147/
二、Tomcat通過(guò)%D或%T統(tǒng)計(jì)請(qǐng)求響應(yīng)時(shí)間
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 "%r" %s %b %D %F" />
%D - 官方解釋:Time taken to process the request, in millis,處理請(qǐng)求的時(shí)間,以毫秒為單位
%T - 官方解釋:Time taken to process the request, in seconds,處理請(qǐng)求的時(shí)間,以秒為單位
%F - 官方解釋:Time taken to commit the response, in millis,提交響應(yīng)的時(shí)間,以毫秒為單位
詳細(xì)說(shuō)明:http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Logging
?
三、通過(guò)awk命令輔助統(tǒng)計(jì)access.log
1.簡(jiǎn)單統(tǒng)計(jì)nginx訪問(wèn)日志access log每分鐘請(qǐng)求數(shù)
awk -F: '{count[$2":"$3]++} END {for (minute in count) print minute, count[minute]}'?/usr/local/nginx/logs/access.log | sort > count.log
結(jié)果如下所示(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.統(tǒng)計(jì)請(qǐng)求響應(yīng)時(shí)間超過(guò)10s的記錄
awk '($NF > 10){print $0}' /usr/local/tengine/logs/cut-log/access_2015-01-12.log >t10_0112.log
更多awk命令統(tǒng)計(jì)訪問(wèn)日志參考:http://www.ibm.com/developerworks/cn/linux/l-cn-awk-httplog/
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的通过Nginx,Tomcat访问日志(access log)记录请求耗时的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: text-indent无效解决方案
- 下一篇: ThreadPool.QueueUser