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 33aa51a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 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
2 changes: 2 additions & 0 deletions plugin/ocgrpc/trace_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const traceContextKey = "grpc-trace-bin"
// It returns ctx with the new trace span added and a serialization of the
// SpanContext added to the outgoing gRPC metadata.
func (c *ClientHandler) traceTagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
c.StartOptions.SpanKind = trace.SpanKindClient
name := "Sent" + strings.Replace(rti.FullMethodName, "/", ".", -1)
span := trace.NewSpan(name, trace.FromContext(ctx), c.StartOptions) // span is ended by traceHandleRPC
ctx = trace.WithSpan(ctx, span)
Expand All @@ -48,6 +49,7 @@ 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 {
s.StartOptions.SpanKind = trace.SpanKindServer
md, _ := metadata.FromIncomingContext(ctx)
name := "Recv" + strings.Replace(rti.FullMethodName, "/", ".", -1)
traceContext := md[traceContextKey]
Expand Down
4 changes: 4 additions & 0 deletions plugin/ochttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ 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.
}

// RoundTrip implements http.RoundTripper, delegating to Base and recording stats and traces for the request.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
t.StartOptions.SpanKind = trace.SpanKindClient
rt := t.base()
// TODO: remove excessive nesting of http.RoundTrippers here.
format := t.Propagation
Expand Down
4 changes: 4 additions & 0 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,6 +74,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Request, func()) {
h.StartOptions.SpanKind = trace.SpanKindServer
name := spanNameFromURL("Recv", r.URL)
ctx := r.Context()
var span *trace.Span
Expand Down

0 comments on commit 33aa51a

Please sign in to comment.