| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelhttp_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"io" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"go.opentelemetry.io/otel/sdk/trace" |
| 14 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 15 |
|
| 16 |
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" |
| 17 |
) |
| 18 |
|
| 19 |
func TestBasicFilter(t *testing.T) { |
| 20 |
rr := httptest.NewRecorder() |
| 21 |
|
| 22 |
spanRecorder := tracetest.NewSpanRecorder() |
| 23 |
provider := trace.NewTracerProvider(trace.WithSpanProcessor(spanRecorder)) |
| 24 |
|
| 25 |
h := otelhttp.NewHandler( |
| 26 |
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 27 |
if _, err := io.WriteString(w, "hello world"); err != nil { |
| 28 |
t.Fatal(err) |
| 29 |
} |
| 30 |
}), "test_handler", |
| 31 |
otelhttp.WithTracerProvider(provider), |
| 32 |
otelhttp.WithFilter(func(*http.Request) bool { |
| 33 |
return false |
| 34 |
}), |
| 35 |
) |
| 36 |
|
| 37 |
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://localhost/", http.NoBody) |
| 38 |
if err != nil { |
| 39 |
t.Fatal(err) |
| 40 |
} |
| 41 |
h.ServeHTTP(rr, r) |
| 42 |
if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected { |
| 43 |
t.Fatalf("got %d, expected %d", got, expected) |
| 44 |
} |
| 45 |
if got := rr.Header().Get("Traceparent"); got != "" { |
| 46 |
t.Fatal("expected empty trace header") |
| 47 |
} |
| 48 |
if got, expected := len(spanRecorder.Ended()), 0; got != expected { |
| 49 |
t.Fatalf("got %d recorded spans, expected %d", got, expected) |
| 50 |
} |
| 51 |
d, err := io.ReadAll(rr.Result().Body) |
| 52 |
if err != nil { |
| 53 |
t.Fatal(err) |
| 54 |
} |
| 55 |
if got, expected := string(d), "hello world"; got != expected { |
| 56 |
t.Fatalf("got %q, expected %q", got, expected) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func TestSpanNameFormatter(t *testing.T) { |
| 61 |
testCases := []struct { |
| 62 |
name string |
| 63 |
formatter func(s string, r *http.Request) string |
| 64 |
operation string |
| 65 |
expected string |
| 66 |
}{ |
| 67 |
{ |
| 68 |
name: "default handler formatter", |
| 69 |
formatter: func(operation string, _ *http.Request) string { |
| 70 |
return operation |
| 71 |
}, |
| 72 |
operation: "test_operation", |
| 73 |
expected: "test_operation", |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "default transport formatter", |
| 77 |
formatter: func(_ string, r *http.Request) string { |
| 78 |
return "HTTP " + r.Method |
| 79 |
}, |
| 80 |
expected: "HTTP GET", |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "custom formatter", |
| 84 |
formatter: func(_ string, r *http.Request) string { |
| 85 |
return r.URL.Path |
| 86 |
}, |
| 87 |
operation: "", |
| 88 |
expected: "/hello", |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
for _, tc := range testCases { |
| 93 |
t.Run(tc.name, func(t *testing.T) { |
| 94 |
rr := httptest.NewRecorder() |
| 95 |
|
| 96 |
spanRecorder := tracetest.NewSpanRecorder() |
| 97 |
provider := trace.NewTracerProvider(trace.WithSpanProcessor(spanRecorder)) |
| 98 |
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 99 |
if _, err := io.WriteString(w, "hello world"); err != nil { |
| 100 |
t.Fatal(err) |
| 101 |
} |
| 102 |
}) |
| 103 |
h := otelhttp.NewHandler( |
| 104 |
handler, |
| 105 |
tc.operation, |
| 106 |
otelhttp.WithTracerProvider(provider), |
| 107 |
otelhttp.WithSpanNameFormatter(tc.formatter), |
| 108 |
) |
| 109 |
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://localhost/hello", http.NoBody) |
| 110 |
if err != nil { |
| 111 |
t.Fatal(err) |
| 112 |
} |
| 113 |
h.ServeHTTP(rr, r) |
| 114 |
if got, expected := rr.Result().StatusCode, http.StatusOK; got != expected { |
| 115 |
t.Fatalf("got %d, expected %d", got, expected) |
| 116 |
} |
| 117 |
|
| 118 |
spans := spanRecorder.Ended() |
| 119 |
if assert.Len(t, spans, 1) { |
| 120 |
assert.Equal(t, tc.expected, spans[0].Name()) |
| 121 |
} |
| 122 |
}) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestEvent(t *testing.T) { |
| 127 |
t.Run("constant values", func(t *testing.T) { |
| 128 |
assert.Equal(t, otelhttp.ReadEvents, otelhttp.Event(1), "ReadEvents should be 1") |
| 129 |
assert.Equal(t, otelhttp.WriteEvents, otelhttp.Event(2), "WriteEvents should be 2") |
| 130 |
}) |
| 131 |
|
| 132 |
t.Run("unspecifiedEvent", func(t *testing.T) { |
| 133 |
var unspecified otelhttp.Event |
| 134 |
assert.Equal(t, otelhttp.Event(0), unspecified, "unspecifiedEvent should be zero-value Event") |
| 135 |
|
| 136 |
|
| 137 |
assert.NotEqual(t, otelhttp.ReadEvents, unspecified, "unspecifiedEvent should not equal ReadEvents") |
| 138 |
assert.NotEqual(t, otelhttp.WriteEvents, unspecified, "unspecifiedEvent should not equal WriteEvents") |
| 139 |
|
| 140 |
|
| 141 |
opt := otelhttp.WithMessageEvents(unspecified) |
| 142 |
assert.NotNil(t, opt, "WithMessageEvents(unspecifiedEvent) should not return nil") |
| 143 |
|
| 144 |
|
| 145 |
optMultiple := otelhttp.WithMessageEvents(unspecified, otelhttp.ReadEvents) |
| 146 |
assert.NotNil(t, optMultiple, "WithMessageEvents with unspecified and valid events should not return nil") |
| 147 |
}) |
| 148 |
|
| 149 |
t.Run("WithMessageEvents", func(t *testing.T) { |
| 150 |
tests := []struct { |
| 151 |
name string |
| 152 |
events []otelhttp.Event |
| 153 |
}{ |
| 154 |
{name: "ReadEvents", events: []otelhttp.Event{otelhttp.ReadEvents}}, |
| 155 |
{name: "WriteEvents", events: []otelhttp.Event{otelhttp.WriteEvents}}, |
| 156 |
{name: "multiple events", events: []otelhttp.Event{otelhttp.ReadEvents, otelhttp.WriteEvents}}, |
| 157 |
{name: "no events", events: []otelhttp.Event{}}, |
| 158 |
{name: "unspecified event only", events: []otelhttp.Event{otelhttp.Event(0)}}, |
| 159 |
{name: "mixed with unspecified", events: []otelhttp.Event{otelhttp.Event(0), otelhttp.ReadEvents}}, |
| 160 |
} |
| 161 |
|
| 162 |
for _, tt := range tests { |
| 163 |
t.Run(tt.name, func(t *testing.T) { |
| 164 |
got := otelhttp.WithMessageEvents(tt.events...) |
| 165 |
assert.NotNil(t, got, "WithMessageEvents(%v) should not return nil", tt.events) |
| 166 |
}) |
| 167 |
} |
| 168 |
}) |
| 169 |
} |