/
span_sanitizer.go
191 lines (165 loc) · 5.54 KB
/
span_sanitizer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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 zipkin
import (
"strconv"
"strings"
zc "github.com/jaegertracing/jaeger/thrift-gen/zipkincore"
)
const (
negativeDurationTag = "errNegativeDuration"
zeroParentIDTag = "errZeroParentID"
)
var (
defaultDuration = int64(1)
// StandardSanitizers is a list of standard zipkin sanitizers.
StandardSanitizers = []Sanitizer{NewSpanStartTimeSanitizer(), NewSpanDurationSanitizer(), NewParentIDSanitizer(), NewErrorTagSanitizer()}
)
// Sanitizer interface for sanitizing spans. Any business logic that needs to be applied to normalize the contents of a
// span should implement this interface.
// TODO - just make this a function
type Sanitizer interface {
Sanitize(span *zc.Span) *zc.Span
}
// ChainedSanitizer applies multiple sanitizers in serial fashion
type ChainedSanitizer []Sanitizer
// NewChainedSanitizer creates a Sanitizer from the variadic list of passed Sanitizers
func NewChainedSanitizer(sanitizers ...Sanitizer) ChainedSanitizer {
return sanitizers
}
// Sanitize calls each Sanitize, returning the first error
func (cs ChainedSanitizer) Sanitize(span *zc.Span) *zc.Span {
for _, s := range cs {
span = s.Sanitize(span)
}
return span
}
// NewSpanDurationSanitizer returns a sanitizer that deals with nil or 0 span duration.
func NewSpanDurationSanitizer() Sanitizer {
return &spanDurationSanitizer{}
}
type spanDurationSanitizer struct {
}
func (s *spanDurationSanitizer) Sanitize(span *zc.Span) *zc.Span {
if span.Duration == nil {
duration := defaultDuration
if len(span.Annotations) >= 2 {
// Prefer RPC one-way (cs -> sr) vs arbitrary annotations.
first := span.Annotations[0].Timestamp
last := span.Annotations[len(span.Annotations)-1].Timestamp
for _, anno := range span.Annotations {
if anno.Value == zc.CLIENT_SEND {
first = anno.Timestamp
} else if anno.Value == zc.CLIENT_RECV {
last = anno.Timestamp
}
}
if first < last {
duration = last - first
if span.Timestamp == nil {
span.Timestamp = &first
}
}
}
span.Duration = &duration
return span
}
duration := *span.Duration
if duration >= 0 {
return span
}
span.Duration = &defaultDuration
annotation := zc.BinaryAnnotation{
Key: negativeDurationTag,
Value: []byte(strconv.FormatInt(duration, 10)),
AnnotationType: zc.AnnotationType_STRING,
}
span.BinaryAnnotations = append(span.BinaryAnnotations, &annotation)
return span
}
// NewSpanStartTimeSanitizer returns a Sanitizer that changes span start time if is nil
// If there is zipkincore.CLIENT_SEND use that, if no fall back on zipkincore.SERVER_RECV
func NewSpanStartTimeSanitizer() Sanitizer {
return &spanStartTimeSanitizer{}
}
type spanStartTimeSanitizer struct {
}
func (s *spanStartTimeSanitizer) Sanitize(span *zc.Span) *zc.Span {
if span.Timestamp != nil || len(span.Annotations) == 0 {
return span
}
for _, anno := range span.Annotations {
if anno.Value == zc.CLIENT_SEND {
span.Timestamp = &anno.Timestamp
return span
}
if anno.Value == zc.SERVER_RECV && span.ParentID == nil {
// continue, cs has higher precedence and might be after
span.Timestamp = &anno.Timestamp
}
}
return span
}
// NewParentIDSanitizer returns a sanitizer that deals parentID == 0
// by replacing with nil, per Zipkin convention.
func NewParentIDSanitizer() Sanitizer {
return &parentIDSanitizer{}
}
type parentIDSanitizer struct {
}
func (s *parentIDSanitizer) Sanitize(span *zc.Span) *zc.Span {
if span.ParentID == nil || *span.ParentID != 0 {
return span
}
annotation := zc.BinaryAnnotation{
Key: zeroParentIDTag,
Value: []byte("0"),
AnnotationType: zc.AnnotationType_STRING,
}
span.BinaryAnnotations = append(span.BinaryAnnotations, &annotation)
span.ParentID = nil
return span
}
// NewErrorTagSanitizer returns a sanitizer that changes error binary annotations to boolean type
// and sets appropriate value, in case value was a string message it adds a 'error.message' binary annotation with
// this message.
func NewErrorTagSanitizer() Sanitizer {
return &errorTagSanitizer{}
}
type errorTagSanitizer struct {
}
func (s *errorTagSanitizer) Sanitize(span *zc.Span) *zc.Span {
for _, binAnno := range span.BinaryAnnotations {
if binAnno.AnnotationType != zc.AnnotationType_BOOL && strings.EqualFold("error", binAnno.Key) {
binAnno.AnnotationType = zc.AnnotationType_BOOL
switch {
case len(binAnno.Value) == 0 || strings.EqualFold("true", string(binAnno.Value)):
binAnno.Value = []byte{1}
case strings.EqualFold("false", string(binAnno.Value)):
binAnno.Value = []byte{0}
default:
// value is different to true/false, create another bin annotation with error message
annoErrorMsg := &zc.BinaryAnnotation{
Key: "error.message",
Value: binAnno.Value,
AnnotationType: zc.AnnotationType_STRING,
}
span.BinaryAnnotations = append(span.BinaryAnnotations, annoErrorMsg)
binAnno.Value = []byte{1}
}
}
}
return span
}