...

Source file src/go.opencensus.io/plugin/ocgrpc/trace_common.go

Documentation: go.opencensus.io/plugin/ocgrpc

     1  // Copyright 2017, OpenCensus Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ocgrpc
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  
    21  	"google.golang.org/grpc/codes"
    22  	"google.golang.org/grpc/metadata"
    23  	"google.golang.org/grpc/stats"
    24  	"google.golang.org/grpc/status"
    25  
    26  	"go.opencensus.io/trace"
    27  	"go.opencensus.io/trace/propagation"
    28  )
    29  
    30  const traceContextKey = "grpc-trace-bin"
    31  
    32  // TagRPC creates a new trace span for the client side of the RPC.
    33  //
    34  // It returns ctx with the new trace span added and a serialization of the
    35  // SpanContext added to the outgoing gRPC metadata.
    36  func (c *ClientHandler) traceTagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
    37  	name := strings.TrimPrefix(rti.FullMethodName, "/")
    38  	name = strings.Replace(name, "/", ".", -1)
    39  	ctx, span := trace.StartSpan(ctx, name,
    40  		trace.WithSampler(c.StartOptions.Sampler),
    41  		trace.WithSpanKind(trace.SpanKindClient)) // span is ended by traceHandleRPC
    42  	traceContextBinary := propagation.Binary(span.SpanContext())
    43  	return metadata.AppendToOutgoingContext(ctx, traceContextKey, string(traceContextBinary))
    44  }
    45  
    46  // TagRPC creates a new trace span for the server side of the RPC.
    47  //
    48  // It checks the incoming gRPC metadata in ctx for a SpanContext, and if
    49  // it finds one, uses that SpanContext as the parent context of the new span.
    50  //
    51  // It returns ctx, with the new trace span added.
    52  func (s *ServerHandler) traceTagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context {
    53  	md, _ := metadata.FromIncomingContext(ctx)
    54  	name := strings.TrimPrefix(rti.FullMethodName, "/")
    55  	name = strings.Replace(name, "/", ".", -1)
    56  	traceContext := md[traceContextKey]
    57  	var (
    58  		parent     trace.SpanContext
    59  		haveParent bool
    60  	)
    61  	if len(traceContext) > 0 {
    62  		// Metadata with keys ending in -bin are actually binary. They are base64
    63  		// encoded before being put on the wire, see:
    64  		// https://github.com/grpc/grpc-go/blob/08d6261/Documentation/grpc-metadata.md#storing-binary-data-in-metadata
    65  		traceContextBinary := []byte(traceContext[0])
    66  		parent, haveParent = propagation.FromBinary(traceContextBinary)
    67  		if haveParent && !s.IsPublicEndpoint {
    68  			ctx, _ := trace.StartSpanWithRemoteParent(ctx, name, parent,
    69  				trace.WithSpanKind(trace.SpanKindServer),
    70  				trace.WithSampler(s.StartOptions.Sampler),
    71  			)
    72  			return ctx
    73  		}
    74  	}
    75  	ctx, span := trace.StartSpan(ctx, name,
    76  		trace.WithSpanKind(trace.SpanKindServer),
    77  		trace.WithSampler(s.StartOptions.Sampler))
    78  	if haveParent {
    79  		span.AddLink(trace.Link{TraceID: parent.TraceID, SpanID: parent.SpanID, Type: trace.LinkTypeChild})
    80  	}
    81  	return ctx
    82  }
    83  
    84  func traceHandleRPC(ctx context.Context, rs stats.RPCStats) {
    85  	span := trace.FromContext(ctx)
    86  	// TODO: compressed and uncompressed sizes are not populated in every message.
    87  	switch rs := rs.(type) {
    88  	case *stats.Begin:
    89  		span.AddAttributes(
    90  			trace.BoolAttribute("Client", rs.Client),
    91  			trace.BoolAttribute("FailFast", rs.FailFast))
    92  	case *stats.InPayload:
    93  		span.AddMessageReceiveEvent(0 /* TODO: messageID */, int64(rs.Length), int64(rs.WireLength))
    94  	case *stats.OutPayload:
    95  		span.AddMessageSendEvent(0, int64(rs.Length), int64(rs.WireLength))
    96  	case *stats.End:
    97  		if rs.Error != nil {
    98  			s, ok := status.FromError(rs.Error)
    99  			if ok {
   100  				span.SetStatus(trace.Status{Code: int32(s.Code()), Message: s.Message()})
   101  			} else {
   102  				span.SetStatus(trace.Status{Code: int32(codes.Internal), Message: rs.Error.Error()})
   103  			}
   104  		}
   105  		span.End()
   106  	}
   107  }
   108  

View as plain text