Skip to content

Commit

Permalink
server/requestlog: Pass Request into Logger via new field in Entry
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyreiss committed Apr 3, 2021
1 parent 6f954e2 commit 857fc15
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
41 changes: 31 additions & 10 deletions server/requestlog/requestlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -95,26 +96,46 @@ 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
ResponseBodySize int64
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 {
Expand Down
15 changes: 14 additions & 1 deletion server/requestlog/requestlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package requestlog

import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 857fc15

Please sign in to comment.