/
spanfilter.go
152 lines (133 loc) · 3.99 KB
/
spanfilter.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
package spanfilter
import (
"context"
"encoding/json"
goerrors "errors"
"strings"
"github.com/signalfx/golib/v3/errors"
)
// Map is the response we return from ingest wrt our span endpoint
// It contains the number of spans that were valid, and a map of string reason to spanIds for each invalid span
//
//nolint:errname
type Map struct {
Valid int `json:"valid"`
Invalid map[string][]string `json:"invalid,omitempty"`
}
const (
// InvalidSpanID has either the wrong length, or does not contain hex digits
InvalidSpanID = "invalidSpanID"
// InvalidTraceID has either the wrong length, or does not contain hex digits
InvalidTraceID = "invalidTraceID"
// ZipkinV2BinaryAnnotations are not allowed
ZipkinV2BinaryAnnotations = "zipkinV2BinaryAnnotations"
// NilServiceName when no localendpoint.name is provided
NilServiceName = "nilServiceName"
// ZeroTraceID when the traceid bytes are all zero
ZeroTraceID = "zeroTraceID"
// ZeroStartTime when the star time of the span is 0
ZeroStartTime = "zeroStartTime"
// TooManySpansInTrace when we find an abusive number of spans for a given traceID
TooManySpansInTrace = "tooManySpansInTrace"
// NilOperationName when no localendpoint.name is provided
NilOperationName = "nilOperationName"
// RequiredTagMissing means the span was missing a required tag
RequiredTagMissing = "requiredTagMissing"
// SpanTooLargeDropped means a span size was over limits and was dropped
SpanTooLargeDropped = "spanTooLargeDropped"
)
var errEmptySpanFilter = &Map{}
const (
// OK valid spans
OK = "ok"
)
// CheckInvalid is a nil safe check if this SpanFilter contains invalid keys
func (s *Map) CheckInvalid() bool {
return s != nil && len(s.Invalid) > 0
}
// Error returns a json representation of the Map
func (s *Map) Error() string {
bytes, err := json.Marshal(s)
resp := "Unable to format Map"
if err == nil {
resp = string(bytes)
}
return resp
}
// Add a error trace id
func (s *Map) Add(errType string, id string) {
if strings.EqualFold(errType, OK) {
s.Valid++
} else {
if s.Invalid == nil {
s.Invalid = make(map[string][]string)
}
s.Invalid[errType] = append(s.Invalid[errType], id)
}
}
// AddValid is a short cut and much faster
func (s *Map) AddValid(i int) {
s.Valid += i
}
// FromBytes returns a Map or an error
//
//nolint:nilerr
func FromBytes(body []byte) *Map {
var spanFilter Map
if err := json.Unmarshal(body, &spanFilter); err != nil {
return nil
}
return &spanFilter
}
// ReturnInvalidOrError returns nil for a valid SpanFilter, an invalid SpanFilter or an error containing the bytes
func ReturnInvalidOrError(body []byte) error {
if sf := FromBytes(body); sf != nil {
if sf.CheckInvalid() {
return sf
}
return nil
}
return errors.New(string(body))
}
type streamMetadata int
const (
spanFailures streamMetadata = iota
)
// WithSpanFilterContext gives you a request with the Map set
func WithSpanFilterContext(ctx context.Context, sf interface{}) context.Context {
return context.WithValue(ctx, spanFailures, sf)
}
// GetSpanFilterMapOrNew is a target for spanumsink.SinkFunc to be turned into a spanumsink.Sink
func GetSpanFilterMapOrNew(ctx context.Context) (context.Context, *Map) {
v := ctx.Value(spanFailures)
if v != nil {
if failMap, ok := v.(*Map); ok {
return ctx, failMap
}
}
failMap := &Map{}
return context.WithValue(ctx, spanFailures, failMap), failMap
}
// GetSpanFilterMapFromContext is a target for spanumsink.SinkFunc to be turned into a spanumsink.Sink
func GetSpanFilterMapFromContext(ctx context.Context) error {
v := ctx.Value(spanFailures)
if v != nil {
if failMap, ok := v.(*Map); ok {
return failMap
}
}
return errEmptySpanFilter
}
// IsMap returns whether an error is an instance of Map
func IsMap(err error) bool {
var errMap *Map
return goerrors.As(err, &errMap)
}
// IsInvalid returns false if it's a Map with no invalid entries or nil, else true
func IsInvalid(err error) bool {
var errMap *Map
if goerrors.As(err, &errMap) {
return errMap.CheckInvalid()
}
return err != nil
}