-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin_otel_test.go
More file actions
75 lines (59 loc) · 2.23 KB
/
Copy pathplugin_otel_test.go
File metadata and controls
75 lines (59 loc) · 2.23 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
package gzip
import (
"context"
"net/http"
"net/http/httptest"
"testing"
rrcontext "github.com/roadrunner-server/context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
)
func TestMiddlewareSpanEndBeforeNext(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
t.Cleanup(func() { _ = tp.Shutdown(t.Context()) })
p := &Plugin{}
require.NoError(t, p.Init())
// "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")
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
// Flush and collect spans
require.NoError(t, tp.ForceFlush(t.Context()))
spans := exporter.GetSpans()
var gzipSpan, nextSpan tracetest.SpanStub
for _, s := range spans {
switch s.Name {
case PluginName:
gzipSpan = s
case "nextHandler":
nextSpan = s
}
}
require.NotEmpty(t, gzipSpan.Name, "gzip middleware span was not found in exported spans")
require.NotEmpty(t, nextSpan.Name, "next handler span was not found in exported spans")
// The gzip span must end before the next handler starts,
// proving the span covers only the middleware's own work.
assert.False(t, gzipSpan.EndTime.After(nextSpan.StartTime),
"gzip span EndTime (%v) should not be after next-handler StartTime (%v)",
gzipSpan.EndTime, nextSpan.StartTime,
)
// The gzip span must use SpanKindInternal (middleware, not entry-point)
assert.Equal(t, trace.SpanKindInternal, gzipSpan.SpanKind,
"gzip span should be SpanKindInternal",
)
}