Skip to content

Commit

Permalink
quick work around for open-telemetry#975
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-smith-zocdoc committed May 15, 2020
1 parent b35f488 commit 3425a19
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
50 changes: 30 additions & 20 deletions receiver/zipkinreceiver/trace_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ func zipkinSpanToTraceSpan(zs *zipkinmodel.SpanModel) (*tracepb.Span, *commonpb.
parentSpanID = tracetranslator.UInt64ToByteSpanID(uint64(*zs.ParentID))
}

var attributes, status = zipkinTagsToTraceAttributes(zs.Tags, zs.Kind)
if status == nil {
status = extractProtoStatus(zs)
}

pbs := &tracepb.Span{
TraceId: traceID,
SpanId: tracetranslator.UInt64ToByteSpanID(uint64(zs.ID)),
Expand All @@ -384,8 +389,8 @@ func zipkinSpanToTraceSpan(zs *zipkinmodel.SpanModel) (*tracepb.Span, *commonpb.
StartTime: internal.TimeToTimestamp(zs.Timestamp),
EndTime: internal.TimeToTimestamp(zs.Timestamp.Add(zs.Duration)),
Kind: zipkinSpanKindToProtoSpanKind(zs.Kind),
Status: extractProtoStatus(zs),
Attributes: zipkinTagsToTraceAttributes(zs.Tags, zs.Kind),
Status: status,
Attributes: attributes,
TimeEvents: zipkinAnnotationsToProtoTimeEvents(zs.Annotations),
}

Expand Down Expand Up @@ -573,7 +578,8 @@ func zipkinAnnotationToProtoAnnotation(zas zipkinmodel.Annotation) *tracepb.Span
}
}

func zipkinTagsToTraceAttributes(tags map[string]string, skind zipkinmodel.Kind) *tracepb.Span_Attributes {

func zipkinTagsToTraceAttributes(tags map[string]string, skind zipkinmodel.Kind) (*tracepb.Span_Attributes, *tracepb.Status) {
// Produce and Consumer span kinds are not representable in OpenCensus format.
// We will represent them using TagSpanKind attribute, according to OpenTracing
// conventions. Check if it is one of those span kinds.
Expand All @@ -587,29 +593,33 @@ func zipkinTagsToTraceAttributes(tags map[string]string, skind zipkinmodel.Kind)

if len(tags) == 0 && spanKindTagVal == "" {
// No input tags and no need to add a span kind tag. Keep attributes map empty.
return nil
return nil, nil
}


sMapper := &zipkin.StatusMapper{}
amap := make(map[string]*tracepb.AttributeValue, len(tags))
for key, value := range tags {
// We did a translation from "boolean" to "string"
// in OpenCensus-Go's Zipkin exporter as per
// https://github.com/census-instrumentation/opencensus-go/blob/1eb9a13c7dd02141e065a665f6bf5c99a090a16a/exporter/zipkin/zipkin.go#L138-L155
switch value {
case "true", "false":
amap[key] = &tracepb.AttributeValue{
Value: &tracepb.AttributeValue_BoolValue{BoolValue: value == "true"},
}
default:
amap[key] = &tracepb.AttributeValue{
Value: &tracepb.AttributeValue_StringValue{
StringValue: &tracepb.TruncatableString{Value: value},
},
}
}

pbAttrib := &tracepb.AttributeValue{}
if iValue, err := strconv.ParseInt(value, 10, 64); err == nil {
pbAttrib.Value = &tracepb.AttributeValue_IntValue{IntValue: iValue}
} else if bValue, err := strconv.ParseBool(value); err == nil {
pbAttrib.Value = &tracepb.AttributeValue_BoolValue{BoolValue: bValue}
} else {
// For now all else go to string
pbAttrib.Value = &tracepb.AttributeValue_StringValue{StringValue: &tracepb.TruncatableString{Value: value}}
}

if drop := sMapper.FromAttribute(key, pbAttrib); drop {
continue
}

amap[key] = pbAttrib
}

var status = sMapper.OcStatus()

if spanKindTagVal != "" {
// Set the previously translated span kind attribute (see top of this function).
// We do this after the "tags" map is translated so that we will overwrite
Expand All @@ -621,7 +631,7 @@ func zipkinTagsToTraceAttributes(tags map[string]string, skind zipkinmodel.Kind)
}
}

return &tracepb.Span_Attributes{AttributeMap: amap}
return &tracepb.Span_Attributes{AttributeMap: amap}, status
}

func transportType(r *http.Request) string {
Expand Down
6 changes: 3 additions & 3 deletions translator/trace/zipkin/status_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type status struct {
}

// statusMapper contains codes translated from different sources to OC status codes
type statusMapper struct {
type StatusMapper struct {
// oc status code extracted from "status.code" tags
fromStatus status
// oc status code extracted from "census.status_code" tags
Expand All @@ -42,7 +42,7 @@ type statusMapper struct {
// then fallback on code extracted from "status.code" tags
// and finally fallback on code extracted and translated from "http.status_code"
// ocStatus must be called after all tags/attributes are processed with the `fromAttribute` method.
func (m *statusMapper) ocStatus() *tracepb.Status {
func (m *StatusMapper) OcStatus() *tracepb.Status {
var s status
switch {
case m.fromCensus.codePtr != nil:
Expand All @@ -66,7 +66,7 @@ func (m *statusMapper) ocStatus() *tracepb.Status {
return nil
}

func (m *statusMapper) fromAttribute(key string, attrib *tracepb.AttributeValue) bool {
func (m *StatusMapper) FromAttribute(key string, attrib *tracepb.AttributeValue) bool {
switch key {
case tracetranslator.TagZipkinCensusCode:
m.fromCensus.codePtr = attribToStatusCode(attrib)
Expand Down
6 changes: 3 additions & 3 deletions translator/trace/zipkin/zipkinv1_thrift_to_protospan.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func zipkinV1ThriftBinAnnotationsToOCAttributes(ztBinAnnotations []*zipkincore.B
return nil, nil, ""
}

sMapper := &statusMapper{}
sMapper := &StatusMapper{}
var localComponent string
attributeMap := make(map[string]*tracepb.AttributeValue)
for _, binaryAnnotation := range ztBinAnnotations {
Expand Down Expand Up @@ -203,14 +203,14 @@ func zipkinV1ThriftBinAnnotationsToOCAttributes(ztBinAnnotations []*zipkincore.B
localComponent = string(binaryAnnotation.Value)
}

if drop := sMapper.fromAttribute(key, pbAttrib); drop {
if drop := sMapper.FromAttribute(key, pbAttrib); drop {
continue
}

attributeMap[key] = pbAttrib
}

status = sMapper.ocStatus()
status = sMapper.OcStatus()

if len(attributeMap) == 0 {
return nil, status, ""
Expand Down
6 changes: 3 additions & 3 deletions translator/trace/zipkin/zipkinv1_to_protospan.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func zipkinV1BinAnnotationsToOCAttributes(binAnnotations []*binaryAnnotation) (a
return nil, nil, ""
}

sMapper := &statusMapper{}
sMapper := &StatusMapper{}
var localComponent string
attributeMap := make(map[string]*tracepb.AttributeValue)
for _, binAnnotation := range binAnnotations {
Expand All @@ -230,14 +230,14 @@ func zipkinV1BinAnnotationsToOCAttributes(binAnnotations []*binaryAnnotation) (a
localComponent = binAnnotation.Value
}

if drop := sMapper.fromAttribute(key, pbAttrib); drop {
if drop := sMapper.FromAttribute(key, pbAttrib); drop {
continue
}

attributeMap[key] = pbAttrib
}

status = sMapper.ocStatus()
status = sMapper.OcStatus()

if len(attributeMap) == 0 {
return nil, status, ""
Expand Down

0 comments on commit 3425a19

Please sign in to comment.