Copyright 2017, OpenCensus Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package ocgrpc

import (
	
	

	
	
	
	

	
	
)

const traceContextKey = "grpc-trace-bin"
TagRPC creates a new trace span for the client side of the RPC. It returns ctx with the new trace span added and a serialization of the SpanContext added to the outgoing gRPC metadata.
func ( *ClientHandler) ( context.Context,  *stats.RPCTagInfo) context.Context {
	 := strings.TrimPrefix(.FullMethodName, "/")
	 = strings.Replace(, "/", ".", -1)
	,  := trace.StartSpan(, ,
		trace.WithSampler(.StartOptions.Sampler),
		trace.WithSpanKind(trace.SpanKindClient)) // span is ended by traceHandleRPC
	 := propagation.Binary(.SpanContext())
	return metadata.AppendToOutgoingContext(, traceContextKey, string())
}
TagRPC creates a new trace span for the server side of the RPC. It checks the incoming gRPC metadata in ctx for a SpanContext, and if it finds one, uses that SpanContext as the parent context of the new span. It returns ctx, with the new trace span added.
func ( *ServerHandler) ( context.Context,  *stats.RPCTagInfo) context.Context {
	,  := metadata.FromIncomingContext()
	 := strings.TrimPrefix(.FullMethodName, "/")
	 = strings.Replace(, "/", ".", -1)
	 := [traceContextKey]
	var (
		     trace.SpanContext
		 bool
	)
Metadata with keys ending in -bin are actually binary. They are base64 encoded before being put on the wire, see: https://github.com/grpc/grpc-go/blob/08d6261/Documentation/grpc-metadata.md#storing-binary-data-in-metadata
		 := []byte([0])
		,  = propagation.FromBinary()
		if  && !.IsPublicEndpoint {
			,  := trace.StartSpanWithRemoteParent(, , ,
				trace.WithSpanKind(trace.SpanKindServer),
				trace.WithSampler(.StartOptions.Sampler),
			)
			return 
		}
	}
	,  := trace.StartSpan(, ,
		trace.WithSpanKind(trace.SpanKindServer),
		trace.WithSampler(.StartOptions.Sampler))
	if  {
		.AddLink(trace.Link{TraceID: .TraceID, SpanID: .SpanID, Type: trace.LinkTypeChild})
	}
	return 
}

func ( context.Context,  stats.RPCStats) {
TODO: compressed and uncompressed sizes are not populated in every message.
	switch rs := .(type) {
	case *stats.Begin:
		.AddAttributes(
			trace.BoolAttribute("Client", .Client),
			trace.BoolAttribute("FailFast", .FailFast))
	case *stats.InPayload:
		.AddMessageReceiveEvent(0 /* TODO: messageID */, int64(.Length), int64(.WireLength))
	case *stats.OutPayload:
		.AddMessageSendEvent(0, int64(.Length), int64(.WireLength))
	case *stats.End:
		if .Error != nil {
			,  := status.FromError(.Error)
			if  {
				.SetStatus(trace.Status{Code: int32(.Code()), Message: .Message()})
			} else {
				.SetStatus(trace.Status{Code: int32(codes.Internal), Message: .Error.Error()})
			}
		}
		.End()
	}