Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Set SpanKind for HTTP and gRPC requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll committed Mar 20, 2018
1 parent 0967e1d commit 9c24be4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
3 changes: 3 additions & 0 deletions plugin/ocgrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
// traces. Use with gRPC clients only.
type ClientHandler struct {
// StartOptions allows configuring the StartOptions used to create new spans.
//
// StartOptions.SpanKind will always be set to trace.SpanKindClient
// for spans started by this handler.
StartOptions trace.StartOptions
}

Expand Down
4 changes: 4 additions & 0 deletions plugin/ocgrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ type ServerHandler struct {
// present on the inbound RPC but the SpanContext is not sampled. This
// ensures that each service has some opportunity to be traced. If you would
// like to not add any additional traces for this gRPC service, set:
//
// StartOptions.Sampler = trace.ProbabilitySampler(0.0)
//
// StartOptions.SpanKind will always be set to trace.SpanKindServer
// for spans started by this handler.
StartOptions trace.StartOptions
}

Expand Down
14 changes: 11 additions & 3 deletions plugin/ocgrpc/trace_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const traceContextKey = "grpc-trace-bin"
// SpanContext added to the outgoing gRPC metadata.
func (c *ClientHandler) traceTagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
name := "Sent" + strings.Replace(rti.FullMethodName, "/", ".", -1)
span := trace.NewSpan(name, trace.FromContext(ctx), c.StartOptions) // span is ended by traceHandleRPC
span := trace.NewSpan(name, trace.FromContext(ctx), trace.StartOptions{
Sampler: c.StartOptions.Sampler,
SpanKind: trace.SpanKindClient,
}) // span is ended by traceHandleRPC
ctx = trace.WithSpan(ctx, span)
traceContextBinary := propagation.Binary(span.SpanContext())
return metadata.AppendToOutgoingContext(ctx, traceContextKey, string(traceContextBinary))
Expand All @@ -48,6 +51,11 @@ func (c *ClientHandler) traceTagRPC(ctx context.Context, rti *stats.RPCTagInfo)
//
// It returns ctx, with the new trace span added.
func (s *ServerHandler) traceTagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
opts := trace.StartOptions{
Sampler: s.StartOptions.Sampler,
SpanKind: trace.SpanKindServer,
}

md, _ := metadata.FromIncomingContext(ctx)
name := "Recv" + strings.Replace(rti.FullMethodName, "/", ".", -1)
traceContext := md[traceContextKey]
Expand All @@ -62,11 +70,11 @@ func (s *ServerHandler) traceTagRPC(ctx context.Context, rti *stats.RPCTagInfo)
traceContextBinary := []byte(traceContext[0])
parent, haveParent = propagation.FromBinary(traceContextBinary)
if haveParent && !s.IsPublicEndpoint {
span := trace.NewSpanWithRemoteParent(name, parent, s.StartOptions)
span := trace.NewSpanWithRemoteParent(name, parent, opts)
return trace.WithSpan(ctx, span)
}
}
span := trace.NewSpan(name, nil, s.StartOptions)
span := trace.NewSpan(name, nil, opts)
if haveParent {
span.AddLink(trace.Link{TraceID: parent.TraceID, SpanID: parent.SpanID, Type: trace.LinkTypeChild})
}
Expand Down
12 changes: 9 additions & 3 deletions plugin/ochttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type Transport struct {

// StartOptions are applied to the span started by this Transport around each
// request.
//
// StartOptions.SpanKind will always be set to trace.SpanKindClient
// for spans started by this transport.
StartOptions trace.StartOptions

// TODO: Implement tag propagation for HTTP.
Expand All @@ -52,9 +55,12 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
format = defaultFormat
}
rt = &traceTransport{
base: rt,
format: format,
startOptions: t.StartOptions,
base: rt,
format: format,
startOptions: trace.StartOptions{
Sampler: t.StartOptions.Sampler,
SpanKind: trace.SpanKindClient,
},
}
rt = statsTransport{base: rt}
return rt.RoundTrip(req)
Expand Down
12 changes: 10 additions & 2 deletions plugin/ochttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type Handler struct {

// StartOptions are applied to the span started by this Handler around each
// request.
//
// StartOptions.SpanKind will always be set to trace.SpanKindServer
// for spans started by this transport.
StartOptions trace.StartOptions

// IsPublicEndpoint should be set to true for publicly accessible HTTP(S)
Expand All @@ -71,15 +74,20 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Request, func()) {
opts := trace.StartOptions{
Sampler: h.StartOptions.Sampler,
SpanKind: trace.SpanKindServer,
}

name := spanNameFromURL("Recv", r.URL)
ctx := r.Context()
var span *trace.Span
sc, ok := h.extractSpanContext(r)
if ok && !h.IsPublicEndpoint {
span = trace.NewSpanWithRemoteParent(name, sc, h.StartOptions)
span = trace.NewSpanWithRemoteParent(name, sc, opts)
ctx = trace.WithSpan(ctx, span)
} else {
span = trace.NewSpan(name, nil, h.StartOptions)
span = trace.NewSpan(name, nil, opts)
if ok {
span.AddLink(trace.Link{
TraceID: sc.TraceID,
Expand Down

0 comments on commit 9c24be4

Please sign in to comment.