| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package zpages |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 16 |
) |
| 17 |
|
| 18 |
func TestNewTracezHandler(t *testing.T) { |
| 19 |
sp := NewSpanProcessor() |
| 20 |
handler := NewTracezHandler(sp) |
| 21 |
require.NotNil(t, handler, "NewTracezHandler returned nil") |
| 22 |
} |
| 23 |
|
| 24 |
func TestTracezHandler_ServeHTTP(t *testing.T) { |
| 25 |
|
| 26 |
sp := NewSpanProcessor() |
| 27 |
defer func() { |
| 28 |
require.NoError(t, sp.Shutdown(t.Context())) |
| 29 |
}() |
| 30 |
|
| 31 |
tp := sdktrace.NewTracerProvider( |
| 32 |
sdktrace.WithSpanProcessor(sp), |
| 33 |
) |
| 34 |
defer func() { |
| 35 |
require.NoError(t, tp.Shutdown(t.Context())) |
| 36 |
}() |
| 37 |
|
| 38 |
tracer := tp.Tracer("test-tracer") |
| 39 |
ctx := t.Context() |
| 40 |
|
| 41 |
_, completedSpan := tracer.Start(ctx, "completed-span") |
| 42 |
completedSpan.End() |
| 43 |
|
| 44 |
_, errorSpan := tracer.Start(ctx, "error-span") |
| 45 |
errorSpan.RecordError(context.DeadlineExceeded) |
| 46 |
errorSpan.End() |
| 47 |
|
| 48 |
_, querySpan := tracer.Start(ctx, "query-span") |
| 49 |
querySpan.End() |
| 50 |
|
| 51 |
for range 5 { |
| 52 |
_, span := tracer.Start(ctx, "multi-span") |
| 53 |
span.End() |
| 54 |
} |
| 55 |
|
| 56 |
handler := NewTracezHandler(sp) |
| 57 |
|
| 58 |
tests := []struct { |
| 59 |
name string |
| 60 |
url string |
| 61 |
wantStatus int |
| 62 |
wantContains []string |
| 63 |
wantNotContain []string |
| 64 |
}{ |
| 65 |
{ |
| 66 |
name: "basic response with no query params", |
| 67 |
url: "/tracez", |
| 68 |
wantStatus: http.StatusOK, |
| 69 |
wantContains: []string{"completed-span", "error-span", "query-span", "multi-span"}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: "query for existing span", |
| 73 |
url: "/tracez?zspanname=query-span", |
| 74 |
wantStatus: http.StatusOK, |
| 75 |
wantContains: []string{"query-span"}, |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "query for non-existing span", |
| 79 |
url: "/tracez?zspanname=non-existing", |
| 80 |
wantStatus: http.StatusOK, |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
wantContains: []string{"non-existing"}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "latency spans (type=1)", |
| 89 |
url: "/tracez?zspanname=completed-span&ztype=1", |
| 90 |
wantStatus: http.StatusOK, |
| 91 |
wantContains: []string{}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "error spans (type=2)", |
| 95 |
url: "/tracez?zspanname=error-span&ztype=2", |
| 96 |
wantStatus: http.StatusOK, |
| 97 |
wantContains: []string{}, |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "with latency bucket", |
| 101 |
url: "/tracez?zspanname=completed-span&ztype=1&zlatencybucket=0", |
| 102 |
wantStatus: http.StatusOK, |
| 103 |
}, |
| 104 |
{ |
| 105 |
name: "all parameters", |
| 106 |
url: "/tracez?zspanname=completed-span&ztype=1&zlatencybucket=2", |
| 107 |
wantStatus: http.StatusOK, |
| 108 |
wantContains: []string{}, |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "type only", |
| 112 |
url: "/tracez?ztype=1", |
| 113 |
wantStatus: http.StatusOK, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "latency bucket only", |
| 117 |
url: "/tracez?zlatencybucket=3", |
| 118 |
wantStatus: http.StatusOK, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "invalid type value", |
| 122 |
url: "/tracez?ztype=invalid", |
| 123 |
wantStatus: http.StatusOK, |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "negative latency bucket", |
| 127 |
url: "/tracez?zlatencybucket=-1", |
| 128 |
wantStatus: http.StatusOK, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "multiple spans with same name", |
| 132 |
url: "/tracez?zspanname=multi-span&ztype=1", |
| 133 |
wantStatus: http.StatusOK, |
| 134 |
wantContains: []string{"multi-span"}, |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "invalid form encoding", |
| 138 |
url: "/tracez?%zzz", |
| 139 |
wantStatus: http.StatusBadRequest, |
| 140 |
}, |
| 141 |
} |
| 142 |
|
| 143 |
for _, tt := range tests { |
| 144 |
t.Run(tt.name, func(t *testing.T) { |
| 145 |
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, tt.url, http.NoBody) |
| 146 |
w := httptest.NewRecorder() |
| 147 |
|
| 148 |
handler.ServeHTTP(w, req) |
| 149 |
|
| 150 |
resp := w.Result() |
| 151 |
defer resp.Body.Close() |
| 152 |
|
| 153 |
assert.Equal(t, tt.wantStatus, resp.StatusCode) |
| 154 |
|
| 155 |
|
| 156 |
if resp.StatusCode == http.StatusOK && tt.url == "/tracez" { |
| 157 |
assert.Equal(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type")) |
| 158 |
} |
| 159 |
|
| 160 |
body := w.Body.String() |
| 161 |
|
| 162 |
|
| 163 |
if resp.StatusCode == http.StatusOK { |
| 164 |
assert.NotEmpty(t, body, "expected non-empty response body") |
| 165 |
|
| 166 |
for _, want := range tt.wantContains { |
| 167 |
assert.Contains(t, body, want) |
| 168 |
} |
| 169 |
for _, notWant := range tt.wantNotContain { |
| 170 |
assert.NotContains(t, body, notWant) |
| 171 |
} |
| 172 |
} |
| 173 |
}) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
func TestTracezHandler_ConcurrentSafe(t *testing.T) { |
| 178 |
sp := NewSpanProcessor() |
| 179 |
defer func() { |
| 180 |
require.NoError(t, sp.Shutdown(t.Context())) |
| 181 |
}() |
| 182 |
|
| 183 |
tp := sdktrace.NewTracerProvider( |
| 184 |
sdktrace.WithSpanProcessor(sp), |
| 185 |
) |
| 186 |
defer func() { |
| 187 |
require.NoError(t, tp.Shutdown(t.Context())) |
| 188 |
}() |
| 189 |
|
| 190 |
tracer := tp.Tracer("test-tracer") |
| 191 |
ctx := t.Context() |
| 192 |
|
| 193 |
for range 10 { |
| 194 |
_, span := tracer.Start(ctx, "concurrent-span") |
| 195 |
span.End() |
| 196 |
} |
| 197 |
|
| 198 |
handler := NewTracezHandler(sp) |
| 199 |
|
| 200 |
var wg sync.WaitGroup |
| 201 |
for range 5 { |
| 202 |
wg.Go(func() { |
| 203 |
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/tracez", http.NoBody) |
| 204 |
w := httptest.NewRecorder() |
| 205 |
|
| 206 |
handler.ServeHTTP(w, req) |
| 207 |
|
| 208 |
resp := w.Result() |
| 209 |
_ = resp.Body.Close() |
| 210 |
|
| 211 |
assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 212 |
}) |
| 213 |
} |
| 214 |
|
| 215 |
wg.Wait() |
| 216 |
} |