Package list golang-github-go-kit-kit / 0ecbed2
tracing: use improved FooToBar syntax Peter Bourgon 4 years ago
4 changed file(s) with 18 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
1111 "github.com/go-kit/kit/log"
1212 )
1313
14 // ToGRPCRequest returns a grpc RequestFunc that injects an OpenTracing Span
14 // ContextToGRPC returns a grpc RequestFunc that injects an OpenTracing Span
1515 // found in `ctx` into the grpc Metadata. If no such Span can be found, the
1616 // RequestFunc is a noop.
17 func ToGRPCRequest(tracer opentracing.Tracer, logger log.Logger) func(ctx context.Context, md *metadata.MD) context.Context {
17 func ContextToGRPC(tracer opentracing.Tracer, logger log.Logger) func(ctx context.Context, md *metadata.MD) context.Context {
1818 return func(ctx context.Context, md *metadata.MD) context.Context {
1919 if span := opentracing.SpanFromContext(ctx); span != nil {
2020 // There's nothing we can do with an error here.
2626 }
2727 }
2828
29 // FromGRPCRequest returns a grpc RequestFunc that tries to join with an
29 // GRPCToContext returns a grpc RequestFunc that tries to join with an
3030 // OpenTracing trace found in `req` and starts a new Span called
3131 // `operationName` accordingly. If no trace could be found in `req`, the Span
3232 // will be a trace root. The Span is incorporated in the returned Context and
3333 // can be retrieved with opentracing.SpanFromContext(ctx).
34 func FromGRPCRequest(tracer opentracing.Tracer, operationName string, logger log.Logger) func(ctx context.Context, md metadata.MD) context.Context {
34 func GRPCToContext(tracer opentracing.Tracer, operationName string, logger log.Logger) func(ctx context.Context, md metadata.MD) context.Context {
3535 return func(ctx context.Context, md metadata.MD) context.Context {
3636 var span opentracing.Span
3737 wireContext, err := tracer.Extract(opentracing.TextMap, metadataReaderWriter{&md})
2121 beforeSpan.SetBaggageItem("baggage", "check")
2222 beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
2323
24 toGRPCFunc := kitot.ToGRPCRequest(tracer, logger)
24 toGRPCFunc := kitot.ContextToGRPC(tracer, logger)
2525 md := metadata.Pairs()
2626 // Call the RequestFunc.
2727 afterCtx := toGRPCFunc(beforeCtx, &md)
3838 t.Errorf("Want %v span(s), found %v", want, have)
3939 }
4040
41 // Use FromGRPCRequest to verify that we can join with the trace given MD.
42 fromGRPCFunc := kitot.FromGRPCRequest(tracer, "joined", logger)
41 // Use GRPCToContext to verify that we can join with the trace given MD.
42 fromGRPCFunc := kitot.GRPCToContext(tracer, "joined", logger)
4343 joinCtx := fromGRPCFunc(afterCtx, md)
4444 joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
4545
1212 kithttp "github.com/go-kit/kit/transport/http"
1313 )
1414
15 // ToHTTPRequest returns an http RequestFunc that injects an OpenTracing Span
15 // ContextToHTTP returns an http RequestFunc that injects an OpenTracing Span
1616 // found in `ctx` into the http headers. If no such Span can be found, the
1717 // RequestFunc is a noop.
18 func ToHTTPRequest(tracer opentracing.Tracer, logger log.Logger) kithttp.RequestFunc {
18 func ContextToHTTP(tracer opentracing.Tracer, logger log.Logger) kithttp.RequestFunc {
1919 return func(ctx context.Context, req *http.Request) context.Context {
2020 // Try to find a Span in the Context.
2121 if span := opentracing.SpanFromContext(ctx); span != nil {
4545 }
4646 }
4747
48 // FromHTTPRequest returns an http RequestFunc that tries to join with an
48 // HTTPToContext returns an http RequestFunc that tries to join with an
4949 // OpenTracing trace found in `req` and starts a new Span called
5050 // `operationName` accordingly. If no trace could be found in `req`, the Span
5151 // will be a trace root. The Span is incorporated in the returned Context and
5252 // can be retrieved with opentracing.SpanFromContext(ctx).
53 func FromHTTPRequest(tracer opentracing.Tracer, operationName string, logger log.Logger) kithttp.RequestFunc {
53 func HTTPToContext(tracer opentracing.Tracer, operationName string, logger log.Logger) kithttp.RequestFunc {
5454 return func(ctx context.Context, req *http.Request) context.Context {
5555 // Try to join to a trace propagated in `req`.
5656 var span opentracing.Span
2323 beforeSpan.SetBaggageItem("baggage", "check")
2424 beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)
2525
26 toHTTPFunc := kitot.ToHTTPRequest(tracer, logger)
26 toHTTPFunc := kitot.ContextToHTTP(tracer, logger)
2727 req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
2828 // Call the RequestFunc.
2929 afterCtx := toHTTPFunc(beforeCtx, req)
4040 t.Errorf("Want %v span(s), found %v", want, have)
4141 }
4242
43 // Use FromHTTPRequest to verify that we can join with the trace given a req.
44 fromHTTPFunc := kitot.FromHTTPRequest(tracer, "joined", logger)
43 // Use HTTPToContext to verify that we can join with the trace given a req.
44 fromHTTPFunc := kitot.HTTPToContext(tracer, "joined", logger)
4545 joinCtx := fromHTTPFunc(afterCtx, req)
4646 joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
4747
6464 }
6565 }
6666
67 func TestToHTTPRequestTags(t *testing.T) {
67 func TestContextToHTTPTags(t *testing.T) {
6868 tracer := mocktracer.New()
6969 span := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
7070 defer span.Finish()
7171 ctx := opentracing.ContextWithSpan(context.Background(), span)
7272 req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
7373
74 kitot.ToHTTPRequest(tracer, log.NewNopLogger())(ctx, req)
74 kitot.ContextToHTTP(tracer, log.NewNopLogger())(ctx, req)
7575
7676 expectedTags := map[string]interface{}{
7777 string(ext.HTTPMethod): "GET",
8383 }
8484 }
8585
86 func TestFromHTTPRequestTags(t *testing.T) {
86 func TestHTTPToContextTags(t *testing.T) {
8787 tracer := mocktracer.New()
8888 parentSpan := tracer.StartSpan("to_extract").(*mocktracer.MockSpan)
8989 defer parentSpan.Finish()
9090 req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
9191 tracer.Inject(parentSpan.Context(), opentracing.TextMap, opentracing.HTTPHeadersCarrier(req.Header))
9292
93 ctx := kitot.FromHTTPRequest(tracer, "op", log.NewNopLogger())(context.Background(), req)
93 ctx := kitot.HTTPToContext(tracer, "op", log.NewNopLogger())(context.Background(), req)
9494 opentracing.SpanFromContext(ctx).Finish()
9595
9696 childSpan := tracer.FinishedSpans()[0]