Commit ad3d43b8 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttpx: Add access log variable for backend host and port

Use $backend_host and $backend_port.  $backend_host is backend host
name given in --backend option.  It could be a path to UNIX domain
socket.
parent 210a5c4f
...@@ -152,6 +152,8 @@ LOGVARS = [ ...@@ -152,6 +152,8 @@ LOGVARS = [
"ssl_protocol", "ssl_protocol",
"ssl_session_id", "ssl_session_id",
"ssl_session_reused", "ssl_session_reused",
"backend_host",
"backend_port",
] ]
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -2082,6 +2082,10 @@ Logging: ...@@ -2082,6 +2082,10 @@ Logging:
* $ssl_session_id: session ID for SSL/TLS connection. * $ssl_session_id: session ID for SSL/TLS connection.
* $ssl_session_reused: "r" if SSL/TLS session was * $ssl_session_reused: "r" if SSL/TLS session was
reused. Otherwise, "." reused. Otherwise, "."
* $backend_host: backend host used to fulfill the
request. "-" if backend host is not available.
* $backend_port: backend port used to fulfill the
request. "-" if backend host is not available.
The variable can be enclosed by "{" and "}" for The variable can be enclosed by "{" and "}" for
disambiguation (e.g., ${remote_addr}). disambiguation (e.g., ${remote_addr}).
......
...@@ -301,9 +301,10 @@ void APIDownstreamConnection::on_upstream_change(Upstream *uptream) {} ...@@ -301,9 +301,10 @@ void APIDownstreamConnection::on_upstream_change(Upstream *uptream) {}
bool APIDownstreamConnection::poolable() const { return false; } bool APIDownstreamConnection::poolable() const { return false; }
DownstreamAddrGroup * const std::shared_ptr<DownstreamAddrGroup> &
APIDownstreamConnection::get_downstream_addr_group() const { APIDownstreamConnection::get_downstream_addr_group() const {
return nullptr; static std::shared_ptr<DownstreamAddrGroup> s;
return s;
} }
DownstreamAddr *APIDownstreamConnection::get_addr() const { return nullptr; } DownstreamAddr *APIDownstreamConnection::get_addr() const { return nullptr; }
......
...@@ -54,7 +54,8 @@ public: ...@@ -54,7 +54,8 @@ public:
// true if this object is poolable. // true if this object is poolable.
virtual bool poolable() const; virtual bool poolable() const;
virtual DownstreamAddrGroup *get_downstream_addr_group() const; virtual const std::shared_ptr<DownstreamAddrGroup> &
get_downstream_addr_group() const;
virtual DownstreamAddr *get_addr() const; virtual DownstreamAddr *get_addr() const;
int send_reply(unsigned int http_status, int api_status); int send_reply(unsigned int http_status, int api_status);
......
...@@ -643,7 +643,7 @@ void ClientHandler::pool_downstream_connection( ...@@ -643,7 +643,7 @@ void ClientHandler::pool_downstream_connection(
dconn->set_client_handler(nullptr); dconn->set_client_handler(nullptr);
auto group = dconn->get_downstream_addr_group(); auto &group = dconn->get_downstream_addr_group();
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
CLOG(INFO, this) << "Pooling downstream connection DCONN:" << dconn.get() CLOG(INFO, this) << "Pooling downstream connection DCONN:" << dconn.get()
...@@ -1143,7 +1143,8 @@ void ClientHandler::write_accesslog(Downstream *downstream) { ...@@ -1143,7 +1143,8 @@ void ClientHandler::write_accesslog(Downstream *downstream) {
upstream_accesslog( upstream_accesslog(
get_config()->logging.access.format, get_config()->logging.access.format,
LogSpec{ LogSpec{
downstream, StringRef{ipaddr_}, http2::to_method_string(req.method), downstream, downstream->get_addr(), StringRef{ipaddr_},
http2::to_method_string(req.method),
req.method == HTTP_CONNECT req.method == HTTP_CONNECT
? StringRef(req.authority) ? StringRef(req.authority)
...@@ -1176,7 +1177,7 @@ void ClientHandler::write_accesslog(int major, int minor, unsigned int status, ...@@ -1176,7 +1177,7 @@ void ClientHandler::write_accesslog(int major, int minor, unsigned int status,
upstream_accesslog(get_config()->logging.access.format, upstream_accesslog(get_config()->logging.access.format,
LogSpec{ LogSpec{
nullptr, StringRef(ipaddr_), nullptr, nullptr, StringRef(ipaddr_),
StringRef::from_lit("-"), // method StringRef::from_lit("-"), // method
StringRef::from_lit("-"), // path, StringRef::from_lit("-"), // path,
StringRef(alpn_), nghttp2::ssl::get_tls_session_info( StringRef(alpn_), nghttp2::ssl::get_tls_session_info(
......
...@@ -434,6 +434,14 @@ LogFragmentType log_var_lookup_token(const char *name, size_t namelen) { ...@@ -434,6 +434,14 @@ LogFragmentType log_var_lookup_token(const char *name, size_t namelen) {
return SHRPX_LOGF_SSL_PROTOCOL; return SHRPX_LOGF_SSL_PROTOCOL;
} }
break; break;
case 't':
if (util::strieq_l("backend_hos", name, 11)) {
return SHRPX_LOGF_BACKEND_HOST;
}
if (util::strieq_l("backend_por", name, 11)) {
return SHRPX_LOGF_BACKEND_PORT;
}
break;
} }
break; break;
case 14: case 14:
......
...@@ -124,6 +124,7 @@ Downstream::Downstream(Upstream *upstream, MemchunkPool *mcpool, ...@@ -124,6 +124,7 @@ Downstream::Downstream(Upstream *upstream, MemchunkPool *mcpool,
response_buf_(mcpool), response_buf_(mcpool),
upstream_(upstream), upstream_(upstream),
blocked_link_(nullptr), blocked_link_(nullptr),
addr_(nullptr),
num_retry_(0), num_retry_(0),
stream_id_(stream_id), stream_id_(stream_id),
assoc_stream_id_(-1), assoc_stream_id_(-1),
...@@ -943,4 +944,13 @@ void Downstream::add_rcbuf(nghttp2_rcbuf *rcbuf) { ...@@ -943,4 +944,13 @@ void Downstream::add_rcbuf(nghttp2_rcbuf *rcbuf) {
rcbufs_.push_back(rcbuf); rcbufs_.push_back(rcbuf);
} }
void Downstream::set_downstream_addr_group(
const std::shared_ptr<DownstreamAddrGroup> &group) {
group_ = group;
}
void Downstream::set_addr(const DownstreamAddr *addr) { addr_ = addr; }
const DownstreamAddr *Downstream::get_addr() const { return addr_; }
} // namespace shrpx } // namespace shrpx
...@@ -49,6 +49,8 @@ namespace shrpx { ...@@ -49,6 +49,8 @@ namespace shrpx {
class Upstream; class Upstream;
class DownstreamConnection; class DownstreamConnection;
struct BlockedLink; struct BlockedLink;
struct DownstreamAddrGroup;
struct DownstreamAddr;
class FieldStore { class FieldStore {
public: public:
...@@ -382,6 +384,12 @@ public: ...@@ -382,6 +384,12 @@ public:
void add_rcbuf(nghttp2_rcbuf *rcbuf); void add_rcbuf(nghttp2_rcbuf *rcbuf);
void
set_downstream_addr_group(const std::shared_ptr<DownstreamAddrGroup> &group);
void set_addr(const DownstreamAddr *addr);
const DownstreamAddr *get_addr() const;
enum { enum {
EVENT_ERROR = 0x1, EVENT_ERROR = 0x1,
EVENT_TIMEOUT = 0x2, EVENT_TIMEOUT = 0x2,
...@@ -429,6 +437,10 @@ private: ...@@ -429,6 +437,10 @@ private:
// only used by HTTP/2 or SPDY upstream // only used by HTTP/2 or SPDY upstream
BlockedLink *blocked_link_; BlockedLink *blocked_link_;
// The backend address used to fulfill this request. These are for
// logging purpose.
std::shared_ptr<DownstreamAddrGroup> group_;
const DownstreamAddr *addr_;
// How many times we tried in backend connection // How many times we tried in backend connection
size_t num_retry_; size_t num_retry_;
// The stream ID in frontend connection // The stream ID in frontend connection
......
...@@ -61,7 +61,8 @@ public: ...@@ -61,7 +61,8 @@ public:
// true if this object is poolable. // true if this object is poolable.
virtual bool poolable() const = 0; virtual bool poolable() const = 0;
virtual DownstreamAddrGroup *get_downstream_addr_group() const = 0; virtual const std::shared_ptr<DownstreamAddrGroup> &
get_downstream_addr_group() const = 0;
virtual DownstreamAddr *get_addr() const = 0; virtual DownstreamAddr *get_addr() const = 0;
void set_client_handler(ClientHandler *client_handler); void set_client_handler(ClientHandler *client_handler);
......
...@@ -95,9 +95,10 @@ void HealthMonitorDownstreamConnection::on_upstream_change(Upstream *uptream) {} ...@@ -95,9 +95,10 @@ void HealthMonitorDownstreamConnection::on_upstream_change(Upstream *uptream) {}
bool HealthMonitorDownstreamConnection::poolable() const { return false; } bool HealthMonitorDownstreamConnection::poolable() const { return false; }
DownstreamAddrGroup * const std::shared_ptr<DownstreamAddrGroup> &
HealthMonitorDownstreamConnection::get_downstream_addr_group() const { HealthMonitorDownstreamConnection::get_downstream_addr_group() const {
return nullptr; static std::shared_ptr<DownstreamAddrGroup> s;
return s;
} }
DownstreamAddr *HealthMonitorDownstreamConnection::get_addr() const { DownstreamAddr *HealthMonitorDownstreamConnection::get_addr() const {
......
...@@ -54,7 +54,8 @@ public: ...@@ -54,7 +54,8 @@ public:
// true if this object is poolable. // true if this object is poolable.
virtual bool poolable() const; virtual bool poolable() const;
virtual DownstreamAddrGroup *get_downstream_addr_group() const; virtual const std::shared_ptr<DownstreamAddrGroup> &
get_downstream_addr_group() const;
virtual DownstreamAddr *get_addr() const; virtual DownstreamAddr *get_addr() const;
}; };
......
...@@ -538,7 +538,7 @@ int Http2DownstreamConnection::on_timeout() { ...@@ -538,7 +538,7 @@ int Http2DownstreamConnection::on_timeout() {
return submit_rst_stream(downstream_, NGHTTP2_NO_ERROR); return submit_rst_stream(downstream_, NGHTTP2_NO_ERROR);
} }
DownstreamAddrGroup * const std::shared_ptr<DownstreamAddrGroup> &
Http2DownstreamConnection::get_downstream_addr_group() const { Http2DownstreamConnection::get_downstream_addr_group() const {
return http2session_->get_downstream_addr_group(); return http2session_->get_downstream_addr_group();
} }
......
...@@ -64,7 +64,8 @@ public: ...@@ -64,7 +64,8 @@ public:
// migrate to another Http2Session object. // migrate to another Http2Session object.
virtual bool poolable() const { return false; } virtual bool poolable() const { return false; }
virtual DownstreamAddrGroup *get_downstream_addr_group() const; virtual const std::shared_ptr<DownstreamAddrGroup> &
get_downstream_addr_group() const;
virtual DownstreamAddr *get_addr() const; virtual DownstreamAddr *get_addr() const;
int send(); int send();
......
...@@ -976,6 +976,10 @@ int on_response_headers(Http2Session *http2session, Downstream *downstream, ...@@ -976,6 +976,10 @@ int on_response_headers(Http2Session *http2session, Downstream *downstream,
resp.http_major = 2; resp.http_major = 2;
resp.http_minor = 0; resp.http_minor = 0;
downstream->set_downstream_addr_group(
http2session->get_downstream_addr_group());
downstream->set_addr(http2session->get_addr());
if (LOG_ENABLED(INFO)) { if (LOG_ENABLED(INFO)) {
std::stringstream ss; std::stringstream ss;
for (auto &nv : nva) { for (auto &nv : nva) {
...@@ -2126,8 +2130,9 @@ bool Http2Session::max_concurrency_reached(size_t extra) const { ...@@ -2126,8 +2130,9 @@ bool Http2Session::max_concurrency_reached(size_t extra) const {
session_, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS); session_, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
} }
DownstreamAddrGroup *Http2Session::get_downstream_addr_group() const { const std::shared_ptr<DownstreamAddrGroup> &
return group_.get(); Http2Session::get_downstream_addr_group() const {
return group_;
} }
void Http2Session::add_to_avail_freelist() { void Http2Session::add_to_avail_freelist() {
......
...@@ -162,7 +162,7 @@ public: ...@@ -162,7 +162,7 @@ public:
DownstreamAddr *get_addr() const; DownstreamAddr *get_addr() const;
DownstreamAddrGroup *get_downstream_addr_group() const; const std::shared_ptr<DownstreamAddrGroup> &get_downstream_addr_group() const;
int handle_downstream_push_promise(Downstream *downstream, int handle_downstream_push_promise(Downstream *downstream,
int32_t promised_stream_id); int32_t promised_stream_id);
......
...@@ -565,7 +565,7 @@ int HttpDownstreamConnection::end_upload_data() { ...@@ -565,7 +565,7 @@ int HttpDownstreamConnection::end_upload_data() {
namespace { namespace {
void remove_from_pool(HttpDownstreamConnection *dconn) { void remove_from_pool(HttpDownstreamConnection *dconn) {
auto group = dconn->get_downstream_addr_group(); auto &group = dconn->get_downstream_addr_group();
auto &shared_addr = group->shared_addr; auto &shared_addr = group->shared_addr;
if (shared_addr->affinity == AFFINITY_NONE) { if (shared_addr->affinity == AFFINITY_NONE) {
...@@ -677,6 +677,11 @@ int htp_hdrs_completecb(http_parser *htp) { ...@@ -677,6 +677,11 @@ int htp_hdrs_completecb(http_parser *htp) {
resp.http_minor = 1; resp.http_minor = 1;
} }
auto dconn = downstream->get_downstream_connection();
downstream->set_downstream_addr_group(dconn->get_downstream_addr_group());
downstream->set_addr(dconn->get_addr());
if (resp.fs.parse_content_length() != 0) { if (resp.fs.parse_content_length() != 0) {
downstream->set_response_state(Downstream::MSG_BAD_HEADER); downstream->set_response_state(Downstream::MSG_BAD_HEADER);
return -1; return -1;
...@@ -1192,9 +1197,9 @@ int HttpDownstreamConnection::actual_signal_write() { ...@@ -1192,9 +1197,9 @@ int HttpDownstreamConnection::actual_signal_write() {
int HttpDownstreamConnection::noop() { return 0; } int HttpDownstreamConnection::noop() { return 0; }
DownstreamAddrGroup * const std::shared_ptr<DownstreamAddrGroup> &
HttpDownstreamConnection::get_downstream_addr_group() const { HttpDownstreamConnection::get_downstream_addr_group() const {
return group_.get(); return group_;
} }
DownstreamAddr *HttpDownstreamConnection::get_addr() const { return addr_; } DownstreamAddr *HttpDownstreamConnection::get_addr() const { return addr_; }
......
...@@ -64,7 +64,8 @@ public: ...@@ -64,7 +64,8 @@ public:
virtual bool poolable() const; virtual bool poolable() const;
virtual DownstreamAddrGroup *get_downstream_addr_group() const; virtual const std::shared_ptr<DownstreamAddrGroup> &
get_downstream_addr_group() const;
virtual DownstreamAddr *get_addr() const; virtual DownstreamAddr *get_addr() const;
int read_clear(); int read_clear();
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
#include "shrpx_config.h" #include "shrpx_config.h"
#include "shrpx_downstream.h" #include "shrpx_downstream.h"
#include "shrpx_worker.h"
#include "util.h" #include "util.h"
#include "template.h" #include "template.h"
...@@ -367,6 +368,21 @@ void upstream_accesslog(const std::vector<LogFragment> &lfv, ...@@ -367,6 +368,21 @@ void upstream_accesslog(const std::vector<LogFragment> &lfv,
std::tie(p, avail) = std::tie(p, avail) =
copy_l(lgsp.tls_info->session_reused ? "r" : ".", avail, p); copy_l(lgsp.tls_info->session_reused ? "r" : ".", avail, p);
break; break;
case SHRPX_LOGF_BACKEND_HOST:
if (!lgsp.downstream_addr) {
std::tie(p, avail) = copy_l("-", avail, p);
break;
}
std::tie(p, avail) = copy(lgsp.downstream_addr->host, avail, p);
break;
case SHRPX_LOGF_BACKEND_PORT:
if (!lgsp.downstream_addr) {
std::tie(p, avail) = copy_l("-", avail, p);
break;
}
std::tie(p, avail) =
copy(util::utos(lgsp.downstream_addr->port), avail, p);
break;
case SHRPX_LOGF_NONE: case SHRPX_LOGF_NONE:
break; break;
default: default:
......
...@@ -85,6 +85,7 @@ using namespace nghttp2; ...@@ -85,6 +85,7 @@ using namespace nghttp2;
namespace shrpx { namespace shrpx {
class Downstream; class Downstream;
struct DownstreamAddr;
enum SeverityLevel { INFO, NOTICE, WARN, ERROR, FATAL }; enum SeverityLevel { INFO, NOTICE, WARN, ERROR, FATAL };
...@@ -131,6 +132,8 @@ enum LogFragmentType { ...@@ -131,6 +132,8 @@ enum LogFragmentType {
SHRPX_LOGF_SSL_PROTOCOL, SHRPX_LOGF_SSL_PROTOCOL,
SHRPX_LOGF_SSL_SESSION_ID, SHRPX_LOGF_SSL_SESSION_ID,
SHRPX_LOGF_SSL_SESSION_REUSED, SHRPX_LOGF_SSL_SESSION_REUSED,
SHRPX_LOGF_BACKEND_HOST,
SHRPX_LOGF_BACKEND_PORT,
}; };
struct LogFragment { struct LogFragment {
...@@ -142,6 +145,7 @@ struct LogFragment { ...@@ -142,6 +145,7 @@ struct LogFragment {
struct LogSpec { struct LogSpec {
Downstream *downstream; Downstream *downstream;
const DownstreamAddr *downstream_addr;
StringRef remote_addr; StringRef remote_addr;
StringRef method; StringRef method;
StringRef path; StringRef path;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment