From a9fa46be95c16a477ce5ae1d540351dbb1b8e0cc Mon Sep 17 00:00:00 2001 From: Bartek Nowotarski Date: Thu, 25 Oct 2018 16:05:30 +0200 Subject: [PATCH 1/2] Log client name and version --- .../horizon/internal/middleware_logger.go | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/services/horizon/internal/middleware_logger.go b/services/horizon/internal/middleware_logger.go index 5425518e28..bc8694cf2a 100644 --- a/services/horizon/internal/middleware_logger.go +++ b/services/horizon/internal/middleware_logger.go @@ -13,6 +13,11 @@ import ( "github.com/stellar/go/support/log" ) +const ( + clientNameHeader = "X-Client-Name" + clientVersionHeader = "X-Client-Version" +) + // LoggerMiddleware is the middleware that logs http requests and resposnes // to the logging subsytem of horizon. func LoggerMiddleware(h http.Handler) http.Handler { @@ -41,27 +46,31 @@ func LoggerMiddleware(h http.Handler) http.Handler { func logStartOfRequest(ctx context.Context, r *http.Request) { log.Ctx(ctx).WithFields(log.F{ - "path": r.URL.String(), - "method": r.Method, - "ip": remoteAddrIP(r), - "ip_port": r.RemoteAddr, - "forwarded_ip": firstXForwardedFor(r), - "host": r.Host, + "client_name": r.Header.Get(clientNameHeader), + "client_version": r.Header.Get(clientVersionHeader), + "forwarded_ip": firstXForwardedFor(r), + "host": r.Host, + "ip": remoteAddrIP(r), + "ip_port": r.RemoteAddr, + "method": r.Method, + "path": r.URL.String(), }).Info("Starting request") } func logEndOfRequest(ctx context.Context, r *http.Request, duration time.Duration, mw middleware.WrapResponseWriter, streaming bool) { log.Ctx(ctx).WithFields(log.F{ - "path": r.URL.String(), - "route": chi.RouteContext(r.Context()).RoutePattern(), - "method": r.Method, - "ip": remoteAddrIP(r), - "ip_port": r.RemoteAddr, - "forwarded_ip": firstXForwardedFor(r), - "host": r.Host, - "status": mw.Status(), - "bytes": mw.BytesWritten(), - "duration": duration.Seconds(), - "streaming": streaming, + "bytes": mw.BytesWritten(), + "client_name": r.Header.Get(clientNameHeader), + "client_version": r.Header.Get(clientVersionHeader), + "duration": duration.Seconds(), + "forwarded_ip": firstXForwardedFor(r), + "host": r.Host, + "ip": remoteAddrIP(r), + "ip_port": r.RemoteAddr, + "method": r.Method, + "path": r.URL.String(), + "route": chi.RouteContext(r.Context()).RoutePattern(), + "status": mw.Status(), + "streaming": streaming, }).Info("Finished request") } From 76940be41f1818cd83b63e8e4a5d95207b5f7e04 Mon Sep 17 00:00:00 2001 From: Bartek Nowotarski Date: Thu, 25 Oct 2018 17:33:09 +0200 Subject: [PATCH 2/2] Check GET values --- .../horizon/internal/middleware_logger.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/services/horizon/internal/middleware_logger.go b/services/horizon/internal/middleware_logger.go index bc8694cf2a..4af01f12bf 100644 --- a/services/horizon/internal/middleware_logger.go +++ b/services/horizon/internal/middleware_logger.go @@ -44,10 +44,21 @@ func LoggerMiddleware(h http.Handler) http.Handler { return http.HandlerFunc(fn) } +// getClientData gets client data (name or version) from header or GET parameter +// (useful when not possible to set headers, like in EventStream). +func getClientData(r *http.Request, headerName string) string { + value := r.Header.Get(headerName) + if value != "" { + return value + } + + return r.URL.Query().Get(headerName) +} + func logStartOfRequest(ctx context.Context, r *http.Request) { log.Ctx(ctx).WithFields(log.F{ - "client_name": r.Header.Get(clientNameHeader), - "client_version": r.Header.Get(clientVersionHeader), + "client_name": getClientData(r, clientNameHeader), + "client_version": getClientData(r, clientVersionHeader), "forwarded_ip": firstXForwardedFor(r), "host": r.Host, "ip": remoteAddrIP(r), @@ -60,8 +71,8 @@ func logStartOfRequest(ctx context.Context, r *http.Request) { func logEndOfRequest(ctx context.Context, r *http.Request, duration time.Duration, mw middleware.WrapResponseWriter, streaming bool) { log.Ctx(ctx).WithFields(log.F{ "bytes": mw.BytesWritten(), - "client_name": r.Header.Get(clientNameHeader), - "client_version": r.Header.Get(clientVersionHeader), + "client_name": getClientData(r, clientNameHeader), + "client_version": getClientData(r, clientVersionHeader), "duration": duration.Seconds(), "forwarded_ip": firstXForwardedFor(r), "host": r.Host,