| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelmux |
| 5 |
|
| 6 |
import ( |
| 7 |
"bufio" |
| 8 |
"io" |
| 9 |
"net" |
| 10 |
"net/http" |
| 11 |
"net/http/httptest" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/gorilla/mux" |
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"go.opentelemetry.io/otel" |
| 18 |
"go.opentelemetry.io/otel/propagation" |
| 19 |
"go.opentelemetry.io/otel/trace" |
| 20 |
) |
| 21 |
|
| 22 |
var sc = trace.NewSpanContext(trace.SpanContextConfig{ |
| 23 |
TraceID: [16]byte{1}, |
| 24 |
SpanID: [8]byte{1}, |
| 25 |
Remote: true, |
| 26 |
TraceFlags: trace.FlagsSampled, |
| 27 |
}) |
| 28 |
|
| 29 |
func TestPassthroughSpanFromGlobalTracer(t *testing.T) { |
| 30 |
var called bool |
| 31 |
router := mux.NewRouter() |
| 32 |
router.Use(Middleware("foobar")) |
| 33 |
|
| 34 |
|
| 35 |
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 36 |
called = true |
| 37 |
got := trace.SpanFromContext(r.Context()).SpanContext() |
| 38 |
assert.Equal(t, sc, got) |
| 39 |
w.WriteHeader(http.StatusOK) |
| 40 |
})) |
| 41 |
|
| 42 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 43 |
r = r.WithContext(trace.ContextWithRemoteSpanContext(t.Context(), sc)) |
| 44 |
w := httptest.NewRecorder() |
| 45 |
|
| 46 |
router.ServeHTTP(w, r) |
| 47 |
assert.True(t, called, "failed to run test") |
| 48 |
} |
| 49 |
|
| 50 |
func TestPropagationWithGlobalPropagators(t *testing.T) { |
| 51 |
defer func(p propagation.TextMapPropagator) { |
| 52 |
otel.SetTextMapPropagator(p) |
| 53 |
}(otel.GetTextMapPropagator()) |
| 54 |
|
| 55 |
prop := propagation.TraceContext{} |
| 56 |
otel.SetTextMapPropagator(prop) |
| 57 |
|
| 58 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 59 |
w := httptest.NewRecorder() |
| 60 |
|
| 61 |
ctx := trace.ContextWithRemoteSpanContext(t.Context(), sc) |
| 62 |
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header)) |
| 63 |
|
| 64 |
var called bool |
| 65 |
router := mux.NewRouter() |
| 66 |
router.Use(Middleware("foobar")) |
| 67 |
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 68 |
called = true |
| 69 |
span := trace.SpanFromContext(r.Context()) |
| 70 |
assert.Equal(t, sc, span.SpanContext()) |
| 71 |
w.WriteHeader(http.StatusOK) |
| 72 |
})) |
| 73 |
|
| 74 |
router.ServeHTTP(w, r) |
| 75 |
assert.True(t, called, "failed to run test") |
| 76 |
} |
| 77 |
|
| 78 |
func TestPropagationWithCustomPropagators(t *testing.T) { |
| 79 |
prop := propagation.TraceContext{} |
| 80 |
|
| 81 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 82 |
w := httptest.NewRecorder() |
| 83 |
|
| 84 |
ctx := trace.ContextWithRemoteSpanContext(t.Context(), sc) |
| 85 |
prop.Inject(ctx, propagation.HeaderCarrier(r.Header)) |
| 86 |
|
| 87 |
var called bool |
| 88 |
router := mux.NewRouter() |
| 89 |
router.Use(Middleware("foobar", WithPropagators(prop))) |
| 90 |
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 91 |
called = true |
| 92 |
span := trace.SpanFromContext(r.Context()) |
| 93 |
assert.Equal(t, sc, span.SpanContext()) |
| 94 |
w.WriteHeader(http.StatusOK) |
| 95 |
})) |
| 96 |
|
| 97 |
router.ServeHTTP(w, r) |
| 98 |
assert.True(t, called, "failed to run test") |
| 99 |
} |
| 100 |
|
| 101 |
type testResponseWriter struct { |
| 102 |
writer http.ResponseWriter |
| 103 |
} |
| 104 |
|
| 105 |
func (rw *testResponseWriter) Header() http.Header { |
| 106 |
return rw.writer.Header() |
| 107 |
} |
| 108 |
|
| 109 |
func (rw *testResponseWriter) Write(b []byte) (int, error) { |
| 110 |
return rw.writer.Write(b) |
| 111 |
} |
| 112 |
|
| 113 |
func (rw *testResponseWriter) WriteHeader(statusCode int) { |
| 114 |
rw.writer.WriteHeader(statusCode) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func (*testResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { |
| 119 |
return nil, nil, nil |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
func (*testResponseWriter) Push(string, *http.PushOptions) error { |
| 124 |
return nil |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
func (*testResponseWriter) Flush() { |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (*testResponseWriter) ReadFrom(io.Reader) (int64, error) { |
| 133 |
return 0, nil |
| 134 |
} |
| 135 |
|
| 136 |
func TestResponseWriterInterfaces(t *testing.T) { |
| 137 |
|
| 138 |
router := mux.NewRouter() |
| 139 |
router.Use(Middleware("foobar")) |
| 140 |
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 141 |
assert.Implements(t, (*http.Hijacker)(nil), w) |
| 142 |
assert.Implements(t, (*http.Pusher)(nil), w) |
| 143 |
assert.Implements(t, (*http.Flusher)(nil), w) |
| 144 |
assert.Implements(t, (*io.ReaderFrom)(nil), w) |
| 145 |
w.WriteHeader(http.StatusOK) |
| 146 |
})) |
| 147 |
|
| 148 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 149 |
w := &testResponseWriter{ |
| 150 |
writer: httptest.NewRecorder(), |
| 151 |
} |
| 152 |
|
| 153 |
router.ServeHTTP(w, r) |
| 154 |
} |
| 155 |
|
| 156 |
func TestFilter(t *testing.T) { |
| 157 |
prop := propagation.TraceContext{} |
| 158 |
|
| 159 |
router := mux.NewRouter() |
| 160 |
var calledHealth, calledTest int |
| 161 |
router.Use(Middleware("foobar", WithFilter(func(r *http.Request) bool { |
| 162 |
return r.URL.Path != "/health" |
| 163 |
}))) |
| 164 |
router.HandleFunc("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 165 |
calledHealth++ |
| 166 |
span := trace.SpanFromContext(r.Context()) |
| 167 |
assert.NotEqual(t, sc, span.SpanContext()) |
| 168 |
w.WriteHeader(http.StatusOK) |
| 169 |
})) |
| 170 |
router.HandleFunc("/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 171 |
calledTest++ |
| 172 |
span := trace.SpanFromContext(r.Context()) |
| 173 |
assert.Equal(t, sc, span.SpanContext()) |
| 174 |
w.WriteHeader(http.StatusOK) |
| 175 |
})) |
| 176 |
|
| 177 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/health", http.NoBody) |
| 178 |
ctx := trace.ContextWithRemoteSpanContext(t.Context(), sc) |
| 179 |
prop.Inject(ctx, propagation.HeaderCarrier(r.Header)) |
| 180 |
w := httptest.NewRecorder() |
| 181 |
router.ServeHTTP(w, r) |
| 182 |
|
| 183 |
r = httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/test", http.NoBody) |
| 184 |
ctx = trace.ContextWithRemoteSpanContext(t.Context(), sc) |
| 185 |
prop.Inject(ctx, propagation.HeaderCarrier(r.Header)) |
| 186 |
w = httptest.NewRecorder() |
| 187 |
router.ServeHTTP(w, r) |
| 188 |
|
| 189 |
assert.Equal(t, 1, calledHealth, "failed to run test") |
| 190 |
assert.Equal(t, 1, calledTest, "failed to run test") |
| 191 |
} |
| 192 |
|
| 193 |
func TestPassthroughSpanFromGlobalTracerWithBody(t *testing.T) { |
| 194 |
expectedBody := `{"message":"successfully"}` |
| 195 |
router := mux.NewRouter() |
| 196 |
router.Use(Middleware("foobar")) |
| 197 |
|
| 198 |
var called bool |
| 199 |
router.HandleFunc("/user", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 200 |
called = true |
| 201 |
got := trace.SpanFromContext(r.Context()).SpanContext() |
| 202 |
assert.Equal(t, sc, got) |
| 203 |
|
| 204 |
body, err := io.ReadAll(r.Body) |
| 205 |
assert.NoError(t, err) |
| 206 |
defer r.Body.Close() |
| 207 |
|
| 208 |
assert.JSONEq(t, `{"name":"John Doe","age":30}`, string(body), "request body does not match") |
| 209 |
|
| 210 |
w.Header().Set("Content-Type", "application/json") |
| 211 |
w.WriteHeader(http.StatusCreated) |
| 212 |
_, err = w.Write([]byte(expectedBody)) |
| 213 |
assert.NoError(t, err) |
| 214 |
})).Methods(http.MethodPost) |
| 215 |
|
| 216 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/user", strings.NewReader(`{"name":"John Doe","age":30}`)) |
| 217 |
r.Header.Set("Content-Type", "application/json") |
| 218 |
r = r.WithContext(trace.ContextWithRemoteSpanContext(t.Context(), sc)) |
| 219 |
w := httptest.NewRecorder() |
| 220 |
|
| 221 |
router.ServeHTTP(w, r) |
| 222 |
|
| 223 |
|
| 224 |
assert.True(t, called, "failed to run test") |
| 225 |
assert.Equal(t, http.StatusCreated, w.Code, "unexpected status code") |
| 226 |
assert.JSONEq(t, expectedBody, w.Body.String(), "unexpected response body") |
| 227 |
} |
| 228 |
|
| 229 |
func TestHeaderAlreadyWrittenWhenFlushing(t *testing.T) { |
| 230 |
var called bool |
| 231 |
|
| 232 |
router := mux.NewRouter() |
| 233 |
router.Use(Middleware("foobar")) |
| 234 |
|
| 235 |
router.HandleFunc("/user/{id}", func(w http.ResponseWriter, _ *http.Request) { |
| 236 |
called = true |
| 237 |
|
| 238 |
w.WriteHeader(http.StatusBadRequest) |
| 239 |
f := w.(http.Flusher) |
| 240 |
f.Flush() |
| 241 |
}) |
| 242 |
|
| 243 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 244 |
r = r.WithContext(trace.ContextWithRemoteSpanContext(t.Context(), sc)) |
| 245 |
w := httptest.NewRecorder() |
| 246 |
|
| 247 |
router.ServeHTTP(w, r) |
| 248 |
|
| 249 |
|
| 250 |
assert.True(t, called, "failed to run test") |
| 251 |
assert.Equal(t, http.StatusBadRequest, w.Code, "Header was not set before flushing") |
| 252 |
} |