1 // Copyright 2017, OpenCensus Authors
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 "google.golang.org/grpc/codes"
22 "go.opencensus.io/trace"
23 "go.opencensus.io/trace/propagation"
24 "golang.org/x/net/context"
25 "google.golang.org/grpc/metadata"
26 "google.golang.org/grpc/stats"
27 "google.golang.org/grpc/status"
30 const traceContextKey = "grpc-trace-bin"
32 // TagRPC creates a new trace span for the client side of the RPC.
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))
46 // TagRPC creates a new trace span for the server side of the RPC.
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.
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]
58 parent trace.SpanContext
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),
75 ctx, span := trace.StartSpan(ctx, name,
76 trace.WithSpanKind(trace.SpanKindServer),
77 trace.WithSampler(s.StartOptions.Sampler))
79 span.AddLink(trace.Link{TraceID: parent.TraceID, SpanID: parent.SpanID, Type: trace.LinkTypeChild})
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) {
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))
98 s, ok := status.FromError(rs.Error)
100 span.SetStatus(trace.Status{Code: int32(s.Code()), Message: s.Message()})
102 span.SetStatus(trace.Status{Code: int32(codes.Internal), Message: rs.Error.Error()})