Skip to content

Commit

Permalink
Don't trace http requests when using httpgrpc. (#124)
Browse files Browse the repository at this point in the history
* Don't trace http requests when using httpgrpc.

* Demonstrate how the same can be acheived with middleware.Tracer.
  • Loading branch information
tomwilkie authored and leth committed Sep 14, 2018
1 parent 1d0e909 commit c831f11
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
12 changes: 1 addition & 11 deletions httpgrpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
"github.com/mwitkow/go-grpc-middleware"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/sercand/kuberesolver"
"golang.org/x/net/context"
"google.golang.org/grpc"
Expand Down Expand Up @@ -44,18 +43,9 @@ func (s Server) Handle(ctx context.Context, r *httpgrpc.HTTPRequest) (*httpgrpc.
return nil, err
}
toHeader(r.Headers, req.Header)
if tracer := opentracing.GlobalTracer(); tracer != nil {
clientContext, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
if err == nil {
span := tracer.StartSpan("httpgrpc", ext.RPCServerOption(clientContext))
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
} else if err != opentracing.ErrSpanContextNotFound {
logging.Global().Warnf("Failed to extract tracing headers from request: %v", err)
}
}
req = req.WithContext(ctx)
req.RequestURI = r.Url

recorder := httptest.NewRecorder()
s.handler.ServeHTTP(recorder, req)
resp := &httpgrpc.HTTPResponse{
Expand Down
14 changes: 9 additions & 5 deletions httpgrpc/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
jaegercfg "github.com/uber/jaeger-client-go/config"
"google.golang.org/grpc"

"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/middleware"
"github.com/weaveworks/common/user"
"google.golang.org/grpc"
)

type testServer struct {
Expand Down Expand Up @@ -110,10 +112,12 @@ func TestTracePropagation(t *testing.T) {
defer closer.Close()
require.NoError(t, err)

server, err := newTestServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span := opentracing.SpanFromContext(r.Context())
fmt.Fprint(w, span.BaggageItem("name"))
}))
server, err := newTestServer(middleware.Tracer{}.Wrap(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span := opentracing.SpanFromContext(r.Context())
fmt.Fprint(w, span.BaggageItem("name"))
}),
))

require.NoError(t, err)
defer server.grpcServer.GracefulStop()
Expand Down

0 comments on commit c831f11

Please sign in to comment.