This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
normalizer.go
228 lines (193 loc) · 6.2 KB
/
normalizer.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package model
import (
"errors"
"fmt"
"strconv"
"time"
log "github.com/cihub/seelog"
)
const (
// MaxServiceLen the maximum length a service can have
MaxServiceLen = 100
// MaxNameLen the maximum length a name can have
MaxNameLen = 100
// MaxTypeLen the maximum length a span type can have
MaxTypeLen = 100
// MaxEndDateOffset the maximum amount of time in the future we
// tolerate for span end dates
MaxEndDateOffset = 10 * time.Minute
)
var (
// Year2000NanosecTS is an arbitrary cutoff to spot weird-looking values
Year2000NanosecTS = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC).UnixNano()
)
// Normalize makes sure a Span is properly initialized and encloses the minimum required info
func (s *Span) Normalize() error {
// Service
if s.Service == "" {
return errors.New("span.normalize: empty `Service`")
}
if len(s.Service) > MaxServiceLen {
return fmt.Errorf("span.normalize: `Service` too long (max %d chars): %s", MaxServiceLen, s.Service)
}
// service shall comply with Datadog tag normalization as it's eventually a tag
s.Service = NormalizeTag(s.Service)
if s.Service == "" {
return errors.New("span.normalize: `Service` could not be normalized")
}
// Name
if s.Name == "" {
return errors.New("span.normalize: empty `Name`")
}
if len(s.Name) > MaxNameLen {
return fmt.Errorf("span.normalize: `Name` too long (max %d chars): %s", MaxNameLen, s.Name)
}
// name shall comply with Datadog metric name normalization
var ok bool
s.Name, ok = normMetricNameParse(s.Name)
if !ok {
return fmt.Errorf("span.normalize: invalid `Name`: %s", s.Name)
}
// Resource
if s.Resource == "" {
return errors.New("span.normalize: empty `Resource`")
}
// TraceID & SpanID should be set in the client
// because they uniquely define the traces and associate them into traces
if s.TraceID == 0 {
return errors.New("span.normalize: empty `TraceID`")
}
if s.SpanID == 0 {
return errors.New("span.normalize: empty `SpanID`")
}
// ParentID, TraceID and SpanID set in the client could be the same
// Supporting the ParentID == TraceID == SpanID for the root span, is compliant
// with the Zipkin implementation. Furthermore, as described in the PR
// https://github.com/openzipkin/zipkin/pull/851 the constraint that the
// root span's ``trace id = span id`` has been removed
if s.ParentID == s.TraceID && s.ParentID == s.SpanID {
s.ParentID = 0
log.Debugf("span.normalize: `ParentID`, `TraceID` and `SpanID` are the same; `ParentID` set to 0: %d", s.TraceID)
}
// Start & Duration as nanoseconds timestamps
// if s.Start is very little, less than year 2000 probably a unit issue so discard
// (or it is "le bug de l'an 2000")
if s.Start < Year2000NanosecTS {
return fmt.Errorf("span.normalize: invalid `Start` (must be nanosecond epoch): %d", s.Start)
}
// If the end date is too far away in the future, it's probably a mistake.
if s.Start+s.Duration > time.Now().Add(MaxEndDateOffset).UnixNano() {
return fmt.Errorf("span.normalize: more than %v in the future", MaxEndDateOffset)
}
if s.Duration <= 0 {
return errors.New("span.normalize: durations need to be strictly positive")
}
// ParentID set on the client side, no way of checking
// Type
if len(s.Type) > MaxTypeLen {
return fmt.Errorf("span.normalize: `Type` too long (max %d chars): %s", MaxTypeLen, s.Type)
}
// Environment
if env, ok := s.Meta["env"]; ok {
s.Meta["env"] = NormalizeTag(env)
}
// Status Code
if sc, ok := s.Meta["http.status_code"]; ok {
if !isValidStatusCode(sc) {
delete(s.Meta, "http.status_code")
log.Debugf("Drop invalid meta `http.status_code`: %s", sc)
}
}
return nil
}
// NormalizeTrace takes a trace and
// * rejects the trace if there is a trace ID discrepancy between 2 spans
// * rejects the trace if two spans have the same span_id
// * rejects empty traces
// * rejects traces where at least one span cannot be normalized
// * return the normalized trace and an error:
// - nil if the trace can be accepted
// - an error string if the trace needs to be dropped
func NormalizeTrace(t Trace) (Trace, error) {
if len(t) == 0 {
return t, errors.New("empty trace")
}
spanIDs := make(map[uint64]struct{})
traceID := t[0].TraceID
for i, span := range t {
if _, ok := spanIDs[span.SpanID]; ok {
return t, fmt.Errorf("duplicate span id %v (span %v)",
span.SpanID, span)
}
if span.TraceID != traceID {
return t, fmt.Errorf("trace id mismatch %s:%x != %s:%x",
t[0].Name, t[0].TraceID, span.Name, span.TraceID)
}
if err := t[i].Normalize(); err != nil {
return t, fmt.Errorf("invalid span %v: %v", span, err)
}
spanIDs[span.SpanID] = struct{}{}
}
return t, nil
}
func isValidStatusCode(sc string) bool {
if code, err := strconv.ParseUint(sc, 10, 64); err == nil {
return 100 <= code && code < 600
}
return false
}
// This code is borrowed from dd-go metric normalization
// fast isAlpha for ascii
func isAlpha(b byte) bool {
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')
}
// fast isAlphaNumeric for ascii
func isAlphaNum(b byte) bool {
return isAlpha(b) || (b >= '0' && b <= '9')
}
// normMetricNameParse normalizes metric names with a parser instead of using
// garbage-creating string replacement routines.
func normMetricNameParse(name string) (string, bool) {
if name == "" || len(name) > MaxNameLen {
return name, false
}
var i, ptr int
res := make([]byte, 0, len(name))
// skip non-alphabetic characters
for ; i < len(name) && !isAlpha(name[i]); i++ {
}
// if there were no alphabetic characters it wasn't valid
if i == len(name) {
return "", false
}
for ; i < len(name); i++ {
switch {
case isAlphaNum(name[i]):
res = append(res, name[i])
ptr++
case name[i] == '.':
// we skipped all non-alpha chars up front so we have seen at least one
switch res[ptr-1] {
// overwrite underscores that happen before periods
case '_':
res[ptr-1] = '.'
default:
res = append(res, '.')
ptr++
}
default:
// we skipped all non-alpha chars up front so we have seen at least one
switch res[ptr-1] {
// no double underscores, no underscores after periods
case '.', '_':
default:
res = append(res, '_')
ptr++
}
}
}
if res[ptr-1] == '_' {
res = res[:ptr-1]
}
return string(res), true
}