Skip to content

Commit

Permalink
Introduce SortReferences adjuster
Browse files Browse the repository at this point in the history
As demonstrated in https://github.com/jaegertracing/jaeger-ui/issues/966,
jaeger-ui expects the first reference to be of `CHILD_OF` type to properly
render the tree. This new adjuster achieves exactly that.
  • Loading branch information
bobrik committed Jun 28, 2022
1 parent b8be460 commit 215cce0
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/query/app/querysvc/adjusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ func StandardAdjusters(maxClockSkewAdjust time.Duration) []adjuster.Adjuster {
adjuster.IPTagAdjuster(),
adjuster.SortLogFields(),
adjuster.SpanReferences(),
adjuster.SortReferences(),
}
}
38 changes: 38 additions & 0 deletions model/adjuster/sort_references.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// 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 adjuster

import (
"sort"

"github.com/jaegertracing/jaeger/model"
)

// SortReferences returns an Adjuster that puts CHILD_OF references first.
// This is necessary to match jaeger-ui expectations:
// * https://github.com/jaegertracing/jaeger-ui/issues/966
func SortReferences() Adjuster {
return Func(func(trace *model.Trace) (*model.Trace, error) {
for _, span := range trace.Spans {
sort.SliceStable(span.References, func(i, j int) bool {
iType := span.References[i].GetRefType()
jType := span.References[j].GetRefType()
return iType == model.SpanRefType_CHILD_OF && jType == model.SpanRefType_FOLLOWS_FROM
})
}
return trace, nil
})
}
90 changes: 90 additions & 0 deletions model/adjuster/sort_references_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// 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 adjuster

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/jaegertracing/jaeger/model"
)

func TestSortReferences(t *testing.T) {
testCases := []struct {
incoming []model.SpanRefType
expected []model.SpanRefType
}{
{
incoming: []model.SpanRefType{},
expected: []model.SpanRefType{},
},
{
incoming: []model.SpanRefType{model.ChildOf},
expected: []model.SpanRefType{model.ChildOf},
},
{
incoming: []model.SpanRefType{model.FollowsFrom},
expected: []model.SpanRefType{model.FollowsFrom},
},
{
incoming: []model.SpanRefType{model.ChildOf, model.FollowsFrom},
expected: []model.SpanRefType{model.ChildOf, model.FollowsFrom},
},
{
incoming: []model.SpanRefType{model.FollowsFrom, model.ChildOf},
expected: []model.SpanRefType{model.ChildOf, model.FollowsFrom},
},
{
incoming: []model.SpanRefType{model.FollowsFrom, model.ChildOf, model.FollowsFrom},
expected: []model.SpanRefType{model.ChildOf, model.FollowsFrom, model.FollowsFrom},
},
}
for _, testCase := range testCases {
trace := &model.Trace{
Spans: []*model.Span{
{
References: makeReferences(testCase.incoming),
},
},
}
trace, err := SortReferences().Adjust(trace)
assert.NoError(t, err)
assert.Equal(t, testCase.expected, extractReferenceTypes(trace.Spans[0].References))
}
}

func makeReferences(types []model.SpanRefType) []model.SpanRef {
references := make([]model.SpanRef, len(types))

for i, refType := range types {
references[i] = model.SpanRef{
RefType: refType,
}
}

return references
}

func extractReferenceTypes(references []model.SpanRef) []model.SpanRefType {
types := make([]model.SpanRefType, len(references))

for i, reference := range references {
types[i] = reference.RefType
}

return types
}

0 comments on commit 215cce0

Please sign in to comment.