diff --git a/server/requestlog/requestlog.go b/server/requestlog/requestlog.go index 4f97ec6b49..310b7b5b1e 100644 --- a/server/requestlog/requestlog.go +++ b/server/requestlog/requestlog.go @@ -58,6 +58,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { start := time.Now() sc := trace.FromContext(r.Context()).SpanContext() ent := &Entry{ + Request: cloneRequestWithoutBody(r), ReceivedTime: start, RequestMethod: r.Method, RequestURL: r.URL.String(), @@ -95,19 +96,21 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.log.Log(ent) } +func cloneRequestWithoutBody(r *http.Request) *http.Request { + r = r.Clone(r.Context()) + r.Body = nil + return r +} + // Entry records information about a completed HTTP request. type Entry struct { - ReceivedTime time.Time - RequestMethod string - RequestURL string - RequestHeaderSize int64 - RequestBodySize int64 - UserAgent string - Referer string - Proto string + // Request is the http request that has been completed. + // + // This request's Body is always nil, regardless of the actual request body. + Request *http.Request - RemoteIP string - ServerIP string + ReceivedTime time.Time + RequestBodySize int64 Status int ResponseHeaderSize int64 @@ -115,6 +118,24 @@ type Entry struct { Latency time.Duration TraceID trace.TraceID SpanID trace.SpanID + + // Deprecated. This value is available by evaluating Request.Referer(). + Referer string + // Deprecated. This value is available directing in Request.Proto. + Proto string + // Deprecated. This value is available directly in Request.Method. + RequestMethod string + // Deprecated. This value is available directly in Request.URL. + RequestURL string + // Deprecated. This value is available by evaluating Request.Header. + RequestHeaderSize int64 + // Deprecated. This value is available by evaluating Request.Header. + UserAgent string + // Deprecated. This value is available by evaluating Request.RemoteAddr.. + RemoteIP string + // Deprecated. This value is available by evaluating reading the + // http.LocalAddrContextKey value from the context returned by Request.Context(). + ServerIP string } func ipFromHostPort(hp string) string { diff --git a/server/requestlog/requestlog_test.go b/server/requestlog/requestlog_test.go index 945ec9da0f..f9283c1aaa 100644 --- a/server/requestlog/requestlog_test.go +++ b/server/requestlog/requestlog_test.go @@ -15,8 +15,10 @@ package requestlog import ( + "context" "fmt" "io" + "net" "net/http" "net/http/httptest" "strings" @@ -46,6 +48,12 @@ func TestHandler(t *testing.T) { if err != nil { t.Fatal("Could not get entry:", err) } + if want := "test-baggage"; ent.Request.Context().Value("baggage") != want { + t.Errorf("Request Context Value = %s; want %s", ent.Request.Context().Value("baggage"), want) + } + if want := "/foo"; ent.Request.URL.Path != want { + t.Errorf("Request Context Value = %s; want %s", ent.Request.Context().Value("baggage"), want) + } if want := "POST"; ent.RequestMethod != want { t.Errorf("RequestMethod = %q; want %q", ent.RequestMethod, want) } @@ -102,7 +110,12 @@ func roundTrip(r *http.Request, h http.Handler) (*Entry, *trace.SpanContext, err capture := new(captureLogger) hh := NewHandler(capture, h) handler := &testSpanHandler{h: hh} - s := httptest.NewServer(handler) + s := httptest.NewUnstartedServer(handler) + s.Config.ConnContext = func(ctx context.Context, c net.Conn) context.Context { + ctx = context.WithValue(ctx, "baggage", "test-baggage") + return ctx + } + s.Start() defer s.Close() r.URL.Host = s.URL[len("http://"):] resp, err := http.DefaultClient.Do(r)