transport: add transport kind (#1059)

* add transport kind
pull/1060/head
Tony Chen 3 years ago committed by GitHub
parent 258e911f4f
commit 6ee0607f03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      middleware/logging/logging.go
  2. 12
      middleware/logging/logging_test.go
  3. 4
      middleware/metrics/metrics.go
  4. 4
      middleware/tracing/tracing.go
  5. 4
      transport/grpc/transport.go
  6. 4
      transport/http/transport.go
  7. 15
      transport/transport.go

@ -23,7 +23,7 @@ func Server(logger log.Logger) middleware.Middleware {
) )
startTime := time.Now() startTime := time.Now()
if info, ok := transport.FromServerContext(ctx); ok { if info, ok := transport.FromServerContext(ctx); ok {
kind = info.Kind() kind = info.Kind().String()
operation = info.Operation() operation = info.Operation()
} }
reply, err = handler(ctx, req) reply, err = handler(ctx, req)
@ -59,7 +59,7 @@ func Client(logger log.Logger) middleware.Middleware {
) )
startTime := time.Now() startTime := time.Now()
if info, ok := transport.FromClientContext(ctx); ok { if info, ok := transport.FromClientContext(ctx); ok {
kind = info.Kind() kind = info.Kind().String()
operation = info.Operation() operation = info.Operation()
} }
reply, err = handler(ctx, req) reply, err = handler(ctx, req)

@ -16,12 +16,12 @@ var (
) )
type Transport struct { type Transport struct {
kind string kind transport.Kind
endpoint string endpoint string
operation string operation string
} }
func (tr *Transport) Kind() string { func (tr *Transport) Kind() transport.Kind {
return tr.kind return tr.kind
} }
@ -52,21 +52,21 @@ func TestHTTP(t *testing.T) {
Server, Server,
err, err,
func() context.Context { func() context.Context {
return transport.NewServerContext(context.Background(), &Transport{kind: "http", endpoint: "endpoint", operation: "/package.service/method"}) return transport.NewServerContext(context.Background(), &Transport{kind: transport.KindHTTP, endpoint: "endpoint", operation: "/package.service/method"})
}(), }(),
}, },
{"http-server@succ", {"http-server@succ",
Server, Server,
nil, nil,
func() context.Context { func() context.Context {
return transport.NewServerContext(context.Background(), &Transport{kind: "http", endpoint: "endpoint", operation: "/package.service/method"}) return transport.NewServerContext(context.Background(), &Transport{kind: transport.KindHTTP, endpoint: "endpoint", operation: "/package.service/method"})
}(), }(),
}, },
{"http-client@succ", {"http-client@succ",
Client, Client,
nil, nil,
func() context.Context { func() context.Context {
return transport.NewClientContext(context.Background(), &Transport{kind: "http", endpoint: "endpoint", operation: "/package.service/method"}) return transport.NewClientContext(context.Background(), &Transport{kind: transport.KindHTTP, endpoint: "endpoint", operation: "/package.service/method"})
}(), }(),
}, },
@ -74,7 +74,7 @@ func TestHTTP(t *testing.T) {
Client, Client,
err, err,
func() context.Context { func() context.Context {
return transport.NewClientContext(context.Background(), &Transport{kind: "http", endpoint: "endpoint", operation: "/package.service/method"}) return transport.NewClientContext(context.Background(), &Transport{kind: transport.KindHTTP, endpoint: "endpoint", operation: "/package.service/method"})
}(), }(),
}, },
} }

@ -51,7 +51,7 @@ func Server(opts ...Option) middleware.Middleware {
) )
startTime := time.Now() startTime := time.Now()
if info, ok := transport.FromServerContext(ctx); ok { if info, ok := transport.FromServerContext(ctx); ok {
kind = info.Kind() kind = info.Kind().String()
operation = info.Operation() operation = info.Operation()
} }
reply, err := handler(ctx, req) reply, err := handler(ctx, req)
@ -86,7 +86,7 @@ func Client(opts ...Option) middleware.Middleware {
) )
startTime := time.Now() startTime := time.Now()
if info, ok := transport.FromClientContext(ctx); ok { if info, ok := transport.FromClientContext(ctx); ok {
kind = info.Kind() kind = info.Kind().String()
operation = info.Operation() operation = info.Operation()
} }
reply, err := handler(ctx, req) reply, err := handler(ctx, req)

@ -38,7 +38,7 @@ func Server(opts ...Option) middleware.Middleware {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) { return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
if tr, ok := transport.FromServerContext(ctx); ok { if tr, ok := transport.FromServerContext(ctx); ok {
var span trace.Span var span trace.Span
ctx, span = tracer.Start(ctx, tr.Kind(), tr.Operation(), tr.Header()) ctx, span = tracer.Start(ctx, tr.Kind().String(), tr.Operation(), tr.Header())
defer func() { tracer.End(ctx, span, err) }() defer func() { tracer.End(ctx, span, err) }()
} }
return handler(ctx, req) return handler(ctx, req)
@ -53,7 +53,7 @@ func Client(opts ...Option) middleware.Middleware {
return func(ctx context.Context, req interface{}) (reply interface{}, err error) { return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
if tr, ok := transport.FromClientContext(ctx); ok { if tr, ok := transport.FromClientContext(ctx); ok {
var span trace.Span var span trace.Span
ctx, span = tracer.Start(ctx, tr.Kind(), tr.Operation(), tr.Header()) ctx, span = tracer.Start(ctx, tr.Kind().String(), tr.Operation(), tr.Header())
defer func() { tracer.End(ctx, span, err) }() defer func() { tracer.End(ctx, span, err) }()
} }
return handler(ctx, req) return handler(ctx, req)

@ -17,8 +17,8 @@ type Transport struct {
} }
// Kind returns the transport kind. // Kind returns the transport kind.
func (tr *Transport) Kind() string { func (tr *Transport) Kind() transport.Kind {
return "grpc" return transport.KindGRPC
} }
// Endpoint returns the transport endpoint. // Endpoint returns the transport endpoint.

@ -21,8 +21,8 @@ type Transport struct {
} }
// Kind returns the transport kind. // Kind returns the transport kind.
func (tr *Transport) Kind() string { func (tr *Transport) Kind() transport.Kind {
return "http" return transport.KindHTTP
} }
// Endpoint returns the transport endpoint. // Endpoint returns the transport endpoint.

@ -31,12 +31,25 @@ type Header interface {
// Transporter is transport context value interface. // Transporter is transport context value interface.
type Transporter interface { type Transporter interface {
Kind() string Kind() Kind
Endpoint() string Endpoint() string
Operation() string Operation() string
Header() Header Header() Header
} }
// Kind defines the type of Transport
type Kind string
func (k Kind) String() string {
return string(k)
}
// Defines a set of transport kind
const (
KindGRPC Kind = "grpc"
KindHTTP Kind = "http"
)
type serverTransportKey struct{} type serverTransportKey struct{}
type clientTransportKey struct{} type clientTransportKey struct{}

Loading…
Cancel
Save