middleware/tracing: add test (#1083)

pull/1086/head
miya 4 years ago committed by GitHub
parent 30334dc47c
commit b22bc26a02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      go.mod
  2. 2
      go.sum
  3. 3
      middleware/recovery/recovery_test.go
  4. 76
      middleware/tracing/tracing_test.go

@ -10,6 +10,7 @@ require (
github.com/imdario/mergo v0.3.12
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/otel v0.20.0
go.opentelemetry.io/otel/sdk v0.20.0
go.opentelemetry.io/otel/trace v0.20.0
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c

@ -55,6 +55,8 @@ go.opentelemetry.io/otel/metric v0.20.0 h1:4kzhXFP+btKm4jwxpjIqjs41A7MakRFUS86bq
go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU=
go.opentelemetry.io/otel/oteltest v0.20.0 h1:HiITxCawalo5vQzdHfKeZurV8x7ljcqAgiWzF6Vaeaw=
go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw=
go.opentelemetry.io/otel/sdk v0.20.0 h1:JsxtGXd06J8jrnya7fdI/U/MR6yXA5DtbZy+qoHQlr8=
go.opentelemetry.io/otel/sdk v0.20.0/go.mod h1:g/IcepuwNsoiX5Byy2nNV0ySUF1em498m7hBWC279Yc=
go.opentelemetry.io/otel/trace v0.20.0 h1:1DL6EXUdcg95gukhuRRvLDO/4X5THh/5dIV52lqtnbw=
go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

@ -15,7 +15,6 @@ func TestOnce(t *testing.T) {
next := func(ctx context.Context, req interface{}) (interface{}, error) {
panic("panic reason")
}
next = Recovery()(next)
_, e := next(context.Background(), "panic")
_, e := Recovery()(next)(context.Background(), "panic")
t.Logf("succ and reason is %v", e)
}

@ -0,0 +1,76 @@
package tracing
import (
"context"
"net/http"
"testing"
"github.com/go-kratos/kratos/v2/transport"
"go.opentelemetry.io/otel/propagation"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
var (
_ transport.Transporter = &Transport{}
)
type headerCarrier http.Header
// Get returns the value associated with the passed key.
func (hc headerCarrier) Get(key string) string {
return http.Header(hc).Get(key)
}
// Set stores the key-value pair.
func (hc headerCarrier) Set(key string, value string) {
http.Header(hc).Set(key, value)
}
// Keys lists the keys stored in this carrier.
func (hc headerCarrier) Keys() []string {
keys := make([]string, 0, len(hc))
for k := range http.Header(hc) {
keys = append(keys, k)
}
return keys
}
type Transport struct {
kind transport.Kind
endpoint string
operation string
header headerCarrier
}
func (tr *Transport) Kind() transport.Kind { return tr.kind }
func (tr *Transport) Endpoint() string { return tr.endpoint }
func (tr *Transport) Operation() string { return tr.operation }
func (tr *Transport) Header() transport.Header { return tr.header }
func TestTracing(t *testing.T) {
var carrier = headerCarrier{}
tp := tracesdk.NewTracerProvider(tracesdk.WithSampler(tracesdk.TraceIDRatioBased(0)))
// caller use Inject
tracer := NewTracer(trace.SpanKindClient, WithTracerProvider(tp), WithPropagators(propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{})))
ts := &Transport{kind: transport.KindHTTP, header: carrier}
ctx, aboveSpan := tracer.Start(transport.NewClientContext(context.Background(), ts), ts.Kind().String(), ts.Operation(), ts.Header())
defer tracer.End(ctx, aboveSpan, nil)
// server use Extract fetch traceInfo from carrier
tracer = NewTracer(trace.SpanKindServer, WithPropagators(propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{})))
ts = &Transport{kind: transport.KindHTTP, header: carrier}
ctx, span := tracer.Start(transport.NewServerContext(ctx, ts), ts.Kind().String(), ts.Operation(), ts.Header())
defer tracer.End(ctx, span, nil)
if aboveSpan.SpanContext().TraceID() != span.SpanContext().TraceID() {
t.Fatalf("TraceID failed to deliver")
}
if v, ok := transport.FromClientContext(ctx); !ok || len(v.Header().Keys()) == 0 {
t.Fatalf("traceHeader failed to deliver")
}
}
Loading…
Cancel
Save