| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package requestlog |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"go.opentelemetry.io/otel" |
| 21 |
"io" |
| 22 |
"net" |
| 23 |
"net/http" |
| 24 |
"net/http/httptest" |
| 25 |
"strings" |
| 26 |
"testing" |
| 27 |
|
| 28 |
"go.opentelemetry.io/otel/trace" |
| 29 |
) |
| 30 |
|
| 31 |
func TestHandler(t *testing.T) { |
| 32 |
const requestMsg = "Hello, World!" |
| 33 |
const responseMsg = "I see you." |
| 34 |
const userAgent = "Request Log Test UA" |
| 35 |
const referer = "http://www.example.com/" |
| 36 |
r, err := http.NewRequest("POST", "http://localhost/foo", strings.NewReader(requestMsg)) |
| 37 |
if err != nil { |
| 38 |
t.Fatal("NewRequest:", err) |
| 39 |
} |
| 40 |
r.Header.Set("User-Agent", userAgent) |
| 41 |
r.Header.Set("Referer", referer) |
| 42 |
requestHdrSize := len(fmt.Sprintf("User-Agent: %s\r\nReferer: %s\r\nContent-Length: %v\r\n", userAgent, referer, len(requestMsg))) |
| 43 |
responseHdrSize := len(fmt.Sprintf("Content-Length: %v\r\n", len(responseMsg))) |
| 44 |
ent, spanCtx, err := roundTrip(r, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 |
w.Header().Set("Content-Length", fmt.Sprint(len(responseMsg))) |
| 46 |
w.WriteHeader(http.StatusOK) |
| 47 |
_, _ = io.WriteString(w, responseMsg) |
| 48 |
})) |
| 49 |
if err != nil { |
| 50 |
t.Fatal("Could not get entry:", err) |
| 51 |
} |
| 52 |
if want := "test-baggage"; ent.Request.Context().Value(testContextKey) != want { |
| 53 |
t.Errorf("Request Context Value = %s; want %s", ent.Request.Context().Value(testContextKey), want) |
| 54 |
} |
| 55 |
if want := "/foo"; ent.Request.URL.Path != want { |
| 56 |
t.Errorf("Request Context Value = %s; want %s", ent.Request.Context().Value(testContextKey), want) |
| 57 |
} |
| 58 |
if want := "POST"; ent.RequestMethod != want { |
| 59 |
t.Errorf("RequestMethod = %q; want %q", ent.RequestMethod, want) |
| 60 |
} |
| 61 |
if want := "/foo"; ent.RequestURL != want { |
| 62 |
t.Errorf("RequestURL = %q; want %q", ent.RequestURL, want) |
| 63 |
} |
| 64 |
if ent.RequestHeaderSize < int64(requestHdrSize) { |
| 65 |
t.Errorf("RequestHeaderSize = %d; want >=%d", ent.RequestHeaderSize, requestHdrSize) |
| 66 |
} |
| 67 |
if ent.RequestBodySize != int64(len(requestMsg)) { |
| 68 |
t.Errorf("RequestBodySize = %d; want %d", ent.RequestBodySize, len(requestMsg)) |
| 69 |
} |
| 70 |
if ent.UserAgent != userAgent { |
| 71 |
t.Errorf("UserAgent = %q; want %q", ent.UserAgent, userAgent) |
| 72 |
} |
| 73 |
if ent.Referer != referer { |
| 74 |
t.Errorf("Referer = %q; want %q", ent.Referer, referer) |
| 75 |
} |
| 76 |
if want := "HTTP/1.1"; ent.Proto != want { |
| 77 |
t.Errorf("Proto = %q; want %q", ent.Proto, want) |
| 78 |
} |
| 79 |
if ent.Status != http.StatusOK { |
| 80 |
t.Errorf("Status = %d; want %d", ent.Status, http.StatusOK) |
| 81 |
} |
| 82 |
if ent.ResponseHeaderSize < int64(responseHdrSize) { |
| 83 |
t.Errorf("ResponseHeaderSize = %d; want >=%d", ent.ResponseHeaderSize, responseHdrSize) |
| 84 |
} |
| 85 |
if ent.ResponseBodySize != int64(len(responseMsg)) { |
| 86 |
t.Errorf("ResponseBodySize = %d; want %d", ent.ResponseBodySize, len(responseMsg)) |
| 87 |
} |
| 88 |
if ent.TraceID != spanCtx.TraceID() { |
| 89 |
t.Errorf("TraceID = %v; want %v", ent.TraceID, spanCtx.TraceID()) |
| 90 |
} |
| 91 |
if ent.SpanID != spanCtx.SpanID() { |
| 92 |
t.Errorf("SpanID = %v; want %v", ent.SpanID, spanCtx.SpanID()) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
type testSpanHandler struct { |
| 97 |
h http.Handler |
| 98 |
spanCtx *trace.SpanContext |
| 99 |
} |
| 100 |
|
| 101 |
func (sh *testSpanHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 102 |
tracer := otel.Tracer("test") |
| 103 |
ctx, span := tracer.Start(r.Context(), "test") |
| 104 |
defer span.End() |
| 105 |
sc := trace.SpanContextFromContext(ctx) |
| 106 |
|
| 107 |
sh.spanCtx = &sc |
| 108 |
r = r.WithContext(ctx) |
| 109 |
|
| 110 |
sh.h.ServeHTTP(w, r) |
| 111 |
} |
| 112 |
|
| 113 |
type contextKey string |
| 114 |
|
| 115 |
const testContextKey = contextKey("baggage") |
| 116 |
|
| 117 |
func roundTrip(r *http.Request, h http.Handler) (*Entry, *trace.SpanContext, error) { |
| 118 |
capture := new(captureLogger) |
| 119 |
hh := NewHandler(capture, h) |
| 120 |
handler := &testSpanHandler{h: hh} |
| 121 |
s := httptest.NewUnstartedServer(handler) |
| 122 |
s.Config.ConnContext = func(ctx context.Context, c net.Conn) context.Context { |
| 123 |
ctx = context.WithValue(ctx, testContextKey, "test-baggage") |
| 124 |
return ctx |
| 125 |
} |
| 126 |
s.Start() |
| 127 |
defer s.Close() |
| 128 |
r.URL.Host = s.URL[len("http://"):] |
| 129 |
resp, err := http.DefaultClient.Do(r) |
| 130 |
if err != nil { |
| 131 |
return nil, nil, err |
| 132 |
} |
| 133 |
resp.Body.Close() |
| 134 |
return &capture.ent, handler.spanCtx, nil |
| 135 |
} |
| 136 |
|
| 137 |
type captureLogger struct { |
| 138 |
ent Entry |
| 139 |
} |
| 140 |
|
| 141 |
func (cl *captureLogger) Log(ent *Entry) { |
| 142 |
cl.ent = *ent |
| 143 |
} |