Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate tracing context over httpgrpc #105

Merged
merged 2 commits into from
Jul 23, 2018
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
21 changes: 20 additions & 1 deletion httpgrpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ 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"

"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/logging"
"github.com/weaveworks/common/middleware"
)

Expand All @@ -41,8 +43,18 @@ func (s Server) Handle(ctx context.Context, r *httpgrpc.HTTPRequest) (*httpgrpc.
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
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)
Expand Down Expand Up @@ -138,6 +150,13 @@ func (c *Client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if tracer := opentracing.GlobalTracer(); tracer != nil {
if span := opentracing.SpanFromContext(r.Context()); span != nil {
if err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header)); err != nil {
logging.Global().Warnf("Failed to inject tracing headers into request: %v", err)
}
}
}
req := &httpgrpc.HTTPRequest{
Method: r.Method,
Url: r.RequestURI,
Expand Down
33 changes: 33 additions & 0 deletions httpgrpc/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"reflect"
"testing"

opentracing "github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
jaegercfg "github.com/uber/jaeger-client-go/config"
"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/user"
"google.golang.org/grpc"
Expand Down Expand Up @@ -101,3 +103,34 @@ func TestParseURL(t *testing.T) {
assert.Equal(t, tc.expected, got)
}
}

func TestTracePropagation(t *testing.T) {
jaeger := jaegercfg.Configuration{}
closer, err := jaeger.InitGlobalTracer("test")
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"))
}))

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

client, err := NewClient(server.URL)
require.NoError(t, err)

req, err := http.NewRequest("GET", "/hello", &bytes.Buffer{})
require.NoError(t, err)

sp, ctx := opentracing.StartSpanFromContext(context.Background(), "Test")
sp.SetBaggageItem("name", "world")

req = req.WithContext(user.InjectOrgID(ctx, "1"))
recorder := httptest.NewRecorder()
client.ServeHTTP(recorder, req)

assert.Equal(t, "world", string(recorder.Body.Bytes()))
assert.Equal(t, 200, recorder.Code)
}