From f78850efb070eb4f5dd72fec405ed50db21a5973 Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Tue, 18 Jul 2017 19:54:03 -0700 Subject: [PATCH 1/4] auth/jwt: use improved FooToBar syntax --- auth/jwt/transport.go | 16 ++++++++-------- auth/jwt/transport_test.go | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/auth/jwt/transport.go b/auth/jwt/transport.go index 7be7db417..bf41e6394 100644 --- a/auth/jwt/transport.go +++ b/auth/jwt/transport.go @@ -17,9 +17,9 @@ const ( bearerFormat string = "Bearer %s" ) -// ToHTTPContext moves JWT token from request header to context. Particularly +// HTTPToContext moves JWT token from request header to context. Particularly // useful for servers. -func ToHTTPContext() http.RequestFunc { +func HTTPToContext() http.RequestFunc { return func(ctx context.Context, r *stdhttp.Request) context.Context { token, ok := extractTokenFromAuthHeader(r.Header.Get("Authorization")) if !ok { @@ -30,9 +30,9 @@ func ToHTTPContext() http.RequestFunc { } } -// FromHTTPContext moves JWT token from context to request header. Particularly +// ContextToHTTP moves JWT token 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 { @@ -42,9 +42,9 @@ func FromHTTPContext() http.RequestFunc { } } -// ToGRPCContext moves JWT token from grpc metadata to context. Particularly +// GRPCToContext moves JWT token 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"] @@ -61,9 +61,9 @@ func ToGRPCContext() grpc.ServerRequestFunc { } } -// FromGRPCContext moves JWT token from context to grpc metadata. Particularly +// ContextToGRPC moves JWT token 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 { diff --git a/auth/jwt/transport_test.go b/auth/jwt/transport_test.go index b04d76feb..83d0f17f8 100644 --- a/auth/jwt/transport_test.go +++ b/auth/jwt/transport_test.go @@ -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{}) @@ -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() @@ -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) @@ -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() From 34e8d901c7c47e10984bc1069950e0700716f709 Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Tue, 18 Jul 2017 19:54:12 -0700 Subject: [PATCH 2/4] examples/addsvc: use improved FooToBar syntax --- examples/addsvc/pkg/addtransport/grpc.go | 8 ++++---- examples/addsvc/pkg/addtransport/http.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/addsvc/pkg/addtransport/grpc.go b/examples/addsvc/pkg/addtransport/grpc.go index cfb4243b7..ec05baa82 100644 --- a/examples/addsvc/pkg/addtransport/grpc.go +++ b/examples/addsvc/pkg/addtransport/grpc.go @@ -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)))..., ), } } @@ -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) @@ -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) diff --git a/examples/addsvc/pkg/addtransport/http.go b/examples/addsvc/pkg/addtransport/http.go index 9759d157b..ecdee9288 100644 --- a/examples/addsvc/pkg/addtransport/http.go +++ b/examples/addsvc/pkg/addtransport/http.go @@ -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 } @@ -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) @@ -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) From 0ecbed2b93b47b650e1ae75c9e1107f98718c813 Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Tue, 18 Jul 2017 19:54:21 -0700 Subject: [PATCH 3/4] tracing: use improved FooToBar syntax --- tracing/opentracing/grpc.go | 8 ++++---- tracing/opentracing/grpc_test.go | 6 +++--- tracing/opentracing/http.go | 8 ++++---- tracing/opentracing/http_test.go | 14 +++++++------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tracing/opentracing/grpc.go b/tracing/opentracing/grpc.go index fa4544009..0122d65e4 100644 --- a/tracing/opentracing/grpc.go +++ b/tracing/opentracing/grpc.go @@ -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. @@ -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}) diff --git a/tracing/opentracing/grpc_test.go b/tracing/opentracing/grpc_test.go index 3d07a14aa..a36fa1509 100644 --- a/tracing/opentracing/grpc_test.go +++ b/tracing/opentracing/grpc_test.go @@ -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) @@ -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) diff --git a/tracing/opentracing/http.go b/tracing/opentracing/http.go index 78286cd7c..09dd7149a 100644 --- a/tracing/opentracing/http.go +++ b/tracing/opentracing/http.go @@ -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 { @@ -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 diff --git a/tracing/opentracing/http_test.go b/tracing/opentracing/http_test.go index 61c1c1623..548777e78 100644 --- a/tracing/opentracing/http_test.go +++ b/tracing/opentracing/http_test.go @@ -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) @@ -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) @@ -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", @@ -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] From 3e883130f430c1a6cbe09394a7060ad04ad46c9e Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Tue, 18 Jul 2017 20:04:39 -0700 Subject: [PATCH 4/4] auth/jwt: fix doc grammar --- auth/jwt/transport.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/auth/jwt/transport.go b/auth/jwt/transport.go index bf41e6394..57b3aaeef 100644 --- a/auth/jwt/transport.go +++ b/auth/jwt/transport.go @@ -17,7 +17,7 @@ const ( bearerFormat string = "Bearer %s" ) -// HTTPToContext moves JWT token from request header to context. Particularly +// HTTPToContext moves a JWT from request header to context. Particularly // useful for servers. func HTTPToContext() http.RequestFunc { return func(ctx context.Context, r *stdhttp.Request) context.Context { @@ -30,7 +30,7 @@ func HTTPToContext() http.RequestFunc { } } -// ContextToHTTP moves JWT token from context to request header. Particularly +// ContextToHTTP moves a JWT from context to request header. Particularly // useful for clients. func ContextToHTTP() http.RequestFunc { return func(ctx context.Context, r *stdhttp.Request) context.Context { @@ -42,7 +42,7 @@ func ContextToHTTP() http.RequestFunc { } } -// GRPCToContext moves JWT token from grpc metadata to context. Particularly +// GRPCToContext moves a JWT from grpc metadata to context. Particularly // userful for servers. func GRPCToContext() grpc.ServerRequestFunc { return func(ctx context.Context, md metadata.MD) context.Context { @@ -61,7 +61,7 @@ func GRPCToContext() grpc.ServerRequestFunc { } } -// ContextToGRPC moves JWT token from context to grpc metadata. Particularly +// ContextToGRPC moves a JWT from context to grpc metadata. Particularly // useful for clients. func ContextToGRPC() grpc.ClientRequestFunc { return func(ctx context.Context, md *metadata.MD) context.Context {