Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions auth/jwt/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const (
bearerFormat string = "Bearer %s"
)

// ToHTTPContext moves JWT token from request header to context. Particularly
// HTTPToContext moves a JWT from request header to context. Particularly
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "a" article use is now inconsistent (present with "JWT", absent with "request header" and "context"), not sure if that's intentional, not a big deal.

// useful for servers.
func ToHTTPContext() http.RequestFunc {
func HTTPToContext() http.RequestFunc {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Context" seems to be in all of these names, so is it redundant? Would To/FromHTTP and To/FromGRPC be equally clear? I haven't taken a close look at the whole file, and I've never used the package, so I'm genuinely asking. :) If not, the names look good!

return func(ctx context.Context, r *stdhttp.Request) context.Context {
token, ok := extractTokenFromAuthHeader(r.Header.Get("Authorization"))
if !ok {
Expand All @@ -30,9 +30,9 @@ func ToHTTPContext() http.RequestFunc {
}
}

// FromHTTPContext moves JWT token from context to request header. Particularly
// ContextToHTTP moves a JWT from context to request header. Particularly
// useful for clients.
func FromHTTPContext() http.RequestFunc {
func ContextToHTTP() http.RequestFunc {
return func(ctx context.Context, r *stdhttp.Request) context.Context {
token, ok := ctx.Value(JWTTokenContextKey).(string)
if ok {
Expand All @@ -42,9 +42,9 @@ func FromHTTPContext() http.RequestFunc {
}
}

// ToGRPCContext moves JWT token from grpc metadata to context. Particularly
// GRPCToContext moves a JWT from grpc metadata to context. Particularly
// userful for servers.
func ToGRPCContext() grpc.ServerRequestFunc {
func GRPCToContext() grpc.ServerRequestFunc {
return func(ctx context.Context, md metadata.MD) context.Context {
// capital "Key" is illegal in HTTP/2.
authHeader, ok := md["authorization"]
Expand All @@ -61,9 +61,9 @@ func ToGRPCContext() grpc.ServerRequestFunc {
}
}

// FromGRPCContext moves JWT token from context to grpc metadata. Particularly
// ContextToGRPC moves a JWT from context to grpc metadata. Particularly
// useful for clients.
func FromGRPCContext() grpc.ClientRequestFunc {
func ContextToGRPC() grpc.ClientRequestFunc {
return func(ctx context.Context, md *metadata.MD) context.Context {
token, ok := ctx.Value(JWTTokenContextKey).(string)
if ok {
Expand Down
16 changes: 8 additions & 8 deletions auth/jwt/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"google.golang.org/grpc/metadata"
)

func TestToHTTPContext(t *testing.T) {
reqFunc := ToHTTPContext()
func TestHTTPToContext(t *testing.T) {
reqFunc := HTTPToContext()

// When the header doesn't exist
ctx := reqFunc(context.Background(), &http.Request{})
Expand Down Expand Up @@ -38,8 +38,8 @@ func TestToHTTPContext(t *testing.T) {
}
}

func TestFromHTTPContext(t *testing.T) {
reqFunc := FromHTTPContext()
func TestContextToHTTP(t *testing.T) {
reqFunc := ContextToHTTP()

// No JWT Token is passed in the context
ctx := context.Background()
Expand All @@ -64,9 +64,9 @@ func TestFromHTTPContext(t *testing.T) {
}
}

func TestToGRPCContext(t *testing.T) {
func TestGRPCToContext(t *testing.T) {
md := metadata.MD{}
reqFunc := ToGRPCContext()
reqFunc := GRPCToContext()

// No Authorization header is passed
ctx := reqFunc(context.Background(), md)
Expand Down Expand Up @@ -96,8 +96,8 @@ func TestToGRPCContext(t *testing.T) {
}
}

func TestFromGRPCContext(t *testing.T) {
reqFunc := FromGRPCContext()
func TestContextToGRPC(t *testing.T) {
reqFunc := ContextToGRPC()

// No JWT Token is passed in the context
ctx := context.Background()
Expand Down
8 changes: 4 additions & 4 deletions examples/addsvc/pkg/addtransport/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func NewGRPCServer(endpoints addendpoint.Set, tracer stdopentracing.Tracer, logg
endpoints.SumEndpoint,
decodeGRPCSumRequest,
encodeGRPCSumResponse,
append(options, grpctransport.ServerBefore(opentracing.FromGRPCRequest(tracer, "Sum", logger)))...,
append(options, grpctransport.ServerBefore(opentracing.GRPCToContext(tracer, "Sum", logger)))...,
),
concat: grpctransport.NewServer(
endpoints.ConcatEndpoint,
decodeGRPCConcatRequest,
encodeGRPCConcatResponse,
append(options, grpctransport.ServerBefore(opentracing.FromGRPCRequest(tracer, "Concat", logger)))...,
append(options, grpctransport.ServerBefore(opentracing.GRPCToContext(tracer, "Concat", logger)))...,
),
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func NewGRPCClient(conn *grpc.ClientConn, tracer stdopentracing.Tracer, logger l
encodeGRPCSumRequest,
decodeGRPCSumResponse,
pb.SumReply{},
grpctransport.ClientBefore(opentracing.ToGRPCRequest(tracer, logger)),
grpctransport.ClientBefore(opentracing.ContextToGRPC(tracer, logger)),
).Endpoint()
sumEndpoint = opentracing.TraceClient(tracer, "Sum")(sumEndpoint)
sumEndpoint = limiter(sumEndpoint)
Expand All @@ -112,7 +112,7 @@ func NewGRPCClient(conn *grpc.ClientConn, tracer stdopentracing.Tracer, logger l
encodeGRPCConcatRequest,
decodeGRPCConcatResponse,
pb.ConcatReply{},
grpctransport.ClientBefore(opentracing.ToGRPCRequest(tracer, logger)),
grpctransport.ClientBefore(opentracing.ContextToGRPC(tracer, logger)),
).Endpoint()
concatEndpoint = opentracing.TraceClient(tracer, "Concat")(concatEndpoint)
concatEndpoint = limiter(concatEndpoint)
Expand Down
8 changes: 4 additions & 4 deletions examples/addsvc/pkg/addtransport/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func NewHTTPHandler(endpoints addendpoint.Set, tracer stdopentracing.Tracer, log
endpoints.SumEndpoint,
decodeHTTPSumRequest,
encodeHTTPGenericResponse,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "Sum", logger)))...,
append(options, httptransport.ServerBefore(opentracing.HTTPToContext(tracer, "Sum", logger)))...,
))
m.Handle("/concat", httptransport.NewServer(
endpoints.ConcatEndpoint,
decodeHTTPConcatRequest,
encodeHTTPGenericResponse,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "Concat", logger)))...,
append(options, httptransport.ServerBefore(opentracing.HTTPToContext(tracer, "Concat", logger)))...,
))
return m
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func NewHTTPClient(instance string, tracer stdopentracing.Tracer, logger log.Log
copyURL(u, "/sum"),
encodeHTTPGenericRequest,
decodeHTTPSumResponse,
httptransport.ClientBefore(opentracing.ToHTTPRequest(tracer, logger)),
httptransport.ClientBefore(opentracing.ContextToHTTP(tracer, logger)),
).Endpoint()
sumEndpoint = opentracing.TraceClient(tracer, "Sum")(sumEndpoint)
sumEndpoint = limiter(sumEndpoint)
Expand All @@ -100,7 +100,7 @@ func NewHTTPClient(instance string, tracer stdopentracing.Tracer, logger log.Log
copyURL(u, "/concat"),
encodeHTTPGenericRequest,
decodeHTTPConcatResponse,
httptransport.ClientBefore(opentracing.ToHTTPRequest(tracer, logger)),
httptransport.ClientBefore(opentracing.ContextToHTTP(tracer, logger)),
).Endpoint()
concatEndpoint = opentracing.TraceClient(tracer, "Concat")(concatEndpoint)
concatEndpoint = limiter(concatEndpoint)
Expand Down
8 changes: 4 additions & 4 deletions tracing/opentracing/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/go-kit/kit/log"
)

// ToGRPCRequest returns a grpc RequestFunc that injects an OpenTracing Span
// ContextToGRPC returns a grpc RequestFunc that injects an OpenTracing Span
// found in `ctx` into the grpc Metadata. If no such Span can be found, the
// RequestFunc is a noop.
func ToGRPCRequest(tracer opentracing.Tracer, logger log.Logger) func(ctx context.Context, md *metadata.MD) context.Context {
func ContextToGRPC(tracer opentracing.Tracer, logger log.Logger) func(ctx context.Context, md *metadata.MD) context.Context {
return func(ctx context.Context, md *metadata.MD) context.Context {
if span := opentracing.SpanFromContext(ctx); span != nil {
// There's nothing we can do with an error here.
Expand All @@ -27,12 +27,12 @@ func ToGRPCRequest(tracer opentracing.Tracer, logger log.Logger) func(ctx contex
}
}

// FromGRPCRequest returns a grpc RequestFunc that tries to join with an
// GRPCToContext returns a grpc RequestFunc that tries to join with an
// OpenTracing trace found in `req` and starts a new Span called
// `operationName` accordingly. If no trace could be found in `req`, the Span
// will be a trace root. The Span is incorporated in the returned Context and
// can be retrieved with opentracing.SpanFromContext(ctx).
func FromGRPCRequest(tracer opentracing.Tracer, operationName string, logger log.Logger) func(ctx context.Context, md metadata.MD) context.Context {
func GRPCToContext(tracer opentracing.Tracer, operationName string, logger log.Logger) func(ctx context.Context, md metadata.MD) context.Context {
return func(ctx context.Context, md metadata.MD) context.Context {
var span opentracing.Span
wireContext, err := tracer.Extract(opentracing.TextMap, metadataReaderWriter{&md})
Expand Down
6 changes: 3 additions & 3 deletions tracing/opentracing/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestTraceGRPCRequestRoundtrip(t *testing.T) {
beforeSpan.SetBaggageItem("baggage", "check")
beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)

toGRPCFunc := kitot.ToGRPCRequest(tracer, logger)
toGRPCFunc := kitot.ContextToGRPC(tracer, logger)
md := metadata.Pairs()
// Call the RequestFunc.
afterCtx := toGRPCFunc(beforeCtx, &md)
Expand All @@ -39,8 +39,8 @@ func TestTraceGRPCRequestRoundtrip(t *testing.T) {
t.Errorf("Want %v span(s), found %v", want, have)
}

// Use FromGRPCRequest to verify that we can join with the trace given MD.
fromGRPCFunc := kitot.FromGRPCRequest(tracer, "joined", logger)
// Use GRPCToContext to verify that we can join with the trace given MD.
fromGRPCFunc := kitot.GRPCToContext(tracer, "joined", logger)
joinCtx := fromGRPCFunc(afterCtx, md)
joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)

Expand Down
8 changes: 4 additions & 4 deletions tracing/opentracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
kithttp "github.com/go-kit/kit/transport/http"
)

// ToHTTPRequest returns an http RequestFunc that injects an OpenTracing Span
// ContextToHTTP returns an http RequestFunc that injects an OpenTracing Span
// found in `ctx` into the http headers. If no such Span can be found, the
// RequestFunc is a noop.
func ToHTTPRequest(tracer opentracing.Tracer, logger log.Logger) kithttp.RequestFunc {
func ContextToHTTP(tracer opentracing.Tracer, logger log.Logger) kithttp.RequestFunc {
return func(ctx context.Context, req *http.Request) context.Context {
// Try to find a Span in the Context.
if span := opentracing.SpanFromContext(ctx); span != nil {
Expand Down Expand Up @@ -46,12 +46,12 @@ func ToHTTPRequest(tracer opentracing.Tracer, logger log.Logger) kithttp.Request
}
}

// FromHTTPRequest returns an http RequestFunc that tries to join with an
// HTTPToContext returns an http RequestFunc that tries to join with an
// OpenTracing trace found in `req` and starts a new Span called
// `operationName` accordingly. If no trace could be found in `req`, the Span
// will be a trace root. The Span is incorporated in the returned Context and
// can be retrieved with opentracing.SpanFromContext(ctx).
func FromHTTPRequest(tracer opentracing.Tracer, operationName string, logger log.Logger) kithttp.RequestFunc {
func HTTPToContext(tracer opentracing.Tracer, operationName string, logger log.Logger) kithttp.RequestFunc {
return func(ctx context.Context, req *http.Request) context.Context {
// Try to join to a trace propagated in `req`.
var span opentracing.Span
Expand Down
14 changes: 7 additions & 7 deletions tracing/opentracing/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestTraceHTTPRequestRoundtrip(t *testing.T) {
beforeSpan.SetBaggageItem("baggage", "check")
beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)

toHTTPFunc := kitot.ToHTTPRequest(tracer, logger)
toHTTPFunc := kitot.ContextToHTTP(tracer, logger)
req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
// Call the RequestFunc.
afterCtx := toHTTPFunc(beforeCtx, req)
Expand All @@ -41,8 +41,8 @@ func TestTraceHTTPRequestRoundtrip(t *testing.T) {
t.Errorf("Want %v span(s), found %v", want, have)
}

// Use FromHTTPRequest to verify that we can join with the trace given a req.
fromHTTPFunc := kitot.FromHTTPRequest(tracer, "joined", logger)
// Use HTTPToContext to verify that we can join with the trace given a req.
fromHTTPFunc := kitot.HTTPToContext(tracer, "joined", logger)
joinCtx := fromHTTPFunc(afterCtx, req)
joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)

Expand All @@ -65,14 +65,14 @@ func TestTraceHTTPRequestRoundtrip(t *testing.T) {
}
}

func TestToHTTPRequestTags(t *testing.T) {
func TestContextToHTTPTags(t *testing.T) {
tracer := mocktracer.New()
span := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
defer span.Finish()
ctx := opentracing.ContextWithSpan(context.Background(), span)
req, _ := http.NewRequest("GET", "http://test.biz/path", nil)

kitot.ToHTTPRequest(tracer, log.NewNopLogger())(ctx, req)
kitot.ContextToHTTP(tracer, log.NewNopLogger())(ctx, req)

expectedTags := map[string]interface{}{
string(ext.HTTPMethod): "GET",
Expand All @@ -84,14 +84,14 @@ func TestToHTTPRequestTags(t *testing.T) {
}
}

func TestFromHTTPRequestTags(t *testing.T) {
func TestHTTPToContextTags(t *testing.T) {
tracer := mocktracer.New()
parentSpan := tracer.StartSpan("to_extract").(*mocktracer.MockSpan)
defer parentSpan.Finish()
req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
tracer.Inject(parentSpan.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(req.Header))

ctx := kitot.FromHTTPRequest(tracer, "op", log.NewNopLogger())(context.Background(), req)
ctx := kitot.HTTPToContext(tracer, "op", log.NewNopLogger())(context.Background(), req)
opentracing.SpanFromContext(ctx).Finish()

childSpan := tracer.FinishedSpans()[0]
Expand Down