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 stackdriver

import (
	
	
	
	
	

	timestamppb 
	wrapperspb 
	
	
	monitoredrespb 
	tracepb 
	statuspb 
)

const (
	maxAnnotationEventsPerSpan = 32
	maxMessageEventsPerSpan    = 128
	maxAttributeStringValue    = 256
	agentLabel                 = "g.co/agent"

	labelHTTPHost       = `/http/host`
	labelHTTPMethod     = `/http/method`
	labelHTTPStatusCode = `/http/status_code`
	labelHTTPPath       = `/http/path`
	labelHTTPUserAgent  = `/http/user_agent`
)
proto returns a protocol buffer representation of a SpanData.
func ( *trace.SpanData,  string,  *monitoredrespb.MonitoredResource,  string) *tracepb.Span {
	if  == nil {
		return nil
	}

	 := .SpanContext.TraceID.String()
	 := .SpanContext.SpanID.String()

	 := .Name
	switch .SpanKind {
	case trace.SpanKindClient:
		 = "Sent." + 
	case trace.SpanKindServer:
		 = "Recv." + 
	}

	 := &tracepb.Span{
		Name:                    "projects/" +  + "/traces/" +  + "/spans/" + ,
		SpanId:                  ,
		DisplayName:             trunc(, 128),
		StartTime:               timestampProto(.StartTime),
		EndTime:                 timestampProto(.EndTime),
		SameProcessAsParentSpan: &wrapperspb.BoolValue{Value: !.HasRemoteParent},
	}
	if  := .ParentSpanID;  != (trace.SpanID{}) {
		.ParentSpanId = .String()
	}
	if .Status.Code != 0 || .Status.Message != "" {
		.Status = &statuspb.Status{Code: .Status.Code, Message: .Status.Message}
	}

	var , , ,  int
	copyAttributes(&.Attributes, .Attributes)
Copy MonitoredResources as span Attributes
Only set the agent label if it is not already set. That enables the OpenCensus agent/collector to set the agent label based on the library that sent the span to the agent. We now provide a config option to set the userAgent explicitly, which is used both here and in request headers when sending metric data, but have retained this non-override functionality for backwards compatibility.
	if ,  := .Attributes.AttributeMap[agentLabel]; ! {
		.Attributes.AttributeMap[agentLabel] = &tracepb.AttributeValue{
			Value: &tracepb.AttributeValue_StringValue{
				StringValue: trunc(, maxAttributeStringValue),
			},
		}
	}

	 := .MessageEvents
	for ,  := range  {
		if  >= maxMessageEventsPerSpan {
			 = len() - 
			break
		}
		++
		if .TimeEvents == nil {
			.TimeEvents = &tracepb.Span_TimeEvents{}
		}
		.TimeEvents.TimeEvent = append(.TimeEvents.TimeEvent, &tracepb.Span_TimeEvent{
			Time: timestampProto(.Time),
			Value: &tracepb.Span_TimeEvent_MessageEvent_{
				MessageEvent: &tracepb.Span_TimeEvent_MessageEvent{
					Type:                  tracepb.Span_TimeEvent_MessageEvent_Type(.EventType),
					Id:                    .MessageID,
					UncompressedSizeBytes: .UncompressedByteSize,
					CompressedSizeBytes:   .CompressedByteSize,
				},
			},
		})
	}

	if  != 0 ||  != 0 {
		if .TimeEvents == nil {
			.TimeEvents = &tracepb.Span_TimeEvents{}
		}
		.TimeEvents.DroppedAnnotationsCount = clip32()
		.TimeEvents.DroppedMessageEventsCount = clip32()
	}

	if len(.Links) > 0 {
		.Links = &tracepb.Span_Links{}
		.Links.Link = make([]*tracepb.Span_Link, 0, len(.Links))
		for ,  := range .Links {
			 := &tracepb.Span_Link{
				TraceId: .TraceID.String(),
				SpanId:  .SpanID.String(),
				Type:    tracepb.Span_Link_Type(.Type),
			}
			copyAttributes(&.Attributes, .Attributes)
			.Links.Link = append(.Links.Link, )
		}
	}
	return 
}
timestampProto creates a timestamp proto for a time.Time.
copyMonitoredResourceAttributes copies proto monitoredResource to proto map field (Span_Attributes) it creates the map if it is nil.
func ( *tracepb.Span_Attributes,  *monitoredrespb.MonitoredResource) *tracepb.Span_Attributes {
	if  == nil {
		return 
	}
	if  == nil {
		 = &tracepb.Span_Attributes{}
	}
	if .AttributeMap == nil {
		.AttributeMap = make(map[string]*tracepb.AttributeValue)
	}
	for ,  := range .Labels {
		 := attributeValue()
		.AttributeMap[fmt.Sprintf("g.co/r/%s/%s", .Type, )] = 
	}
	return 
}
copyAttributes copies a map of attributes to a proto map field. It creates the map if it is nil.
func ( **tracepb.Span_Attributes,  map[string]interface{}) {
	if len() == 0 {
		return
	}
	if * == nil {
		* = &tracepb.Span_Attributes{}
	}
	if (*).AttributeMap == nil {
		(*).AttributeMap = make(map[string]*tracepb.AttributeValue)
	}
	var  int32
	for ,  := range  {
		 := attributeValue()
		if  == nil {
			continue
		}
		switch  {
		case ochttp.PathAttribute:
			(*).AttributeMap[labelHTTPPath] = 
		case ochttp.HostAttribute:
			(*).AttributeMap[labelHTTPHost] = 
		case ochttp.MethodAttribute:
			(*).AttributeMap[labelHTTPMethod] = 
		case ochttp.UserAgentAttribute:
			(*).AttributeMap[labelHTTPUserAgent] = 
		case ochttp.StatusCodeAttribute:
			(*).AttributeMap[labelHTTPStatusCode] = 
		default:
			if len() > 128 {
				++
				continue
			}
			(*).AttributeMap[] = 
		}
	}
	(*).DroppedAttributesCount = 
}

func ( interface{}) *tracepb.AttributeValue {
	switch value := .(type) {
	case bool:
		return &tracepb.AttributeValue{
			Value: &tracepb.AttributeValue_BoolValue{BoolValue: },
		}
	case int64:
		return &tracepb.AttributeValue{
			Value: &tracepb.AttributeValue_IntValue{IntValue: },
		}
TODO: set double value if Stackdriver Trace support it in the future.
trunc returns a TruncatableString truncated to the given limit.
func ( string,  int) *tracepb.TruncatableString {
	if len() >  {
		 := []byte([:])
		for {
			,  := utf8.DecodeLastRune()
			if  == utf8.RuneError &&  == 1 {
				 = [:len()-1]
			} else {
				break
			}
		}
		return &tracepb.TruncatableString{
			Value:              string(),
			TruncatedByteCount: clip32(len() - len()),
		}
	}
	return &tracepb.TruncatableString{
		Value:              ,
		TruncatedByteCount: 0,
	}
}
clip32 clips an int to the range of an int32.
func ( int) int32 {
	if  < math.MinInt32 {
		return math.MinInt32
	}
	if  > math.MaxInt32 {
		return math.MaxInt32
	}
	return int32()