/
span.go
142 lines (123 loc) · 2.92 KB
/
span.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
package apptracer
import (
"golang.org/x/net/context"
"github.com/evalphobia/apptracer/platform"
)
// SpanWrapper contains multiple Span data for each platforms.
type SpanWrapper struct {
ctx context.Context
IsRoot bool
spanList []platform.Span
}
// Context returns context.Context in this SpanWrapper.
func (s *SpanWrapper) Context() context.Context {
return s.ctx
}
// NewChildSpan creates SpanWrapper with child span data.
func (s *SpanWrapper) NewChildSpan(name string) *SpanWrapper {
child := &SpanWrapper{
spanList: make([]platform.Span, len(s.spanList)),
}
for i, ss := range s.spanList {
child.spanList[i] = ss.NewChild(name)
}
return child
}
// SetLabel sets labels into each spans.
func (s *SpanWrapper) SetLabel(key, value string) *SpanWrapper {
for _, ss := range s.spanList {
ss.SetLabel(key, value)
}
return s
}
// SetLabels sets multiple labels into each spans.
func (s *SpanWrapper) SetLabels(labels []Label) *SpanWrapper {
if len(labels) == 0 {
return s
}
for _, ss := range s.spanList {
for _, l := range labels {
ss.SetLabel(l.Key, l.Value)
}
}
return s
}
// SetServiceName sets service name into each spans.
func (s *SpanWrapper) SetServiceName(name string) *SpanWrapper {
for _, ss := range s.spanList {
ss.SetServiceName(name)
}
return s
}
// SetVersion sets version data into each spans.
func (s *SpanWrapper) SetVersion(ver string) *SpanWrapper {
for _, ss := range s.spanList {
ss.SetVersion(ver)
}
return s
}
// SetEnvironment sets environment data into each spans.
func (s *SpanWrapper) SetEnvironment(env string) *SpanWrapper {
for _, ss := range s.spanList {
ss.SetEnvironment(env)
}
return s
}
// SetError sets error data into each spans.
func (s *SpanWrapper) SetError(err error) *SpanWrapper {
for _, ss := range s.spanList {
ss.SetError(err)
}
return s
}
// SetUser sets user data into each spans.
func (s *SpanWrapper) SetUser(user string) *SpanWrapper {
for _, ss := range s.spanList {
ss.SetUser(user)
}
return s
}
// SetResponse sets response status code into each spans.
func (s *SpanWrapper) SetResponse(status int) *SpanWrapper {
for _, ss := range s.spanList {
ss.SetResponse(status)
}
return s
}
// SetSQL sets sql data into each spans.
func (s *SpanWrapper) SetSQL(sql string) *SpanWrapper {
for _, ss := range s.spanList {
ss.SetSQL(sql)
}
return s
}
// Finish sends tracing data(span) for each platforms.
func (s *SpanWrapper) Finish() {
for _, ss := range s.spanList {
switch {
case s.IsRoot:
ss.FinishWait()
default:
ss.Finish()
}
}
}
// GetSummary gets trace summary of the spans.
func (s *SpanWrapper) GetSummary() []string {
for _, ss := range s.spanList {
if !ss.CanGetSummary() {
continue
}
return ss.GetSummary()
}
return nil
}
// OutputSummary prints trace summary of the spans.
func (s *SpanWrapper) OutputSummary() {
for _, ss := range s.spanList {
if !ss.CanGetSummary() {
continue
}
ss.OutputSummary()
}
}