1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelmux
import (
"bufio"
"context"
"io"
"net"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
var sc = trace.NewSpanContext(trace.SpanContextConfig{
TraceID: [16]byte{1},
SpanID: [8]byte{1},
Remote: true,
TraceFlags: trace.FlagsSampled,
})
func TestPassthroughSpanFromGlobalTracer(t *testing.T) {
var called bool
router := mux.NewRouter()
router.Use(Middleware("foobar"))
// The default global TracerProvider provides "pass through" spans for any
// span context in the incoming request context.
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
got := trace.SpanFromContext(r.Context()).SpanContext()
assert.Equal(t, sc, got)
w.WriteHeader(http.StatusOK)
}))
r := httptest.NewRequest("GET", "/user/123", nil)
r = r.WithContext(trace.ContextWithRemoteSpanContext(context.Background(), sc))
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
assert.True(t, called, "failed to run test")
}
func TestPropagationWithGlobalPropagators(t *testing.T) {
defer func(p propagation.TextMapPropagator) {
otel.SetTextMapPropagator(p)
}(otel.GetTextMapPropagator())
prop := propagation.TraceContext{}
otel.SetTextMapPropagator(prop)
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header))
var called bool
router := mux.NewRouter()
router.Use(Middleware("foobar"))
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
span := trace.SpanFromContext(r.Context())
assert.Equal(t, sc, span.SpanContext())
w.WriteHeader(http.StatusOK)
}))
router.ServeHTTP(w, r)
assert.True(t, called, "failed to run test")
}
func TestPropagationWithCustomPropagators(t *testing.T) {
prop := propagation.TraceContext{}
r := httptest.NewRequest("GET", "/user/123", nil)
w := httptest.NewRecorder()
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
prop.Inject(ctx, propagation.HeaderCarrier(r.Header))
var called bool
router := mux.NewRouter()
router.Use(Middleware("foobar", WithPropagators(prop)))
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
span := trace.SpanFromContext(r.Context())
assert.Equal(t, sc, span.SpanContext())
w.WriteHeader(http.StatusOK)
}))
router.ServeHTTP(w, r)
assert.True(t, called, "failed to run test")
}
type testResponseWriter struct {
writer http.ResponseWriter
}
func (rw *testResponseWriter) Header() http.Header {
return rw.writer.Header()
}
func (rw *testResponseWriter) Write(b []byte) (int, error) {
return rw.writer.Write(b)
}
func (rw *testResponseWriter) WriteHeader(statusCode int) {
rw.writer.WriteHeader(statusCode)
}
// implement Hijacker.
func (rw *testResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return nil, nil, nil
}
// implement Pusher.
func (rw *testResponseWriter) Push(target string, opts *http.PushOptions) error {
return nil
}
// implement Flusher.
func (rw *testResponseWriter) Flush() {
}
// implement io.ReaderFrom.
func (rw *testResponseWriter) ReadFrom(r io.Reader) (n int64, err error) {
return 0, nil
}
func TestResponseWriterInterfaces(t *testing.T) {
// make sure the recordingResponseWriter preserves interfaces implemented by the wrapped writer
router := mux.NewRouter()
router.Use(Middleware("foobar"))
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Implements(t, (*http.Hijacker)(nil), w)
assert.Implements(t, (*http.Pusher)(nil), w)
assert.Implements(t, (*http.Flusher)(nil), w)
assert.Implements(t, (*io.ReaderFrom)(nil), w)
w.WriteHeader(http.StatusOK)
}))
r := httptest.NewRequest("GET", "/user/123", nil)
w := &testResponseWriter{
writer: httptest.NewRecorder(),
}
router.ServeHTTP(w, r)
}
func TestFilter(t *testing.T) {
prop := propagation.TraceContext{}
router := mux.NewRouter()
var calledHealth, calledTest int
router.Use(Middleware("foobar", WithFilter(func(r *http.Request) bool {
return r.URL.Path != "/health"
})))
router.HandleFunc("/health", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calledHealth++
span := trace.SpanFromContext(r.Context())
assert.NotEqual(t, sc, span.SpanContext())
w.WriteHeader(http.StatusOK)
}))
router.HandleFunc("/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calledTest++
span := trace.SpanFromContext(r.Context())
assert.Equal(t, sc, span.SpanContext())
w.WriteHeader(http.StatusOK)
}))
r := httptest.NewRequest("GET", "/health", nil)
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
prop.Inject(ctx, propagation.HeaderCarrier(r.Header))
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
r = httptest.NewRequest("GET", "/test", nil)
ctx = trace.ContextWithRemoteSpanContext(context.Background(), sc)
prop.Inject(ctx, propagation.HeaderCarrier(r.Header))
w = httptest.NewRecorder()
router.ServeHTTP(w, r)
assert.Equal(t, 1, calledHealth, "failed to run test")
assert.Equal(t, 1, calledTest, "failed to run test")
}
|