Skip to content

Commit

Permalink
Patch transformation logic
Browse files Browse the repository at this point in the history
Signed-off-by: Yuri Shkuro <github@ysh.us>
  • Loading branch information
yurishkuro committed Dec 7, 2023
1 parent e18eb5d commit 96ccb81
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
29 changes: 28 additions & 1 deletion pkg/translator/jaeger/jaegerthrift_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,41 @@ func jThriftSpansToInternal(spans []*jaeger.Span, dest ptrace.SpanSlice) {
}
}

// jThriftSpanParentID infers the parent span ID for a given span.
// Based on https://github.com/jaegertracing/jaeger/blob/8c61b6561f9057a199c1504606d8e68319ee7b31/model/span.go#L143
func jThriftSpanParentID(span *jaeger.Span) int64 {
if span.ParentSpanId != 0 {
return span.ParentSpanId
}
// If span.ParentSpanId undefined but there are references to the same trace,
// they can also be considered a parent, with CHILD_OF being higher priority.
var ffRef *jaeger.SpanRef
for _, ref := range span.References {
// must be from the same trace
if ref.TraceIdHigh != span.TraceIdHigh || ref.TraceIdLow != span.TraceIdLow {
continue
}
if ref.RefType == jaeger.SpanRefType_CHILD_OF {
return ref.SpanId
}
if ffRef == nil && ref.RefType == jaeger.SpanRefType_FOLLOWS_FROM {
ffRef = ref
}
}
if ffRef != nil {
return ffRef.SpanId
}
return 0
}

func jThriftSpanToInternal(span *jaeger.Span, dest ptrace.Span) {
dest.SetTraceID(idutils.UInt64ToTraceID(uint64(span.TraceIdHigh), uint64(span.TraceIdLow)))
dest.SetSpanID(idutils.UInt64ToSpanID(uint64(span.SpanId)))
dest.SetName(span.OperationName)
dest.SetStartTimestamp(microsecondsToUnixNano(span.StartTime))
dest.SetEndTimestamp(microsecondsToUnixNano(span.StartTime + span.Duration))

parentSpanID := span.ParentSpanId
parentSpanID := jThriftSpanParentID(span)
if parentSpanID != 0 {
dest.SetParentSpanID(idutils.UInt64ToSpanID(uint64(parentSpanID)))
}
Expand Down
30 changes: 19 additions & 11 deletions pkg/translator/jaeger/traces_to_jaegerproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func attributeToJaegerProtoTag(key string, attr pcommon.Value) model.KeyValue {

func spanToJaegerProto(span ptrace.Span, libraryTags pcommon.InstrumentationScope) *model.Span {
traceID := traceIDToJaegerProto(span.TraceID())
jReferences := makeJaegerProtoReferences(span.Links(), span.ParentSpanID(), traceID)
jReferences := makeJaegerProtoReferences(span.Links(), spanIDToJaegerProto(span.ParentSpanID()), traceID)

startTime := span.StartTimestamp().AsTime()
return &model.Span{
Expand Down Expand Up @@ -237,32 +237,40 @@ func spanIDToJaegerProto(spanID pcommon.SpanID) model.SpanID {
return model.SpanID(idutils.SpanIDToUInt64(spanID))
}

// makeJaegerProtoReferences constructs jaeger span references based on parent span ID and span links
func makeJaegerProtoReferences(links ptrace.SpanLinkSlice, parentSpanID pcommon.SpanID, traceID model.TraceID) []model.SpanRef {
parentSpanIDSet := !parentSpanID.IsEmpty()
if !parentSpanIDSet && links.Len() == 0 {
return nil
}

// makeJaegerProtoReferences constructs jaeger span references based on parent span ID and span links.
// The parent span ID is used to add a CHILD_OF reference, _unless_ it is referenced from one of the links.
func makeJaegerProtoReferences(links ptrace.SpanLinkSlice, parentSpanID model.SpanID, traceID model.TraceID) []model.SpanRef {
refsCount := links.Len()
if parentSpanIDSet {
if parentSpanID != 0 {
refsCount++
}

if refsCount == 0 {
return nil
}

refs := make([]model.SpanRef, 0, refsCount)

// Put parent span ID at the first place because usually backends look for it
// as the first CHILD_OF item in the model.SpanRef slice.
if parentSpanIDSet {
if parentSpanID != 0 {
refs = append(refs, model.SpanRef{
TraceID: traceID,
SpanID: spanIDToJaegerProto(parentSpanID),
SpanID: parentSpanID,
RefType: model.SpanRefType_CHILD_OF,
})
}

for i := 0; i < links.Len(); i++ {
link := links.At(i)
linkTraceID := traceIDToJaegerProto(link.TraceID())
linkSpanID := spanIDToJaegerProto(link.SpanID())
linkRefType := refTypeFromLink(link)
if parentSpanID != 0 && linkTraceID == traceID && linkSpanID == parentSpanID {
// We already added a reference to this span, but maybe with the wrong type, so override.
refs[0].RefType = linkRefType
continue
}
refs = append(refs, model.SpanRef{
TraceID: traceIDToJaegerProto(link.TraceID()),
SpanID: spanIDToJaegerProto(link.SpanID()),
Expand Down
2 changes: 1 addition & 1 deletion pkg/translator/jaeger/traces_to_jaegerproto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func TestInternalTracesToJaegerProto(t *testing.T) {
},
{
name: "a-spans-with-two-parent",
td: generateTracesSpanWithTwoParents(),
jb: &model.Batch{
Process: &model.Process{
ServiceName: tracetranslator.ResourceNoServiceName,
Expand All @@ -324,7 +325,6 @@ func TestInternalTracesToJaegerProto(t *testing.T) {
generateProtoTwoParentsSpan(),
},
},
td: generateTracesSpanWithTwoParents(),
},
}

Expand Down

0 comments on commit 96ccb81

Please sign in to comment.