Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log client name and version #727

Merged
merged 3 commits into from
Jan 10, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions services/horizon/internal/middleware_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"github.com/stellar/go/support/log"
)

const (
clientNameHeader = "X-Client-Name"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any documentations about these two fields? I couldn't find the information about the client who would send request with these header fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about documenting these fields. Added a new item to #725.

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 {
Expand All @@ -39,29 +44,44 @@ 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{
"path": r.URL.String(),
"method": r.Method,
"ip": remoteAddrIP(r),
"ip_port": r.RemoteAddr,
"forwarded_ip": firstXForwardedFor(r),
"host": r.Host,
"client_name": getClientData(r, clientNameHeader),
"client_version": getClientData(r, 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": getClientData(r, clientNameHeader),
"client_version": getClientData(r, 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")
}