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

@ -16,12 +16,12 @@ var (
)
type Transport struct {
kind string
kind transport.Kind
endpoint string
operation string
}
func (tr *Transport) Kind() string {
func (tr *Transport) Kind() transport.Kind {
return tr.kind
}
@ -52,21 +52,21 @@ func TestHTTP(t *testing.T) {
Server,
err,
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",
Server,
nil,
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",
Client,
nil,
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,
err,
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()
if info, ok := transport.FromServerContext(ctx); ok {
kind = info.Kind()
kind = info.Kind().String()
operation = info.Operation()
}
reply, err := handler(ctx, req)
@ -86,7 +86,7 @@ func Client(opts ...Option) middleware.Middleware {
)
startTime := time.Now()
if info, ok := transport.FromClientContext(ctx); ok {
kind = info.Kind()
kind = info.Kind().String()
operation = info.Operation()
}
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) {
if tr, ok := transport.FromServerContext(ctx); ok {
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) }()
}
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) {
if tr, ok := transport.FromClientContext(ctx); ok {
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) }()
}
return handler(ctx, req)

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

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

@ -31,12 +31,25 @@ type Header interface {
// Transporter is transport context value interface.
type Transporter interface {
Kind() string
Kind() Kind
Endpoint() string
Operation() string
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 clientTransportKey struct{}

Loading…
Cancel
Save