-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin_otel_test.go
More file actions
139 lines (109 loc) · 4.43 KB
/
plugin_otel_test.go
File metadata and controls
139 lines (109 loc) · 4.43 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
package static
import (
"context"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
rrcontext "github.com/roadrunner-server/context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
func TestMiddlewareSpanEndsBeforeNextHandler(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
t.Cleanup(func() { _ = tp.Shutdown(t.Context()) })
dir := t.TempDir()
p := &Plugin{
cfg: &Config{Dir: dir},
log: slog.New(slog.DiscardHandler),
root: http.Dir(dir),
allowedExtensions: make(map[string]struct{}),
forbiddenExtensions: make(map[string]struct{}),
prop: propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}),
}
// "next" handler that creates its own span to mark when downstream starts
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, span := tp.Tracer("test").Start(r.Context(), "nextHandler")
defer span.End()
w.WriteHeader(http.StatusOK)
})
handler := p.Middleware(next)
// Create a parent span so the middleware finds a TracerProvider in context
ctx, parentSpan := tp.Tracer("test").Start(t.Context(), "parent")
defer parentSpan.End()
// Set OtelTracerNameKey so the middleware activates its OTEL branch
ctx = context.WithValue(ctx, rrcontext.OtelTracerNameKey, "test-tracer")
// Request without file extension — delegates to next handler
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/noext", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
// Flush and collect spans
require.NoError(t, tp.ForceFlush(t.Context()))
spans := exporter.GetSpans()
var staticSpan, nextSpan tracetest.SpanStub
for _, s := range spans {
switch s.Name {
case PluginName:
staticSpan = s
case "nextHandler":
nextSpan = s
}
}
require.NotEmpty(t, staticSpan.Name, "static middleware span was not found in exported spans")
require.NotEmpty(t, nextSpan.Name, "next handler span was not found in exported spans")
require.NotZero(t, staticSpan.EndTime, "static span should have ended")
require.NotZero(t, nextSpan.StartTime, "next handler span should have started")
assert.True(t,
!staticSpan.EndTime.After(nextSpan.StartTime),
"static span must end before (or at) the next handler span starts: static.End=%v, next.Start=%v",
staticSpan.EndTime, nextSpan.StartTime,
)
}
func TestMiddlewareSpanEndsAfterServingFile(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
t.Cleanup(func() { _ = tp.Shutdown(t.Context()) })
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "hello.txt"), []byte("hello world"), 0o600))
p := &Plugin{
cfg: &Config{Dir: dir},
log: slog.New(slog.DiscardHandler),
root: http.Dir(dir),
allowedExtensions: make(map[string]struct{}),
forbiddenExtensions: make(map[string]struct{}),
prop: propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}),
}
nextCalled := false
next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
nextCalled = true
w.WriteHeader(http.StatusOK)
})
handler := p.Middleware(next)
// Create a parent span so the middleware finds a TracerProvider in context
ctx, parentSpan := tp.Tracer("test").Start(t.Context(), "parent")
defer parentSpan.End()
ctx = context.WithValue(ctx, rrcontext.OtelTracerNameKey, "test-tracer")
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/hello.txt", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
assert.False(t, nextCalled, "next handler should not have been called for a served static file")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Contains(t, rec.Body.String(), "hello world")
// Flush and collect spans
require.NoError(t, tp.ForceFlush(t.Context()))
spans := exporter.GetSpans()
var staticSpan tracetest.SpanStub
for _, s := range spans {
if s.Name == PluginName {
staticSpan = s
}
}
require.NotEmpty(t, staticSpan.Name, "static middleware span was not found in exported spans")
require.NotZero(t, staticSpan.EndTime, "static span should have ended after serving the file")
}