Skip to content

Commit df581cd

Browse files
author
Yuri Shkuro
committed
Add adjuster that removes bad span references
Signed-off-by: Yuri Shkuro <ys@uber.com>
1 parent 8d6d07a commit df581cd

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

cmd/query/app/adjusters.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ var StandardAdjusters = []adjuster.Adjuster{
2525
adjuster.ClockSkew(),
2626
adjuster.IPTagAdjuster(),
2727
adjuster.SortLogFields(),
28+
adjuster.SpanReferences(),
2829
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2017 Uber Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package adjuster
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/jaegertracing/jaeger/model"
21+
)
22+
23+
// SpanReferences creates am adjuster that removes invalid span references, e.g. with traceID==0
24+
func SpanReferences() Adjuster {
25+
return Func(func(trace *model.Trace) (*model.Trace, error) {
26+
adjuster := &spanReferenceAdjuster{}
27+
for _, span := range trace.Spans {
28+
adjuster.adjust(span)
29+
}
30+
return trace, nil
31+
})
32+
}
33+
34+
type spanReferenceAdjuster struct{}
35+
36+
func (s *spanReferenceAdjuster) adjust(span *model.Span) *model.Span {
37+
foundInvalid := false
38+
for i := range span.References {
39+
if !s.valid(&span.References[i]) {
40+
foundInvalid = true
41+
break
42+
}
43+
}
44+
if !foundInvalid {
45+
return span
46+
}
47+
references := make([]model.SpanRef, 0, len(span.References)-1)
48+
for i := range span.References {
49+
if !s.valid(&span.References[i]) {
50+
span.Warnings = append(span.Warnings, fmt.Sprintf("Invalid span reference removed %+v", span.References[i]))
51+
continue
52+
}
53+
references = append(references, span.References[i])
54+
}
55+
span.References = references
56+
return span
57+
}
58+
59+
func (s *spanReferenceAdjuster) valid(ref *model.SpanRef) bool {
60+
return ref.TraceID.High != 0 || ref.TraceID.Low != 0
61+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2017 Uber Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package adjuster
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
22+
"github.com/jaegertracing/jaeger/model"
23+
)
24+
25+
func TestSpanReferencesAdjuster(t *testing.T) {
26+
trace := &model.Trace{
27+
Spans: []*model.Span{
28+
&model.Span{},
29+
&model.Span{
30+
References: []model.SpanRef{},
31+
},
32+
&model.Span{
33+
References: []model.SpanRef{
34+
{TraceID: model.TraceID{High: 0, Low: 1}},
35+
{TraceID: model.TraceID{High: 1, Low: 0}},
36+
{TraceID: model.TraceID{High: 0, Low: 0}},
37+
},
38+
},
39+
},
40+
}
41+
trace, err := SpanReferences().Adjust(trace)
42+
assert.NoError(t, err)
43+
assert.Len(t, trace.Spans[0].References, 0)
44+
assert.Len(t, trace.Spans[1].References, 0)
45+
assert.Len(t, trace.Spans[2].References, 2)
46+
assert.Equal(t, []string{"Invalid span reference removed {RefType:child-of TraceID:0 SpanID:0}"}, trace.Spans[2].Warnings)
47+
}

0 commit comments

Comments
 (0)