| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelrestful_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/emicklei/go-restful/v3" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"go.opentelemetry.io/otel" |
| 14 |
"go.opentelemetry.io/otel/propagation" |
| 15 |
oteltrace "go.opentelemetry.io/otel/trace" |
| 16 |
"go.opentelemetry.io/otel/trace/noop" |
| 17 |
|
| 18 |
"go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful" |
| 19 |
b3prop "go.opentelemetry.io/contrib/propagators/b3" |
| 20 |
) |
| 21 |
|
| 22 |
const tracerName = "go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful" |
| 23 |
|
| 24 |
func TestGetSpanNotInstrumented(t *testing.T) { |
| 25 |
handlerFunc := func(req *restful.Request, resp *restful.Response) { |
| 26 |
span := oteltrace.SpanFromContext(req.Request.Context()) |
| 27 |
ok := !span.SpanContext().IsValid() |
| 28 |
assert.True(t, ok) |
| 29 |
resp.WriteHeader(http.StatusOK) |
| 30 |
} |
| 31 |
ws := &restful.WebService{} |
| 32 |
ws.Route(ws.GET("/user/{id}").To(handlerFunc)) |
| 33 |
container := restful.NewContainer() |
| 34 |
container.Add(ws) |
| 35 |
|
| 36 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 37 |
w := httptest.NewRecorder() |
| 38 |
|
| 39 |
container.ServeHTTP(w, r) |
| 40 |
} |
| 41 |
|
| 42 |
func TestPropagationWithGlobalPropagators(t *testing.T) { |
| 43 |
defer func(p propagation.TextMapPropagator) { |
| 44 |
otel.SetTextMapPropagator(p) |
| 45 |
}(otel.GetTextMapPropagator()) |
| 46 |
provider := noop.NewTracerProvider() |
| 47 |
otel.SetTextMapPropagator(propagation.TraceContext{}) |
| 48 |
|
| 49 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 50 |
w := httptest.NewRecorder() |
| 51 |
|
| 52 |
ctx := t.Context() |
| 53 |
sc := oteltrace.NewSpanContext(oteltrace.SpanContextConfig{ |
| 54 |
TraceID: oteltrace.TraceID{0x01}, |
| 55 |
SpanID: oteltrace.SpanID{0x01}, |
| 56 |
}) |
| 57 |
ctx = oteltrace.ContextWithRemoteSpanContext(ctx, sc) |
| 58 |
ctx, _ = provider.Tracer(tracerName).Start(ctx, "test") |
| 59 |
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header)) |
| 60 |
|
| 61 |
handlerFunc := func(req *restful.Request, _ *restful.Response) { |
| 62 |
span := oteltrace.SpanFromContext(req.Request.Context()) |
| 63 |
assert.Equal(t, sc.TraceID(), span.SpanContext().TraceID()) |
| 64 |
assert.Equal(t, sc.SpanID(), span.SpanContext().SpanID()) |
| 65 |
w.WriteHeader(http.StatusOK) |
| 66 |
} |
| 67 |
ws := &restful.WebService{} |
| 68 |
ws.Route(ws.GET("/user/{id}").To(handlerFunc)) |
| 69 |
|
| 70 |
container := restful.NewContainer() |
| 71 |
container.Filter(otelrestful.OTelFilter("foobar", otelrestful.WithTracerProvider(provider))) |
| 72 |
container.Add(ws) |
| 73 |
|
| 74 |
container.ServeHTTP(w, r) |
| 75 |
} |
| 76 |
|
| 77 |
func TestPropagationWithCustomPropagators(t *testing.T) { |
| 78 |
provider := noop.NewTracerProvider() |
| 79 |
b3 := b3prop.New() |
| 80 |
|
| 81 |
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/user/123", http.NoBody) |
| 82 |
w := httptest.NewRecorder() |
| 83 |
|
| 84 |
ctx := t.Context() |
| 85 |
sc := oteltrace.NewSpanContext(oteltrace.SpanContextConfig{ |
| 86 |
TraceID: oteltrace.TraceID{0x01}, |
| 87 |
SpanID: oteltrace.SpanID{0x01}, |
| 88 |
}) |
| 89 |
ctx = oteltrace.ContextWithRemoteSpanContext(ctx, sc) |
| 90 |
ctx, _ = provider.Tracer(tracerName).Start(ctx, "test") |
| 91 |
b3.Inject(ctx, propagation.HeaderCarrier(r.Header)) |
| 92 |
|
| 93 |
handlerFunc := func(req *restful.Request, _ *restful.Response) { |
| 94 |
span := oteltrace.SpanFromContext(req.Request.Context()) |
| 95 |
assert.Equal(t, sc.TraceID(), span.SpanContext().TraceID()) |
| 96 |
assert.Equal(t, sc.SpanID(), span.SpanContext().SpanID()) |
| 97 |
w.WriteHeader(http.StatusOK) |
| 98 |
} |
| 99 |
ws := &restful.WebService{} |
| 100 |
ws.Route(ws.GET("/user/{id}").To(handlerFunc)) |
| 101 |
|
| 102 |
container := restful.NewContainer() |
| 103 |
container.Filter(otelrestful.OTelFilter("foobar", |
| 104 |
otelrestful.WithTracerProvider(provider), |
| 105 |
otelrestful.WithPropagators(b3))) |
| 106 |
container.Add(ws) |
| 107 |
|
| 108 |
container.ServeHTTP(w, r) |
| 109 |
} |