nghttpx: Optimize accesslog write

This commit is contained in:
Tatsuhiro Tsujikawa 2016-01-17 11:19:19 +09:00
parent 506de55475
commit 959d378f2a
4 changed files with 46 additions and 29 deletions

View File

@ -835,25 +835,27 @@ void ClientHandler::write_accesslog(Downstream *downstream) {
upstream_accesslog( upstream_accesslog(
get_config()->accesslog_format, get_config()->accesslog_format,
LogSpec{ LogSpec{
downstream, ipaddr_.c_str(), http2::to_method_string(req.method), downstream, ipaddr_, http2::to_method_string(req.method),
req.method == HTTP_CONNECT req.method == HTTP_CONNECT
? req.authority.c_str() ? req.authority
: (get_config()->http2_proxy || get_config()->client_proxy) : (get_config()->http2_proxy || get_config()->client_proxy)
? construct_absolute_request_uri(req).c_str() ? construct_absolute_request_uri(req)
: req.path.empty() ? req.method == HTTP_OPTIONS ? "*" : "-" : req.path.empty()
: req.path.c_str(), ? req.method == HTTP_OPTIONS
? StringAdaptor::from_lit("*")
: StringAdaptor::from_lit("-")
: req.path,
alpn_.c_str(), alpn_, nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl),
nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl),
std::chrono::system_clock::now(), // time_now std::chrono::system_clock::now(), // time_now
downstream->get_request_start_time(), // request_start_time downstream->get_request_start_time(), // request_start_time
std::chrono::high_resolution_clock::now(), // request_end_time std::chrono::high_resolution_clock::now(), // request_end_time
req.http_major, req.http_minor, resp.http_status, req.http_major, req.http_minor, resp.http_status,
downstream->response_sent_body_length, port_.c_str(), downstream->response_sent_body_length, port_, get_config()->port,
get_config()->port, get_config()->pid, get_config()->pid,
}); });
} }
@ -863,20 +865,19 @@ void ClientHandler::write_accesslog(int major, int minor, unsigned int status,
auto highres_now = std::chrono::high_resolution_clock::now(); auto highres_now = std::chrono::high_resolution_clock::now();
nghttp2::ssl::TLSSessionInfo tls_info; nghttp2::ssl::TLSSessionInfo tls_info;
upstream_accesslog(get_config()->accesslog_format, upstream_accesslog(
get_config()->accesslog_format,
LogSpec{ LogSpec{
nullptr, ipaddr_.c_str(), nullptr, ipaddr_,
"-", // method StringAdaptor::from_lit("-"), // method
"-", // path, StringAdaptor::from_lit("-"), // path,
alpn_.c_str(), nghttp2::ssl::get_tls_session_info( alpn_, nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl),
&tls_info, conn_.tls.ssl),
time_now, time_now,
highres_now, // request_start_time TODO is highres_now, // request_start_time TODO is
// there a better value? // there a better value?
highres_now, // request_end_time highres_now, // request_end_time
major, minor, // major, minor major, minor, // major, minor
status, body_bytes_sent, port_.c_str(), status, body_bytes_sent, port_, get_config()->port, get_config()->pid,
get_config()->port, get_config()->pid,
}); });
} }

View File

@ -182,6 +182,14 @@ std::pair<OutputIterator, size_t> copy(const std::string &src, size_t avail,
} }
} // namespace } // namespace
namespace {
template <typename OutputIterator>
std::pair<OutputIterator, size_t> copy(const StringAdaptor &src, size_t avail,
OutputIterator oitr) {
return copy(src.c_str(), src.size(), avail, oitr);
}
} // namespace
namespace { namespace {
template <size_t N, typename OutputIterator> template <size_t N, typename OutputIterator>
std::pair<OutputIterator, size_t> copy_l(const char(&src)[N], size_t avail, std::pair<OutputIterator, size_t> copy_l(const char(&src)[N], size_t avail,

View File

@ -36,6 +36,9 @@
#include "shrpx_log_config.h" #include "shrpx_log_config.h"
#include "ssl.h" #include "ssl.h"
#include "template.h"
using namespace nghttp2;
#define ENABLE_LOG 1 #define ENABLE_LOG 1
@ -139,10 +142,10 @@ struct LogFragment {
struct LogSpec { struct LogSpec {
Downstream *downstream; Downstream *downstream;
const char *remote_addr; StringAdaptor remote_addr;
const char *method; StringAdaptor method;
const char *path; StringAdaptor path;
const char *alpn; StringAdaptor alpn;
const nghttp2::ssl::TLSSessionInfo *tls_info; const nghttp2::ssl::TLSSessionInfo *tls_info;
std::chrono::system_clock::time_point time_now; std::chrono::system_clock::time_point time_now;
std::chrono::high_resolution_clock::time_point request_start_time; std::chrono::high_resolution_clock::time_point request_start_time;
@ -150,7 +153,7 @@ struct LogSpec {
int major, minor; int major, minor;
unsigned int status; unsigned int status;
int64_t body_bytes_sent; int64_t body_bytes_sent;
const char *remote_port; StringAdaptor remote_port;
uint16_t server_port; uint16_t server_port;
pid_t pid; pid_t pid;
}; };

View File

@ -254,6 +254,11 @@ struct StringAdaptor {
StringAdaptor(const T &s) StringAdaptor(const T &s)
: base(s.c_str()), len(s.size()) {} : base(s.c_str()), len(s.size()) {}
StringAdaptor(const char *s) : base(s), len(strlen(s)) {} StringAdaptor(const char *s) : base(s), len(strlen(s)) {}
StringAdaptor(const char *s, size_t n) : base(s), len(n) {}
template <size_t N> static StringAdaptor from_lit(const char(&s)[N]) {
return StringAdaptor(s, N - 1);
}
const char *c_str() const { return base; } const char *c_str() const { return base; }
size_t size() const { return len; } size_t size() const { return len; }