You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
3.9 KiB
Go
148 lines
3.9 KiB
Go
4 years ago
|
package handler
|
||
4 years ago
|
|
||
|
import (
|
||
3 years ago
|
"context"
|
||
2 years ago
|
"io"
|
||
4 years ago
|
"net/http"
|
||
|
"net/http/httptest"
|
||
2 years ago
|
"strconv"
|
||
4 years ago
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
3 years ago
|
ztrace "github.com/zeromicro/go-zero/core/trace"
|
||
2 years ago
|
"github.com/zeromicro/go-zero/rest/chain"
|
||
3 years ago
|
"go.opentelemetry.io/otel"
|
||
|
"go.opentelemetry.io/otel/propagation"
|
||
|
"go.opentelemetry.io/otel/trace"
|
||
4 years ago
|
)
|
||
|
|
||
3 years ago
|
func TestOtelHandler(t *testing.T) {
|
||
|
ztrace.StartAgent(ztrace.Config{
|
||
|
Name: "go-zero-test",
|
||
|
Endpoint: "http://localhost:14268/api/traces",
|
||
|
Batcher: "jaeger",
|
||
|
Sampler: 1.0,
|
||
|
})
|
||
2 years ago
|
defer ztrace.StopAgent()
|
||
3 years ago
|
|
||
3 years ago
|
for _, test := range []string{"", "bar"} {
|
||
|
t.Run(test, func(t *testing.T) {
|
||
2 years ago
|
h := chain.New(TraceHandler("foo", test)).Then(
|
||
3 years ago
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
2 years ago
|
span := trace.SpanFromContext(r.Context())
|
||
|
assert.True(t, span.SpanContext().IsValid())
|
||
|
assert.True(t, span.IsRecording())
|
||
3 years ago
|
}))
|
||
|
ts := httptest.NewServer(h)
|
||
|
defer ts.Close()
|
||
|
|
||
|
client := ts.Client()
|
||
|
err := func(ctx context.Context) error {
|
||
|
ctx, span := otel.Tracer("httptrace/client").Start(ctx, "test")
|
||
|
defer span.End()
|
||
|
|
||
|
req, _ := http.NewRequest("GET", ts.URL, nil)
|
||
|
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
|
||
|
|
||
|
res, err := client.Do(req)
|
||
|
assert.Nil(t, err)
|
||
|
return res.Body.Close()
|
||
|
}(context.Background())
|
||
|
|
||
|
assert.Nil(t, err)
|
||
|
})
|
||
|
}
|
||
4 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func TestDontTracingSpan(t *testing.T) {
|
||
2 years ago
|
ztrace.StartAgent(ztrace.Config{
|
||
|
Name: "go-zero-test",
|
||
|
Endpoint: "http://localhost:14268/api/traces",
|
||
|
Batcher: "jaeger",
|
||
|
Sampler: 1.0,
|
||
|
})
|
||
2 years ago
|
defer ztrace.StopAgent()
|
||
2 years ago
|
|
||
|
for _, test := range []string{"", "bar", "foo"} {
|
||
|
t.Run(test, func(t *testing.T) {
|
||
2 years ago
|
h := chain.New(TraceHandler("foo", test, WithTraceIgnorePaths([]string{"bar"}))).Then(
|
||
2 years ago
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
2 years ago
|
span := trace.SpanFromContext(r.Context())
|
||
|
spanCtx := span.SpanContext()
|
||
2 years ago
|
if test == "bar" {
|
||
|
assert.False(t, spanCtx.IsValid())
|
||
2 years ago
|
assert.False(t, span.IsRecording())
|
||
2 years ago
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
assert.True(t, span.IsRecording())
|
||
2 years ago
|
assert.True(t, spanCtx.IsValid())
|
||
|
}))
|
||
|
ts := httptest.NewServer(h)
|
||
|
defer ts.Close()
|
||
|
|
||
|
client := ts.Client()
|
||
|
err := func(ctx context.Context) error {
|
||
|
ctx, span := otel.Tracer("httptrace/client").Start(ctx, "test")
|
||
|
defer span.End()
|
||
|
|
||
|
req, _ := http.NewRequest("GET", ts.URL, nil)
|
||
|
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
|
||
|
|
||
|
res, err := client.Do(req)
|
||
|
assert.Nil(t, err)
|
||
|
return res.Body.Close()
|
||
|
}(context.Background())
|
||
|
|
||
|
assert.Nil(t, err)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
func TestTraceResponseWriter(t *testing.T) {
|
||
|
ztrace.StartAgent(ztrace.Config{
|
||
|
Name: "go-zero-test",
|
||
|
Endpoint: "http://localhost:14268/api/traces",
|
||
|
Batcher: "jaeger",
|
||
|
Sampler: 1.0,
|
||
|
})
|
||
|
defer ztrace.StopAgent()
|
||
|
|
||
|
for _, test := range []int{0, 200, 300, 400, 401, 500, 503} {
|
||
|
t.Run(strconv.Itoa(test), func(t *testing.T) {
|
||
2 years ago
|
h := chain.New(TraceHandler("foo", "bar")).Then(
|
||
2 years ago
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
span := trace.SpanFromContext(r.Context())
|
||
|
spanCtx := span.SpanContext()
|
||
|
assert.True(t, span.IsRecording())
|
||
|
assert.True(t, spanCtx.IsValid())
|
||
|
if test != 0 {
|
||
|
w.WriteHeader(test)
|
||
|
}
|
||
|
w.Write([]byte("hello"))
|
||
|
}))
|
||
|
ts := httptest.NewServer(h)
|
||
|
defer ts.Close()
|
||
|
|
||
|
client := ts.Client()
|
||
|
err := func(ctx context.Context) error {
|
||
|
ctx, span := otel.Tracer("httptrace/client").Start(ctx, "test")
|
||
|
defer span.End()
|
||
|
|
||
|
req, _ := http.NewRequest("GET", ts.URL, nil)
|
||
|
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
|
||
|
|
||
|
res, err := client.Do(req)
|
||
|
assert.Nil(t, err)
|
||
|
resBody := make([]byte, 5)
|
||
|
_, err = res.Body.Read(resBody)
|
||
|
assert.Equal(t, io.EOF, err)
|
||
|
assert.Equal(t, []byte("hello"), resBody, "response body fail")
|
||
|
return res.Body.Close()
|
||
|
}(context.Background())
|
||
|
|
||
|
assert.Nil(t, err)
|
||
|
})
|
||
|
}
|
||
|
}
|