Skip to content

Commit 09b677e

Browse files
authored
Merge pull request #30 from unistack-org/spanfix
fix span tags
2 parents 93cb40a + d0b475b commit 09b677e

File tree

4 files changed

+92
-68
lines changed

4 files changed

+92
-68
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ go 1.16
44

55
require (
66
github.com/opentracing/opentracing-go v1.2.0
7-
go.unistack.org/micro/v3 v3.10.1
7+
go.unistack.org/micro/v3 v3.10.4
88
gopkg.in/yaml.v2 v2.4.0 // indirect
99
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:
7373
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
7474
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
7575
go.unistack.org/micro-proto/v3 v3.3.1/go.mod h1:cwRyv8uInM2I7EbU7O8Fx2Ls3N90Uw9UCCcq4olOdfE=
76-
go.unistack.org/micro/v3 v3.10.1 h1:MbtPj7ueTw4x6lL2Ok2ez70ORyGXBWhxizO5fQsnAA4=
77-
go.unistack.org/micro/v3 v3.10.1/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI=
76+
go.unistack.org/micro/v3 v3.10.4 h1:8HneC2t7oteTwwkFLmSg5bs62h/OqEzevx/IbXG1vRo=
77+
go.unistack.org/micro/v3 v3.10.4/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI=
7878
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
7979
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
8080
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

opentracing.go

Lines changed: 87 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ import (
1010
"go.unistack.org/micro/v3/tracer"
1111
)
1212

13-
var _ tracer.Tracer = &opentracingTracer{}
13+
var _ tracer.Tracer = &otTracer{}
1414

15-
type opentracingTracer struct {
15+
type otTracer struct {
1616
opts tracer.Options
1717
tracer opentracing.Tracer
1818
}
1919

20-
func (ot *opentracingTracer) Name() string {
20+
func (ot *otTracer) Name() string {
2121
return ot.opts.Name
2222
}
2323

24-
func (ot *opentracingTracer) Init(opts ...tracer.Option) error {
24+
func (ot *otTracer) Init(opts ...tracer.Option) error {
2525
for _, o := range opts {
2626
o(&ot.opts)
2727
}
@@ -35,69 +35,109 @@ func (ot *opentracingTracer) Init(opts ...tracer.Option) error {
3535
return nil
3636
}
3737

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
4157
}
4258

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
4666
}
4767

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
5070
}
5171

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...)
5579
}
5680
os.span.Finish()
5781
}
5882

59-
func (os *opentracingSpan) AddEvent(name string, opts ...tracer.EventOption) {
83+
func (os *otSpan) AddEvent(name string, opts ...tracer.EventOption) {
6084
os.span.LogFields(log.Event(name))
6185
}
6286

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)
6589
}
6690

67-
func (os *opentracingSpan) SetName(name string) {
91+
func (os *otSpan) SetName(name string) {
6892
os.span = os.span.SetOperationName(name)
6993
}
7094

71-
func (os *opentracingSpan) SetLabels(labels ...interface{}) {
72-
os.labels = labels
95+
func (os *otSpan) SetLabels(labels ...interface{}) {
96+
os.opts.Labels = labels
7397
}
7498

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
77101
}
78102

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 {
80108
options := tracer.NewOptions(opts...)
81-
return &opentracingTracer{opts: options}
109+
return &otTracer{opts: options}
82110
}
83111

84112
func spanFromContext(ctx context.Context) opentracing.Span {
85113
return opentracing.SpanFromContext(ctx)
86114
}
87115

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) {
91134
var parentCtx opentracing.SpanContext
92135

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+
}
101141
}
102142

103143
if parentCtx != nil {
@@ -106,52 +146,38 @@ func startSpanFromOutgoingContext(ctx context.Context, tracer opentracing.Tracer
106146

107147
nmd := metadata.Copy(md)
108148

109-
sp := tracer.StartSpan(name, opts...)
149+
sp := ot.tracer.StartSpan(name, opts...)
110150
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(nmd)); err != nil {
111-
return nil, nil, err
151+
return nil, nil
112152
}
113153

114154
ctx = metadata.NewOutgoingContext(opentracing.ContextWithSpan(ctx, sp), nmd)
115155

116-
return ctx, sp, nil
156+
return ctx, sp
117157
}
118158

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) {
122160
var parentCtx opentracing.SpanContext
123161

124-
// Find parent span.
125162
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+
}
132167
}
133168

134169
if parentCtx != nil {
135170
opts = append(opts, opentracing.ChildOf(parentCtx))
136171
}
137172

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)
144174

145175
sp := tracer.StartSpan(name, opts...)
146176
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
152178
}
153179

154180
ctx = metadata.NewIncomingContext(opentracing.ContextWithSpan(ctx, sp), nmd)
155181

156-
return ctx, sp, nil
182+
return ctx, sp
157183
}

opentracing_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ func TestStartSpanFromIncomingContext(t *testing.T) {
2020
ctx = metadata.NewIncomingContext(ctx, md)
2121

2222
tracer := opentracing.GlobalTracer()
23+
ot := &otTracer{tracer: tracer}
2324

2425
g.Add(8000)
2526
cherr := make(chan error)
2627
for i := 0; i < 8000; i++ {
2728
go func() {
2829
defer g.Done()
29-
_, sp, err := startSpanFromIncomingContext(ctx, tracer, "test")
30-
if err != nil {
31-
cherr <- err
32-
}
30+
_, sp := ot.startSpanFromIncomingContext(ctx, tracer, "test")
3331
sp.Finish()
3432
}()
3533
}

0 commit comments

Comments
 (0)