| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelrestful_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"strconv" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/emicklei/go-restful/v3" |
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
"go.opentelemetry.io/otel" |
| 16 |
"go.opentelemetry.io/otel/attribute" |
| 17 |
"go.opentelemetry.io/otel/codes" |
| 18 |
"go.opentelemetry.io/otel/propagation" |
| 19 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 20 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 21 |
oteltrace "go.opentelemetry.io/otel/trace" |
| 22 |
|
| 23 |
"go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful" |
| 24 |
) |
| 25 |
|
| 26 |
func TestChildSpanFromGlobalTracer(t *testing.T) { |
| 27 |
sr := tracetest.NewSpanRecorder() |
| 28 |
otel.SetTracerProvider(sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))) |
| 29 |
|
| 30 |
handlerFunc := func(_ *restful.Request, resp *restful.Response) { |
| 31 |
resp.WriteHeader(http.StatusOK) |
| 32 |
} |
| 33 |
ws := &restful.WebService{} |
| 34 |
ws.Route(ws.GET("/user/{id}").To(handlerFunc). |
| 35 |
Returns(http.StatusOK, "OK", nil). |
| 36 |
Returns(http.StatusNotFound, "Not Found", nil)) |
| 37 |
container := restful.NewContainer() |
| 38 |
container.Filter(otelrestful.OTelFilter("my-service")) |
| 39 |
container.Add(ws) |
| 40 |
|
| 41 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 42 |
w := httptest.NewRecorder() |
| 43 |
|
| 44 |
container.ServeHTTP(w, r) |
| 45 |
assert.Len(t, sr.Ended(), 1) |
| 46 |
} |
| 47 |
|
| 48 |
func TestChildSpanFromCustomTracer(t *testing.T) { |
| 49 |
sr := tracetest.NewSpanRecorder() |
| 50 |
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 51 |
|
| 52 |
handlerFunc := func(_ *restful.Request, resp *restful.Response) { |
| 53 |
resp.WriteHeader(http.StatusOK) |
| 54 |
} |
| 55 |
ws := &restful.WebService{} |
| 56 |
ws.Route(ws.GET("/user/{id}").To(handlerFunc)) |
| 57 |
|
| 58 |
container := restful.NewContainer() |
| 59 |
container.Filter(otelrestful.OTelFilter("my-service", otelrestful.WithTracerProvider(provider))) |
| 60 |
container.Add(ws) |
| 61 |
|
| 62 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 63 |
w := httptest.NewRecorder() |
| 64 |
|
| 65 |
container.ServeHTTP(w, r) |
| 66 |
assert.Len(t, sr.Ended(), 1) |
| 67 |
} |
| 68 |
|
| 69 |
func TestChildSpanNames(t *testing.T) { |
| 70 |
sr := tracetest.NewSpanRecorder() |
| 71 |
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 72 |
|
| 73 |
handlerFunc := func(_ *restful.Request, resp *restful.Response) { |
| 74 |
resp.WriteHeader(http.StatusOK) |
| 75 |
} |
| 76 |
ws := &restful.WebService{} |
| 77 |
ws.Route(ws.GET("/user/{id:[0-9]+}").To(handlerFunc)) |
| 78 |
|
| 79 |
container := restful.NewContainer() |
| 80 |
container.Filter(otelrestful.OTelFilter("foobar", otelrestful.WithTracerProvider(provider))) |
| 81 |
container.Add(ws) |
| 82 |
|
| 83 |
ws.Route(ws.GET("/book/{title}").To(func(_ *restful.Request, resp *restful.Response) { |
| 84 |
_, _ = resp.Write([]byte("ok")) |
| 85 |
})) |
| 86 |
|
| 87 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 88 |
w := httptest.NewRecorder() |
| 89 |
|
| 90 |
container.ServeHTTP(w, r) |
| 91 |
spans := sr.Ended() |
| 92 |
require.Len(t, spans, 1) |
| 93 |
assertSpan( |
| 94 |
t, |
| 95 |
spans[0], |
| 96 |
"/user/{id:[0-9]+}", |
| 97 |
attribute.String("server.address", "foobar"), |
| 98 |
attribute.Int("http.response.status_code", http.StatusOK), |
| 99 |
attribute.String("http.request.method", "GET"), |
| 100 |
attribute.String("http.route", "/user/{id:[0-9]+}"), |
| 101 |
) |
| 102 |
|
| 103 |
r = httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/book/foo", http.NoBody) |
| 104 |
w = httptest.NewRecorder() |
| 105 |
container.ServeHTTP(w, r) |
| 106 |
spans = sr.Ended() |
| 107 |
require.Len(t, spans, 2) |
| 108 |
assertSpan( |
| 109 |
t, |
| 110 |
spans[1], |
| 111 |
"/book/{title}", |
| 112 |
attribute.String("server.address", "foobar"), |
| 113 |
attribute.Int("http.response.status_code", http.StatusOK), |
| 114 |
attribute.String("http.request.method", "GET"), |
| 115 |
attribute.String("http.route", "/book/{title}"), |
| 116 |
) |
| 117 |
} |
| 118 |
|
| 119 |
func TestMultiFilters(t *testing.T) { |
| 120 |
sr := tracetest.NewSpanRecorder() |
| 121 |
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 122 |
|
| 123 |
retOK := func(_ *restful.Request, resp *restful.Response) { resp.WriteHeader(http.StatusOK) } |
| 124 |
ws1 := &restful.WebService{} |
| 125 |
ws1.Path("/user") |
| 126 |
ws1.Route(ws1.GET("/{id}"). |
| 127 |
Filter(otelrestful.OTelFilter("my-service", otelrestful.WithTracerProvider(provider))). |
| 128 |
To(retOK)) |
| 129 |
ws1.Route(ws1.GET("/{id}/books"). |
| 130 |
Filter(otelrestful.OTelFilter("book-service", otelrestful.WithTracerProvider(provider))). |
| 131 |
To(retOK)) |
| 132 |
|
| 133 |
ws2 := &restful.WebService{} |
| 134 |
ws2.Path("/library") |
| 135 |
ws2.Filter(otelrestful.OTelFilter("library-service", otelrestful.WithTracerProvider(provider))) |
| 136 |
ws2.Route(ws2.GET("/{name}").To(retOK)) |
| 137 |
|
| 138 |
container := restful.NewContainer() |
| 139 |
container.Add(ws1) |
| 140 |
container.Add(ws2) |
| 141 |
|
| 142 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 143 |
w := httptest.NewRecorder() |
| 144 |
container.ServeHTTP(w, r) |
| 145 |
spans := sr.Ended() |
| 146 |
require.Len(t, spans, 1) |
| 147 |
assertSpan(t, spans[0], "/user/{id}") |
| 148 |
|
| 149 |
r = httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123/books", http.NoBody) |
| 150 |
w = httptest.NewRecorder() |
| 151 |
container.ServeHTTP(w, r) |
| 152 |
spans = sr.Ended() |
| 153 |
require.Len(t, spans, 2) |
| 154 |
assertSpan(t, spans[1], "/user/{id}/books") |
| 155 |
|
| 156 |
r = httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/library/metropolitan", http.NoBody) |
| 157 |
w = httptest.NewRecorder() |
| 158 |
container.ServeHTTP(w, r) |
| 159 |
spans = sr.Ended() |
| 160 |
require.Len(t, spans, 3) |
| 161 |
assertSpan(t, spans[2], "/library/{name}") |
| 162 |
} |
| 163 |
|
| 164 |
func TestSpanStatus(t *testing.T) { |
| 165 |
testCases := []struct { |
| 166 |
httpStatusCode int |
| 167 |
wantSpanStatus codes.Code |
| 168 |
}{ |
| 169 |
{http.StatusOK, codes.Unset}, |
| 170 |
{http.StatusBadRequest, codes.Unset}, |
| 171 |
{http.StatusInternalServerError, codes.Error}, |
| 172 |
} |
| 173 |
for _, tc := range testCases { |
| 174 |
t.Run(strconv.Itoa(tc.httpStatusCode), func(t *testing.T) { |
| 175 |
sr := tracetest.NewSpanRecorder() |
| 176 |
provider := sdktrace.NewTracerProvider() |
| 177 |
provider.RegisterSpanProcessor(sr) |
| 178 |
handlerFunc := func(_ *restful.Request, resp *restful.Response) { |
| 179 |
resp.WriteHeader(tc.httpStatusCode) |
| 180 |
} |
| 181 |
ws := &restful.WebService{} |
| 182 |
ws.Route(ws.GET("/").To(handlerFunc)) |
| 183 |
container := restful.NewContainer() |
| 184 |
container.Filter(otelrestful.OTelFilter("my-service", otelrestful.WithTracerProvider(provider))) |
| 185 |
container.Add(ws) |
| 186 |
|
| 187 |
container.ServeHTTP(httptest.NewRecorder(), httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", http.NoBody)) |
| 188 |
|
| 189 |
require.Len(t, sr.Ended(), 1, "should emit a span") |
| 190 |
assert.Equal(t, tc.wantSpanStatus, sr.Ended()[0].Status().Code, "should only set Error status for HTTP statuses >= 500") |
| 191 |
}) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func TestWithPublicEndpoint(t *testing.T) { |
| 196 |
spanRecorder := tracetest.NewSpanRecorder() |
| 197 |
provider := sdktrace.NewTracerProvider( |
| 198 |
sdktrace.WithSpanProcessor(spanRecorder), |
| 199 |
) |
| 200 |
remoteSpan := oteltrace.SpanContextConfig{ |
| 201 |
TraceID: oteltrace.TraceID{0x01}, |
| 202 |
SpanID: oteltrace.SpanID{0x01}, |
| 203 |
Remote: true, |
| 204 |
} |
| 205 |
prop := propagation.TraceContext{} |
| 206 |
|
| 207 |
handlerFunc := func(req *restful.Request, _ *restful.Response) { |
| 208 |
s := oteltrace.SpanFromContext(req.Request.Context()) |
| 209 |
sc := s.SpanContext() |
| 210 |
|
| 211 |
|
| 212 |
assert.True(t, sc.IsValid()) |
| 213 |
assert.False(t, sc.IsRemote()) |
| 214 |
assert.NotEqual(t, remoteSpan.TraceID, sc.TraceID()) |
| 215 |
} |
| 216 |
|
| 217 |
ws := &restful.WebService{} |
| 218 |
ws.Route(ws.GET("/user/{id}").To(handlerFunc)) |
| 219 |
|
| 220 |
container := restful.NewContainer() |
| 221 |
container.Filter(otelrestful.OTelFilter("test_handler", |
| 222 |
otelrestful.WithPublicEndpoint(), |
| 223 |
otelrestful.WithPropagators(prop), |
| 224 |
otelrestful.WithTracerProvider(provider)), |
| 225 |
) |
| 226 |
container.Add(ws) |
| 227 |
|
| 228 |
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://localhost/user/123", http.NoBody) |
| 229 |
require.NoError(t, err) |
| 230 |
|
| 231 |
sc := oteltrace.NewSpanContext(remoteSpan) |
| 232 |
ctx := oteltrace.ContextWithSpanContext(t.Context(), sc) |
| 233 |
prop.Inject(ctx, propagation.HeaderCarrier(r.Header)) |
| 234 |
|
| 235 |
rr := httptest.NewRecorder() |
| 236 |
container.ServeHTTP(rr, r) |
| 237 |
assert.Equal(t, 200, rr.Result().StatusCode) |
| 238 |
|
| 239 |
|
| 240 |
assert.NoError(t, spanRecorder.ForceFlush(ctx)) |
| 241 |
done := spanRecorder.Ended() |
| 242 |
require.Len(t, done, 1) |
| 243 |
require.Len(t, done[0].Links(), 1, "should contain link") |
| 244 |
require.True(t, sc.Equal(done[0].Links()[0].SpanContext), "should link incoming span context") |
| 245 |
} |
| 246 |
|
| 247 |
func TestWithPublicEndpointFn(t *testing.T) { |
| 248 |
remoteSpan := oteltrace.SpanContextConfig{ |
| 249 |
TraceID: oteltrace.TraceID{0x01}, |
| 250 |
SpanID: oteltrace.SpanID{0x01}, |
| 251 |
TraceFlags: oteltrace.FlagsSampled, |
| 252 |
Remote: true, |
| 253 |
} |
| 254 |
prop := propagation.TraceContext{} |
| 255 |
|
| 256 |
for _, tt := range []struct { |
| 257 |
name string |
| 258 |
fn func(*http.Request) bool |
| 259 |
handlerAssert func(*testing.T, oteltrace.SpanContext) |
| 260 |
spansAssert func(*testing.T, oteltrace.SpanContext, []sdktrace.ReadOnlySpan) |
| 261 |
}{ |
| 262 |
{ |
| 263 |
name: "with the method returning true", |
| 264 |
fn: func(*http.Request) bool { |
| 265 |
return true |
| 266 |
}, |
| 267 |
handlerAssert: func(t *testing.T, sc oteltrace.SpanContext) { |
| 268 |
|
| 269 |
assert.True(t, sc.IsValid()) |
| 270 |
assert.False(t, sc.IsRemote()) |
| 271 |
assert.NotEqual(t, remoteSpan.TraceID, sc.TraceID()) |
| 272 |
}, |
| 273 |
spansAssert: func(t *testing.T, sc oteltrace.SpanContext, spans []sdktrace.ReadOnlySpan) { |
| 274 |
require.Len(t, spans, 1) |
| 275 |
require.Len(t, spans[0].Links(), 1, "should contain link") |
| 276 |
require.True(t, sc.Equal(spans[0].Links()[0].SpanContext), "should link incoming span context") |
| 277 |
}, |
| 278 |
}, |
| 279 |
{ |
| 280 |
name: "with the method returning false", |
| 281 |
fn: func(*http.Request) bool { |
| 282 |
return false |
| 283 |
}, |
| 284 |
handlerAssert: func(t *testing.T, sc oteltrace.SpanContext) { |
| 285 |
|
| 286 |
assert.True(t, sc.IsValid()) |
| 287 |
assert.False(t, sc.IsRemote()) |
| 288 |
assert.Equal(t, remoteSpan.TraceID, sc.TraceID()) |
| 289 |
}, |
| 290 |
spansAssert: func(t *testing.T, _ oteltrace.SpanContext, spans []sdktrace.ReadOnlySpan) { |
| 291 |
require.Len(t, spans, 1) |
| 292 |
require.Empty(t, spans[0].Links(), "should not contain link") |
| 293 |
}, |
| 294 |
}, |
| 295 |
} { |
| 296 |
t.Run(tt.name, func(t *testing.T) { |
| 297 |
spanRecorder := tracetest.NewSpanRecorder() |
| 298 |
provider := sdktrace.NewTracerProvider( |
| 299 |
sdktrace.WithSpanProcessor(spanRecorder), |
| 300 |
) |
| 301 |
|
| 302 |
handlerFunc := func(req *restful.Request, _ *restful.Response) { |
| 303 |
s := oteltrace.SpanFromContext(req.Request.Context()) |
| 304 |
tt.handlerAssert(t, s.SpanContext()) |
| 305 |
} |
| 306 |
|
| 307 |
ws := &restful.WebService{} |
| 308 |
ws.Route(ws.GET("/user/{id}").To(handlerFunc)) |
| 309 |
|
| 310 |
container := restful.NewContainer() |
| 311 |
container.Filter(otelrestful.OTelFilter("test_handler", |
| 312 |
otelrestful.WithPublicEndpointFn(tt.fn), |
| 313 |
otelrestful.WithPropagators(prop), |
| 314 |
otelrestful.WithTracerProvider(provider)), |
| 315 |
) |
| 316 |
container.Add(ws) |
| 317 |
|
| 318 |
r, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://localhost/user/123", http.NoBody) |
| 319 |
require.NoError(t, err) |
| 320 |
|
| 321 |
sc := oteltrace.NewSpanContext(remoteSpan) |
| 322 |
ctx := oteltrace.ContextWithSpanContext(t.Context(), sc) |
| 323 |
prop.Inject(ctx, propagation.HeaderCarrier(r.Header)) |
| 324 |
|
| 325 |
rr := httptest.NewRecorder() |
| 326 |
container.ServeHTTP(rr, r) |
| 327 |
assert.Equal(t, http.StatusOK, rr.Result().StatusCode) |
| 328 |
|
| 329 |
|
| 330 |
assert.NoError(t, spanRecorder.ForceFlush(ctx)) |
| 331 |
spans := spanRecorder.Ended() |
| 332 |
tt.spansAssert(t, sc, spans) |
| 333 |
}) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func assertSpan(t *testing.T, span sdktrace.ReadOnlySpan, name string, attrs ...attribute.KeyValue) { |
| 338 |
t.Helper() |
| 339 |
|
| 340 |
assert.Equal(t, name, span.Name()) |
| 341 |
assert.Equal(t, oteltrace.SpanKindServer, span.SpanKind()) |
| 342 |
|
| 343 |
gotA := span.Attributes() |
| 344 |
for _, a := range attrs { |
| 345 |
assert.Contains(t, gotA, a) |
| 346 |
} |
| 347 |
} |