Repository navigation
Expand file tree
/
Copy pathotel_behavior_test.go
More file actions
469 lines (425 loc) · 15.2 KB
/
Copy pathotel_behavior_test.go
File metadata and controls
469 lines (425 loc) · 15.2 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2026 LabStack and Echo contributors
package echootel
import (
"bytes"
"errors"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
metricnoop "go.opentelemetry.io/otel/metric/noop"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
tracenoop "go.opentelemetry.io/otel/trace/noop"
)
func newTestProviders() (*tracetest.InMemoryExporter, *sdktrace.TracerProvider, *sdkmetric.ManualReader, *sdkmetric.MeterProvider) {
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
reader := sdkmetric.NewManualReader()
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
return exporter, tp, reader, mp
}
// durationPoints returns the attribute sets of the http.server.request.duration data points.
func durationPoints(t *testing.T, reader *sdkmetric.ManualReader) []attribute.Set {
t.Helper()
rm := metricdata.ResourceMetrics{}
assert.NoError(t, reader.Collect(t.Context(), &rm))
var sets []attribute.Set
for _, sm := range rm.ScopeMetrics {
for _, m := range sm.Metrics {
if m.Name != "http.server.request.duration" {
continue
}
for _, dp := range m.Data.(metricdata.Histogram[float64]).DataPoints {
sets = append(sets, dp.Attributes)
}
}
}
return sets
}
func metricHas(set attribute.Set, key attribute.Key) bool {
_, ok := set.Value(key)
return ok
}
func TestPanicIsRecordedAndRepanicked(t *testing.T) {
exporter, tp, reader, mp := newTestProviders()
e := echo.New()
e.Use(middleware.Recover()) // usual order: Recover wraps the otel middleware
e.Use(NewMiddlewareWithConfig(Config{ServerName: "foobar", TracerProvider: tp, MeterProvider: mp}))
e.GET("/panic", func(c *echo.Context) error {
panic("boom")
})
w := httptest.NewRecorder()
e.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/panic", http.NoBody))
assert.Equal(t, http.StatusInternalServerError, w.Result().StatusCode, "Recover must still handle the panic")
spans := exporter.GetSpans()
if !assert.Len(t, spans, 1) {
return
}
assert.Equal(t, codes.Error, spans[0].Status.Code)
assert.Equal(t, "panic: boom", spans[0].Status.Description)
assert.Contains(t, spans[0].Attributes, attribute.Int("http.response.status_code", http.StatusInternalServerError))
assert.Contains(t, spans[0].Attributes, attribute.String("error.type", "panic"))
points := durationPoints(t, reader)
if assert.Len(t, points, 1) {
v, ok := points[0].Value("error.type")
assert.True(t, ok)
assert.Equal(t, "panic", v.AsString())
}
}
func TestUnknownRequestBodySizeIsNotRecorded(t *testing.T) {
exporter, tp, reader, mp := newTestProviders()
e := echo.New()
e.Use(NewMiddlewareWithConfig(Config{ServerName: "foobar", TracerProvider: tp, MeterProvider: mp}))
e.POST("/upload", func(c *echo.Context) error {
_, _ = io.Copy(io.Discard, c.Request().Body)
return c.NoContent(http.StatusNoContent)
})
r := httptest.NewRequest(http.MethodPost, "/upload", bytes.NewReader([]byte("chunked body")))
r.ContentLength = -1 // unknown size, as for a chunked request
e.ServeHTTP(httptest.NewRecorder(), r)
spans := exporter.GetSpans()
assert.Len(t, spans, 1)
for _, a := range spans[0].Attributes {
assert.NotEqual(t, attribute.Key("http.request.body.size"), a.Key)
}
rm := metricdata.ResourceMetrics{}
assert.NoError(t, reader.Collect(t.Context(), &rm))
for _, m := range rm.ScopeMetrics[0].Metrics {
if m.Name == "http.server.request.body.size" {
assert.Empty(t, m.Data.(metricdata.Histogram[int64]).DataPoints, "unknown size must not be recorded")
}
}
}
func TestMetricsHaveNoClientChosenAttributes(t *testing.T) {
exporter, tp, reader, mp := newTestProviders()
e := echo.New()
e.Use(NewMiddlewareWithConfig(Config{ServerName: "api.example.com", TracerProvider: tp, MeterProvider: mp}))
e.Any("/x", func(c *echo.Context) error {
return c.NoContent(http.StatusOK)
})
r := httptest.NewRequest("FOO", "/x", http.NoBody)
r.Host = "evil.example.com:1234"
e.ServeHTTP(httptest.NewRecorder(), r)
spans := exporter.GetSpans()
assert.Len(t, spans, 1)
assert.Contains(t, spans[0].Attributes, attribute.String("server.address", "api.example.com"))
assert.Contains(t, spans[0].Attributes, attribute.String("http.request.method_original", "FOO"))
for _, a := range spans[0].Attributes {
assert.NotEqual(t, attribute.Key("server.port"), a.Key, "port must not come from the Host header when ServerName is set")
}
points := durationPoints(t, reader)
if assert.Len(t, points, 1) {
assert.False(t, metricHas(points[0], "server.address"))
assert.False(t, metricHas(points[0], "server.port"))
assert.False(t, metricHas(points[0], "http.request.method_original"))
m, _ := points[0].Value("http.request.method")
assert.Equal(t, "_OTHER", m.AsString())
}
}
func TestHTTPRouteIsEchoRouteBehindServeMux(t *testing.T) {
testCases := []struct {
name string
pre bool
whenTarget string
expectName string
expectPath string
}{
{name: "Pre middleware, matched route", pre: true, whenTarget: "/api/users/1", expectName: "GET /api/users/:id", expectPath: "/api/users/:id"},
{name: "Use middleware, matched route", pre: false, whenTarget: "/api/users/1", expectName: "GET /api/users/:id", expectPath: "/api/users/:id"},
{name: "Pre middleware, not found", pre: true, whenTarget: "/api/nothing", expectName: "GET"},
{name: "Use middleware, not found", pre: false, whenTarget: "/api/nothing", expectName: "GET"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
exporter, tp, _, mp := newTestProviders()
e := echo.New()
mw := NewMiddlewareWithConfig(Config{ServerName: "foobar", TracerProvider: tp, MeterProvider: mp})
if tc.pre {
e.Pre(mw)
} else {
e.Use(mw)
}
e.GET("/api/users/:id", func(c *echo.Context) error {
return c.NoContent(http.StatusOK)
})
mux := http.NewServeMux()
mux.Handle("GET /api/", e)
mux.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, tc.whenTarget, http.NoBody))
spans := exporter.GetSpans()
assert.Len(t, spans, 1)
assert.Equal(t, tc.expectName, spans[0].Name)
route := ""
for _, a := range spans[0].Attributes {
if a.Key == "http.route" {
route = a.Value.AsString()
}
}
assert.Equal(t, tc.expectPath, route)
})
}
}
func TestOnExtractionError(t *testing.T) {
var got error
e := echo.New()
e.Use(NewMiddlewareWithConfig(Config{OnExtractionError: func(c *echo.Context, err error) { got = err }}))
e.GET("/x", func(c *echo.Context) error { return c.NoContent(http.StatusOK) })
r := httptest.NewRequest(http.MethodGet, "/x", http.NoBody)
r.Host = "bad:host:name:1"
e.ServeHTTP(httptest.NewRecorder(), r)
assert.Error(t, got)
}
func TestSpanStartOptionsAndTracerKey(t *testing.T) {
exporter, tp, _, _ := newTestProviders()
e := echo.New()
e.Use(NewMiddlewareWithConfig(Config{
ServerName: "foobar",
TracerProvider: tp,
SpanStartOptions: []trace.SpanStartOption{trace.WithAttributes(attribute.String("custom", "yes"))},
}))
e.GET("/x", func(c *echo.Context) error {
tracer, err := echo.ContextGet[trace.Tracer](c, TracerKey)
if assert.NoError(t, err) {
_, child := tracer.Start(c.Request().Context(), "child")
child.End()
}
return c.NoContent(http.StatusOK)
})
e.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/x", http.NoBody))
spans := exporter.GetSpans()
assert.Len(t, spans, 2)
server := spans[1]
assert.Contains(t, server.Attributes, attribute.String("custom", "yes"))
assert.Equal(t, server.SpanContext.TraceID(), spans[0].SpanContext.TraceID(), "child span must be in the same trace")
}
func TestSpanEndAttributesCallbackMustAppend(t *testing.T) {
testCases := []struct {
name string
callback AttributesFunc
expectErrType bool
}{
{
name: "append to attr keeps error.type",
callback: func(c *echo.Context, v *Values, attr []attribute.KeyValue) []attribute.KeyValue {
return append(attr, attribute.String("extra", "1"))
},
expectErrType: true,
},
{
name: "new slice drops error.type",
callback: func(c *echo.Context, v *Values, attr []attribute.KeyValue) []attribute.KeyValue {
return []attribute.KeyValue{attribute.String("extra", "1")}
},
expectErrType: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
exporter, tp, _, _ := newTestProviders()
e := echo.New()
e.Use(NewMiddlewareWithConfig(Config{ServerName: "foobar", TracerProvider: tp, SpanEndAttributes: tc.callback}))
e.GET("/x", func(c *echo.Context) error { return c.NoContent(http.StatusServiceUnavailable) })
e.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/x", http.NoBody))
spans := exporter.GetSpans()
assert.Len(t, spans, 1)
assert.Contains(t, spans[0].Attributes, attribute.String("extra", "1"))
has := false
for _, a := range spans[0].Attributes {
if a.Key == "error.type" {
has = true
}
}
assert.Equal(t, tc.expectErrType, has)
})
}
}
func TestMultipartFormTempFilesAreRemoved(t *testing.T) {
var tmpFile string
e := echo.New()
e.Use(NewMiddleware("foobar"))
e.POST("/upload", func(c *echo.Context) error {
if err := c.Request().ParseMultipartForm(1); err != nil { // tiny memory limit forces a temp file
return err
}
f, err := c.Request().MultipartForm.File["file"][0].Open()
if err != nil {
return err
}
defer f.Close()
osFile, ok := f.(*os.File)
if assert.True(t, ok, "upload must be stored in a temp file") {
tmpFile = osFile.Name()
}
return c.NoContent(http.StatusOK)
})
body := &bytes.Buffer{}
mw := multipart.NewWriter(body)
fw, _ := mw.CreateFormFile("file", "big.txt")
_, _ = fw.Write(bytes.Repeat([]byte("x"), 64*1024))
_ = mw.Close()
r := httptest.NewRequest(http.MethodPost, "/upload", body)
r.Header.Set("Content-Type", mw.FormDataContentType())
w := httptest.NewRecorder()
e.ServeHTTP(w, r)
assert.Equal(t, http.StatusOK, w.Result().StatusCode)
if assert.NotEmpty(t, tmpFile) {
_, err := os.Stat(tmpFile)
assert.True(t, os.IsNotExist(err), "temp file must be removed after the request")
}
}
func TestPanicPaths(t *testing.T) {
testCases := []struct {
name string
recover bool
config func(cfg *Config)
handler echo.HandlerFunc
expectRepanic any
expectStatus int // 0 = no http.response.status_code attribute
expectErrorType string
}{
{
name: "panic after 4xx response was sent is still an error",
recover: true,
handler: func(c *echo.Context) error { _ = c.NoContent(http.StatusNotFound); panic("late") },
expectStatus: http.StatusNotFound,
expectErrorType: "panic",
},
{
name: "panic with a status error records its status",
recover: true,
handler: func(c *echo.Context) error { panic(echo.ErrUnauthorized) },
expectStatus: http.StatusUnauthorized,
expectErrorType: "panic",
},
{
name: "http.ErrAbortHandler without response records no status",
recover: false,
handler: func(c *echo.Context) error { panic(http.ErrAbortHandler) },
expectRepanic: http.ErrAbortHandler,
expectStatus: 0,
expectErrorType: "panic",
},
{
name: "panic in OnNextError is recorded",
recover: true,
config: func(cfg *Config) {
cfg.OnNextError = func(c *echo.Context, err error) { panic("hook") }
},
handler: func(c *echo.Context) error { return errors.New("x") },
expectStatus: http.StatusInternalServerError,
expectErrorType: "panic",
},
{
name: "panic in a recording callback keeps the original panic value",
recover: false,
config: func(cfg *Config) {
cfg.SpanEndAttributes = func(c *echo.Context, v *Values, attr []attribute.KeyValue) []attribute.KeyValue {
panic("callback")
}
},
handler: func(c *echo.Context) error { panic("original") },
expectRepanic: "original",
expectStatus: -1, // not checked, recording was interrupted
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
exporter, tp, _, mp := newTestProviders()
cfg := Config{ServerName: "foobar", TracerProvider: tp, MeterProvider: mp}
if tc.config != nil {
tc.config(&cfg)
}
e := echo.New()
if tc.recover {
e.Use(middleware.Recover())
}
e.Use(NewMiddlewareWithConfig(cfg))
e.GET("/x", tc.handler)
var repanic any
func() {
defer func() { repanic = recover() }()
e.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/x", http.NoBody))
}()
if tc.expectRepanic != nil {
assert.Equal(t, tc.expectRepanic, repanic)
}
if tc.expectStatus < 0 {
return
}
spans := exporter.GetSpans()
if !assert.Len(t, spans, 1) {
return
}
assert.Equal(t, codes.Error, spans[0].Status.Code)
var gotStatus int64
var gotErrType string
for _, a := range spans[0].Attributes {
switch a.Key {
case "http.response.status_code":
gotStatus = a.Value.AsInt64()
case "error.type":
gotErrType = a.Value.AsString()
}
}
assert.Equal(t, int64(tc.expectStatus), gotStatus)
assert.Equal(t, tc.expectErrorType, gotErrType)
})
}
}
func TestRecoverAddedAfterMiddlewareReportsPanic(t *testing.T) {
exporter, tp, _, mp := newTestProviders()
e := echo.New()
e.Use(NewMiddlewareWithConfig(Config{ServerName: "foobar", TracerProvider: tp, MeterProvider: mp}))
e.Use(middleware.Recover()) // Recover inside turns the panic into *middleware.PanicStackError
e.GET("/x", func(c *echo.Context) error { panic("boom") })
e.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/x", http.NoBody))
spans := exporter.GetSpans()
if assert.Len(t, spans, 1) {
assert.Equal(t, codes.Error, spans[0].Status.Code)
assert.Equal(t, "panic: boom", spans[0].Status.Description)
assert.Contains(t, spans[0].Attributes, attribute.String("error.type", "panic"))
}
}
func TestServerNameWithoutHostIsRejected(t *testing.T) {
_, err := Config{ServerName: ":8080"}.ToMiddleware()
assert.Error(t, err)
}
func BenchmarkMiddleware(b *testing.B) {
benchmarks := []struct {
name string
tp trace.TracerProvider
mp *sdkmetric.MeterProvider
}{
{name: "noop providers"},
{name: "sdk providers", tp: sdktrace.NewTracerProvider(), mp: sdkmetric.NewMeterProvider(sdkmetric.WithReader(sdkmetric.NewManualReader()))},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
cfg := Config{ServerName: "foobar", TracerProvider: tracenoop.NewTracerProvider(), MeterProvider: metricnoop.NewMeterProvider()}
if bm.tp != nil {
cfg.TracerProvider = bm.tp
cfg.MeterProvider = bm.mp
}
e := echo.New()
e.Use(NewMiddlewareWithConfig(cfg))
e.GET("/users/:id", func(c *echo.Context) error { return c.String(http.StatusOK, "ok") })
r := httptest.NewRequest(http.MethodGet, "/users/123?x=1", http.NoBody)
r.Header.Set("User-Agent", "bench")
b.ReportAllocs()
for b.Loop() {
e.ServeHTTP(httptest.NewRecorder(), r)
}
})
}
}