@@ -10,18 +10,18 @@ import (
10
10
"go.unistack.org/micro/v3/tracer"
11
11
)
12
12
13
- var _ tracer.Tracer = & opentracingTracer {}
13
+ var _ tracer.Tracer = & otTracer {}
14
14
15
- type opentracingTracer struct {
15
+ type otTracer struct {
16
16
opts tracer.Options
17
17
tracer opentracing.Tracer
18
18
}
19
19
20
- func (ot * opentracingTracer ) Name () string {
20
+ func (ot * otTracer ) Name () string {
21
21
return ot .opts .Name
22
22
}
23
23
24
- func (ot * opentracingTracer ) Init (opts ... tracer.Option ) error {
24
+ func (ot * otTracer ) Init (opts ... tracer.Option ) error {
25
25
for _ , o := range opts {
26
26
o (& ot .opts )
27
27
}
@@ -35,69 +35,109 @@ func (ot *opentracingTracer) Init(opts ...tracer.Option) error {
35
35
return nil
36
36
}
37
37
38
- func (ot * opentracingTracer ) Start (ctx context.Context , name string , opts ... tracer.SpanOption ) (context.Context , tracer.Span ) {
39
- ctx , span , _ := startSpanFromIncomingContext (ctx , ot .tracer , name )
40
- return ctx , & opentracingSpan {span : span }
38
+ func (ot * otTracer ) Start (ctx context.Context , name string , opts ... tracer.SpanOption ) (context.Context , tracer.Span ) {
39
+ options := tracer .NewSpanOptions (opts ... )
40
+ var span opentracing.Span
41
+ switch options .Kind {
42
+ case tracer .SpanKindInternal , tracer .SpanKindUnspecified :
43
+ ctx , span = ot .startSpanFromContext (ctx , name )
44
+ case tracer .SpanKindClient , tracer .SpanKindProducer :
45
+ ctx , span = ot .startSpanFromOutgoingContext (ctx , name )
46
+ case tracer .SpanKindServer , tracer .SpanKindConsumer :
47
+ ctx , span = ot .startSpanFromIncomingContext (ctx , ot .tracer , name )
48
+ }
49
+ return ctx , & otSpan {span : span , opts : options }
50
+ }
51
+
52
+ type otSpan struct {
53
+ span opentracing.Span
54
+ opts tracer.SpanOptions
55
+ status tracer.SpanStatus
56
+ statusMsg string
41
57
}
42
58
43
- type opentracingSpan struct {
44
- span opentracing.Span
45
- labels []interface {}
59
+ func (os * otSpan ) SetStatus (st tracer.SpanStatus , msg string ) {
60
+ switch st {
61
+ case tracer .SpanStatusError :
62
+ os .span .SetTag ("error" , true )
63
+ }
64
+ os .status = st
65
+ os .statusMsg = msg
46
66
}
47
67
48
- func (os * opentracingSpan ) Tracer () tracer.Tracer {
49
- return & opentracingTracer { tracer : os .span . Tracer ()}
68
+ func (os * otSpan ) Status () ( tracer.SpanStatus , string ) {
69
+ return os . status , os .statusMsg
50
70
}
51
71
52
- func (os * opentracingSpan ) Finish (opts ... tracer.SpanOption ) {
53
- if len (os .labels ) > 0 {
54
- os .span .LogKV (os .labels ... )
72
+ func (os * otSpan ) Tracer () tracer.Tracer {
73
+ return & otTracer {tracer : os .span .Tracer ()}
74
+ }
75
+
76
+ func (os * otSpan ) Finish (opts ... tracer.SpanOption ) {
77
+ if len (os .opts .Labels ) > 0 {
78
+ os .span .LogKV (os .opts .Labels ... )
55
79
}
56
80
os .span .Finish ()
57
81
}
58
82
59
- func (os * opentracingSpan ) AddEvent (name string , opts ... tracer.EventOption ) {
83
+ func (os * otSpan ) AddEvent (name string , opts ... tracer.EventOption ) {
60
84
os .span .LogFields (log .Event (name ))
61
85
}
62
86
63
- func (os * opentracingSpan ) Context () context.Context {
64
- return tracer . NewSpanContext (context .Background (), os )
87
+ func (os * otSpan ) Context () context.Context {
88
+ return opentracing . ContextWithSpan (context .Background (), os . span )
65
89
}
66
90
67
- func (os * opentracingSpan ) SetName (name string ) {
91
+ func (os * otSpan ) SetName (name string ) {
68
92
os .span = os .span .SetOperationName (name )
69
93
}
70
94
71
- func (os * opentracingSpan ) SetLabels (labels ... interface {}) {
72
- os .labels = labels
95
+ func (os * otSpan ) SetLabels (labels ... interface {}) {
96
+ os .opts . Labels = labels
73
97
}
74
98
75
- func (os * opentracingSpan ) AddLabels ( labels ... interface {}) {
76
- os . labels = append ( os .labels , labels ... )
99
+ func (os * otSpan ) Kind () tracer. SpanKind {
100
+ return os .opts . Kind
77
101
}
78
102
79
- func NewTracer (opts ... tracer.Option ) * opentracingTracer {
103
+ func (os * otSpan ) AddLabels (labels ... interface {}) {
104
+ os .opts .Labels = append (os .opts .Labels , labels ... )
105
+ }
106
+
107
+ func NewTracer (opts ... tracer.Option ) * otTracer {
80
108
options := tracer .NewOptions (opts ... )
81
- return & opentracingTracer {opts : options }
109
+ return & otTracer {opts : options }
82
110
}
83
111
84
112
func spanFromContext (ctx context.Context ) opentracing.Span {
85
113
return opentracing .SpanFromContext (ctx )
86
114
}
87
115
88
- // startSpanFromOutgoingContext returns a new span with the given operation name and options. If a span
89
- // is found in the context, it will be used as the parent of the resulting span.
90
- func startSpanFromOutgoingContext (ctx context.Context , tracer opentracing.Tracer , name string , opts ... opentracing.StartSpanOption ) (context.Context , opentracing.Span , error ) {
116
+ func (ot * otTracer ) startSpanFromContext (ctx context.Context , name string , opts ... opentracing.StartSpanOption ) (context.Context , opentracing.Span ) {
117
+ if parentSpan := opentracing .SpanFromContext (ctx ); parentSpan != nil {
118
+ opts = append (opts , opentracing .ChildOf (parentSpan .Context ()))
119
+ }
120
+
121
+ md := metadata .New (1 )
122
+
123
+ sp := ot .tracer .StartSpan (name , opts ... )
124
+ if err := sp .Tracer ().Inject (sp .Context (), opentracing .TextMap , opentracing .TextMapCarrier (md )); err != nil {
125
+ return nil , nil
126
+ }
127
+
128
+ ctx = opentracing .ContextWithSpan (ctx , sp )
129
+
130
+ return ctx , sp
131
+ }
132
+
133
+ func (ot * otTracer ) startSpanFromOutgoingContext (ctx context.Context , name string , opts ... opentracing.StartSpanOption ) (context.Context , opentracing.Span ) {
91
134
var parentCtx opentracing.SpanContext
92
135
93
- md , ok := metadata .FromIncomingContext (ctx )
94
- // Find parent span.
95
- if parentSpan := opentracing .SpanFromContext (ctx ); parentSpan != nil {
96
- // First try to get span within current service boundary.
97
- parentCtx = parentSpan .Context ()
98
- } else if spanCtx , err := tracer .Extract (opentracing .TextMap , opentracing .TextMapCarrier (md )); err == nil && ok {
99
- // If there doesn't exist, try to get it from metadata(which is cross boundary)
100
- parentCtx = spanCtx
136
+ md , ok := metadata .FromOutgoingContext (ctx )
137
+ if ok && md != nil {
138
+ if spanCtx , err := ot .tracer .Extract (opentracing .TextMap , opentracing .TextMapCarrier (md )); err == nil && ok {
139
+ parentCtx = spanCtx
140
+ }
101
141
}
102
142
103
143
if parentCtx != nil {
@@ -106,52 +146,38 @@ func startSpanFromOutgoingContext(ctx context.Context, tracer opentracing.Tracer
106
146
107
147
nmd := metadata .Copy (md )
108
148
109
- sp := tracer .StartSpan (name , opts ... )
149
+ sp := ot . tracer .StartSpan (name , opts ... )
110
150
if err := sp .Tracer ().Inject (sp .Context (), opentracing .TextMap , opentracing .TextMapCarrier (nmd )); err != nil {
111
- return nil , nil , err
151
+ return nil , nil
112
152
}
113
153
114
154
ctx = metadata .NewOutgoingContext (opentracing .ContextWithSpan (ctx , sp ), nmd )
115
155
116
- return ctx , sp , nil
156
+ return ctx , sp
117
157
}
118
158
119
- // startSpanFromIncomingContext returns a new span with the given operation name and options. If a span
120
- // is found in the context, it will be used as the parent of the resulting span.
121
- func startSpanFromIncomingContext (ctx context.Context , tracer opentracing.Tracer , name string , opts ... opentracing.StartSpanOption ) (context.Context , opentracing.Span , error ) {
159
+ func (ot * otTracer ) startSpanFromIncomingContext (ctx context.Context , tracer opentracing.Tracer , name string , opts ... opentracing.StartSpanOption ) (context.Context , opentracing.Span ) {
122
160
var parentCtx opentracing.SpanContext
123
161
124
- // Find parent span.
125
162
md , ok := metadata .FromIncomingContext (ctx )
126
- if parentSpan := opentracing .SpanFromContext (ctx ); parentSpan != nil {
127
- // First try to get span within current service boundary.
128
- parentCtx = parentSpan .Context ()
129
- } else if spanCtx , err := tracer .Extract (opentracing .TextMap , opentracing .TextMapCarrier (md )); err == nil && ok {
130
- // If there doesn't exist, try to get it from metadata(which is cross boundary)
131
- parentCtx = spanCtx
163
+ if ok && md != nil {
164
+ if spanCtx , err := tracer .Extract (opentracing .TextMap , opentracing .TextMapCarrier (md )); err == nil {
165
+ parentCtx = spanCtx
166
+ }
132
167
}
133
168
134
169
if parentCtx != nil {
135
170
opts = append (opts , opentracing .ChildOf (parentCtx ))
136
171
}
137
172
138
- var nmd metadata.Metadata
139
- if ok {
140
- nmd = metadata .New (len (md ))
141
- } else {
142
- nmd = metadata .New (0 )
143
- }
173
+ nmd := metadata .Copy (md )
144
174
145
175
sp := tracer .StartSpan (name , opts ... )
146
176
if err := sp .Tracer ().Inject (sp .Context (), opentracing .TextMap , opentracing .TextMapCarrier (nmd )); err != nil {
147
- return nil , nil , err
148
- }
149
-
150
- for k , v := range md {
151
- nmd .Set (k , v )
177
+ return nil , nil
152
178
}
153
179
154
180
ctx = metadata .NewIncomingContext (opentracing .ContextWithSpan (ctx , sp ), nmd )
155
181
156
- return ctx , sp , nil
182
+ return ctx , sp
157
183
}
0 commit comments