-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathtracing.go
137 lines (112 loc) · 3.01 KB
/
tracing.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
package proxy
import (
ot "github.com/opentracing/opentracing-go"
"github.com/zalando/skipper/tracing"
)
const (
ClientRequestStateTag = "client.request"
ComponentTag = "component"
ErrorTag = "error"
BlockTag = "blocked"
FlowIDTag = "flow_id"
HostnameTag = "hostname"
HTTPHostTag = "http.host"
HTTPMethodTag = "http.method"
HTTPRemoteIPTag = "http.remote_ip"
HTTPPathTag = "http.path"
HTTPUrlTag = "http.url"
HTTPStatusCodeTag = "http.status_code"
SkipperRouteIDTag = "skipper.route_id"
SpanKindTag = "span.kind"
ClientRequestCanceled = "canceled"
SpanKindClient = "client"
SpanKindServer = "server"
EndEvent = "end"
StartEvent = "start"
StreamHeadersEvent = "stream_Headers"
StreamBodyEvent = "streamBody.byte"
StreamBodyError = "streamBody error"
)
type proxyTracing struct {
tracer ot.Tracer
initialOperationName string
disableFilterSpans bool
logFilterLifecycleEvents bool
logStreamEvents bool
excludeTags map[string]bool
}
type filterTracing struct {
span ot.Span
logEvents bool
}
func newProxyTracing(p *OpenTracingParams) *proxyTracing {
if p == nil {
p = &OpenTracingParams{}
}
if p.InitialSpan == "" {
p.InitialSpan = "ingress"
}
if p.Tracer == nil {
p.Tracer = &ot.NoopTracer{}
}
excludedTags := map[string]bool{}
for _, t := range p.ExcludeTags {
excludedTags[t] = true
}
return &proxyTracing{
tracer: p.Tracer,
initialOperationName: p.InitialSpan,
disableFilterSpans: p.DisableFilterSpans,
logFilterLifecycleEvents: p.LogFilterEvents,
logStreamEvents: p.LogStreamEvents,
excludeTags: excludedTags,
}
}
func (t *proxyTracing) logEvent(span ot.Span, eventName, eventValue string) {
if span == nil {
return
}
span.LogKV(eventName, ensureUTF8(eventValue))
}
func (t *proxyTracing) setTag(span ot.Span, key string, value interface{}) *proxyTracing {
if span == nil {
return t
}
if !t.excludeTags[key] {
if s, ok := value.(string); ok {
span.SetTag(key, ensureUTF8(s))
} else {
span.SetTag(key, value)
}
}
return t
}
func (t *proxyTracing) logStreamEvent(span ot.Span, eventName, eventValue string) {
if !t.logStreamEvents {
return
}
t.logEvent(span, eventName, ensureUTF8(eventValue))
}
func (t *proxyTracing) startFilterTracing(operation string, ctx *context) *filterTracing {
if t.disableFilterSpans {
return nil
}
span := tracing.CreateSpan(operation, ctx.request.Context(), t.tracer)
ctx.parentSpan = span
return &filterTracing{span, t.logFilterLifecycleEvents}
}
func (t *filterTracing) finish() {
if t != nil {
t.span.Finish()
}
}
func (t *filterTracing) logStart(filterName string) {
if t != nil && t.logEvents {
t.span.LogKV(filterName, StartEvent)
}
}
func (t *filterTracing) logEnd(filterName string) {
if t != nil && t.logEvents {
t.span.LogKV(filterName, EndEvent)
}
}