Source File
trace_proto.go
Belonging Package
contrib.go.opencensus.io/exporter/stackdriver
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`
)
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)
.Attributes = copyMonitoredResourceAttributes(.Attributes, )
:= .Annotations
for , := range {
if >= maxAnnotationEventsPerSpan {
= len() -
break
}
:= &tracepb.Span_TimeEvent_Annotation{Description: trunc(.Message, maxAttributeStringValue)}
copyAttributes(&.Attributes, .Attributes)
:= &tracepb.Span_TimeEvent{
Time: timestampProto(.Time),
Value: &tracepb.Span_TimeEvent_Annotation_{Annotation: },
}
++
if .TimeEvents == nil {
.TimeEvents = &tracepb.Span_TimeEvents{}
}
.TimeEvents.TimeEvent = append(.TimeEvents.TimeEvent, )
}
if .Attributes == nil {
.Attributes = &tracepb.Span_Attributes{
AttributeMap: make(map[string]*tracepb.AttributeValue),
}
}
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
}
func ( time.Time) *timestamppb.Timestamp {
return ×tamppb.Timestamp{
Seconds: .Unix(),
Nanos: int32(.Nanosecond()),
}
}
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
}
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: },
}
return &tracepb.AttributeValue{
Value: &tracepb.AttributeValue_StringValue{
StringValue: trunc(strconv.FormatFloat(, 'f', -1, 64),
maxAttributeStringValue)},
}
case string:
return &tracepb.AttributeValue{
Value: &tracepb.AttributeValue_StringValue{StringValue: trunc(, maxAttributeStringValue)},
}
}
return nil
}
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,
}
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |