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

server/requestlog: Add Request Context in requestlog.Entry #2983

Merged
merged 1 commit into from Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 31 additions & 10 deletions server/requestlog/requestlog.go
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
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